Skip to main content

Installation

npm add @loops-fi/sdk

Quickstart

import { Loops } from "@loops-fi/sdk";

const loops = new Loops({
  apiKey: process.env["LOOPS_API_KEY"] ?? "",
});

async function run() {
  const result = await loops.checkoutSessions.create({
    paymentLinkId: "<id>",
  });

  console.log(result);
}

run();

Authentication

The SDK uses API Key authentication. Set your API key when initializing the client:
const loops = new Loops({
  apiKey: process.env["LOOPS_API_KEY"] ?? "",
});

Available Operations

The SDK provides access to the following API endpoint:

Checkout Sessions

  • Create Checkout Session - loops.checkoutSessions.create()
const result = await loops.checkoutSessions.create({
  paymentLinkId: "<payment-link-id>",
  externalCustomerId: "customer-123", // optional
  metadata: {
    // optional
    orderId: "order-456",
    source: "website",
  },
});

Error Handling

The SDK includes comprehensive error handling with specific error types:
import * as errors from "@loops-fi/sdk/models/errors";

try {
  const result = await loops.checkoutSessions.create({
    paymentLinkId: "<id>",
  });
} catch (error) {
  if (error instanceof errors.BadRequestError) {
    console.log("Bad request:", error.data$.error);
  } else if (error instanceof errors.UnauthorizedError) {
    console.log("Unauthorized - check your API key");
  } else if (error instanceof errors.LoopsError) {
    console.log("API error:", error.message);
  }
}

Server Selection

You can configure the SDK to use different servers:
// Development server
const loops = new Loops({
  serverIdx: 0, // http://localhost:3001
  apiKey: process.env["LOOPS_API_KEY"] ?? "",
});

// Production server (default)
const loops = new Loops({
  serverIdx: 1, // https://api.loops.fi
  apiKey: process.env["LOOPS_API_KEY"] ?? "",
});

Retries

The SDK supports configurable retry strategies:
const result = await loops.checkoutSessions.create(
  {
    paymentLinkId: "<id>",
  },
  {
    retries: {
      strategy: "backoff",
      backoff: {
        initialInterval: 1,
        maxInterval: 50,
        exponent: 1.1,
        maxElapsedTime: 100,
      },
      retryConnectionErrors: false,
    },
  }
);

Debugging

Enable debug logging to see SDK requests and responses:
const loops = new Loops({
  debugLogger: console,
  apiKey: process.env["LOOPS_API_KEY"] ?? "",
});
Or set the environment variable:
LOOPS_DEBUG=true

More Information

I