> ## Documentation Index
> Fetch the complete documentation index at: https://docs.onflay.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Checkout con Node.js

> Crea sesiones de checkout desde tu backend usando @onflay/node.

## Instalar

```bash theme={"dark"}
pnpm add @onflay/node
```

## Crear cliente

```ts theme={"dark"}
import { Onflay } from '@onflay/node';

const onflay = new Onflay(process.env.ONFLAY_API_KEY!);
```

## Checkout recomendado (Listing/Variant)

```ts theme={"dark"}
const session = await onflay.checkoutSessions.create(
  {
    listingId: 'lst_...',
    variantId: 'var_...', // opcional
    customerEmail: 'comprador@ejemplo.com',
    successUrl: 'https://tu-app.com/gracias',
    cancelUrl: 'https://tu-app.com/cancelado',
    metadata: { userId: 'user_123', orderId: 'order_456' },
  },
  { idempotencyKey: 'checkout:order_456' }
);

return Response.redirect(session.url, 303);
```

## Alternativa (lineItems con priceId)

Si ya usas catálogo técnico por `priceId`, `lineItems` sigue siendo compatible:

```ts theme={"dark"}
const session = await onflay.checkoutSessions.create({
  lineItems: [{ priceId: 'price_...', quantity: 1 }],
  customerEmail: 'comprador@ejemplo.com',
  successUrl: 'https://tu-app.com/gracias',
  cancelUrl: 'https://tu-app.com/cancelado',
});
```

## Ejemplo Next.js

```ts theme={"dark"}
import { NextResponse } from 'next/server';
import { Onflay } from '@onflay/node';

const onflay = new Onflay(process.env.ONFLAY_API_KEY!);

export async function POST(request: Request) {
  const body = await request.json();
  const session = await onflay.checkoutSessions.create(
    {
      listingId: body.listingId,
      variantId: body.variantId,
      customerEmail: body.email,
      successUrl: 'https://tu-app.com/gracias',
      cancelUrl: 'https://tu-app.com/precios',
      metadata: { userId: body.userId },
    },
    { idempotencyKey: `checkout:${body.userId}:${body.listingId}:${body.variantId ?? 'default'}` }
  );
  return NextResponse.json({ url: session.url });
}
```
