You can also create products programmatically and get the ID from the response:
Copy
import { Loops } from "@loops-fi/sdk";const loops = new Loops({ apiKey: process.env.LOOPS_API_KEY,});// Create a productconst product = await loops.products.create({ name: "Premium Plan", description: "Access to all premium features", priceAmount: 2999, // $29.99 in cents priceCurrency: "usd",});console.log("Product ID:", product.id);// Save this ID - you'll use it for checkout sessions// product.id -> "prod_abc123xyz"
One-time setup: You typically create products once (either in dashboard or via API), then reuse the same product ID for multiple checkout sessions.
import { Loops } from "@loops-fi/sdk";const loops = new Loops({ apiKey: process.env.LOOPS_API_KEY,});async function createCheckout() { const session = await loops.checkoutSessions.create({ // Use the product ID you copied from dashboard or got from API productId: "prod_abc123xyz", externalCustomerId: "customer_123", metadata: { orderId: "order_456", }, }); console.log("Checkout URL:", session.url); return session.url;}
V2 accepts both parameters: Since V2 supports both workflows:
productId (recommended) - Direct product checkout: prod_abc123xyz
Here’s a full example showing the entire flow from product creation to checkout:
Copy
import { Loops } from "@loops-fi/sdk";const loops = new Loops({ apiKey: process.env.LOOPS_API_KEY,});// Step 1: Create a product (do this once)async function setupProduct() { const product = await loops.products.create({ name: "Premium Membership", description: "Get access to all premium features", priceAmount: 4999, // $49.99 priceCurrency: "usd", }); console.log("Product created:", product.id); // Save this ID in your database or config return product.id;}// Step 2: Create checkout sessions (do this for each purchase)async function createCheckoutForCustomer(productId: string, userId: string) { const session = await loops.checkoutSessions.create({ productId: productId, // Use the product ID from step 1 externalCustomerId: userId, metadata: { userId: userId, plan: "premium", source: "website", }, }); console.log("Checkout URL:", session.url); // Redirect user to session.url return session.url;}// Example usage:async function main() { // First time setup const productId = await setupProduct(); // productId = "prod_abc123xyz" // Now use this productId for all your checkouts const checkoutUrl = await createCheckoutForCustomer(productId, "user_123"); console.log("Send customer to:", checkoutUrl);}
Important: Create your products once (either in dashboard or via API), then reuse the same product ID for all checkout sessions. Don’t create a new product for each checkout!
Don’t rely solely on redirects for order fulfillment! Always use webhooks to verify payment completion, as users can close the browser before being redirected.
Use webhooks as your primary method for detecting successful payments. Redirects should only be used for user experience.See our Webhooks Guide for implementation details.
Use metadata to store information you’ll need later:
Copy
metadata: { // ✅ Good - useful identifiers orderId: "123", userId: "user_456", // ❌ Bad - sensitive information creditCard: "1234-5678-9012-3456", // Never store PII password: "secret123", // Never store credentials}