intents.fi
Next.js · App Router · Node.js

Accept crypto payments in Next.js with two route handlers

Accept crypto in Next.js with two route handlers: one creates the invoice, one verifies the webhook. Customers pay in any token; you get USDC, USDT or BTC.

This page covers the server side in a Next.js app: where the invoice is created, how the webhook is verified, and what changes on Vercel. Component props and theming are on the React page and in the widget docs.

npm install @intents-fi/sdk
import { createClient } from "@intents-fi/sdk";
import { redirect } from "next/navigation";
import { getProduct } from "@/lib/catalogue";

const intents = createClient({ apiKey: process.env.INTENTS_SECRET_KEY! });

export async function POST(request: Request) {
  const form = await request.formData();
  const product = await getProduct(String(form.get("sku"))); // price from your catalogue
  const reference = `order_${crypto.randomUUID()}`;

  const invoice = await intents.checkout.invoices.create({
    amount: product.price,               // "49.00", in USD
    reference,                           // idempotency key
    description: product.name,
    successUrl: `${process.env.APP_URL}/thanks?order=${reference}`,
    cancelUrl: `${process.env.APP_URL}/cart`,
  });
  redirect(invoice.url);                 // the hosted checkout
}
Short answerUpdated · Facts checked

To accept crypto payments in Next.js, create the invoice in a route handler or server action, send the customer to the hosted checkout or open it with a React button, and mark the order paid only from a verified webhook. With Intents Checkout that is two route handlers, and USDC, USDT or BTC settles to your own wallet.

Our takeStart with the redirect to the hosted page; add <PayButton> later if you want the payment inside your app, because the server code does not change.

Code

app/buy/route.ts · ts
import { createClient } from "@intents-fi/sdk";
import { redirect } from "next/navigation";
import { getProduct } from "@/lib/catalogue";

const intents = createClient({ apiKey: process.env.INTENTS_SECRET_KEY! });

export async function POST(request: Request) {
  const form = await request.formData();
  const product = await getProduct(String(form.get("sku"))); // price from your catalogue
  const reference = `order_${crypto.randomUUID()}`;

  const invoice = await intents.checkout.invoices.create({
    amount: product.price,               // "49.00", in USD
    reference,                           // idempotency key
    description: product.name,
    successUrl: `${process.env.APP_URL}/thanks?order=${reference}`,
    cancelUrl: `${process.env.APP_URL}/cart`,
  });
  redirect(invoice.url);                 // the hosted checkout
}
app/api/webhooks/intents/route.ts · ts
import { verifyAndParse } from "@intents-fi/sdk";
import { markPaid } from "@/lib/orders";

export async function POST(request: Request) {
  const body = await request.text();     // not request.json(): the signature covers raw bytes
  const event = await verifyAndParse(
    body,
    request.headers.get("x-intents-signature"),
    process.env.INTENTS_WEBHOOK_SECRET!,
  );
  if (!event) return new Response("bad signature", { status: 400 });

  if (event.data.status === "paid" && event.data.reference) {
    await markPaid(event.data.reference, event.data.id, event.late === true); // idempotent
  }
  return new Response(null, { status: 204 });
}
.env.local · bash
# Test keys while you build: dashboard.intents.fi → Developers → API keys
INTENTS_SECRET_KEY=ik_test_...
# Returned once, when you register the webhook endpoint
INTENTS_WEBHOOK_SECRET=whsec_...
APP_URL=http://localhost:3000

The whole integration in a Next.js app

Accepting crypto payments in a Next.js app takes three pieces, and two of them are files you already know how to write.

Piece Where it lives What it does
Create the invoice app/buy/route.ts (or a server action) Looks up the price, calls invoices.create(), redirects to invoice.url
Take the payment The hosted page at pay.intents.fi, or <PayButton> Shows the price, lets the customer choose a token and network, collects the payment
Confirm it app/api/webhooks/intents/route.ts Verifies the signature and marks the order paid

Nothing in the browser holds a key, and nothing in the browser can mark an order paid. The customer pays in what they hold, such as USDT on Tron, SOL or ETH, and the exact amount of USDC, USDT or native BTC you chose lands at your own address. The code above is the complete server side; the same flow over plain HTTP is in the quickstart, and every create field is explained on the crypto checkout API page.

Where the code runs

Next.js mixes server and client code in one tree, which makes it easy to put the secret key in the wrong place. A simple rule keeps it safe: the SDK client is only ever imported from server files.

  • Route handlers (app/**/route.ts) run on the server. Create invoices and receive webhooks here.
  • Server actions run on the server too, so a form in a server component can call invoices.create() directly and then redirect().
  • Server components can read an invoice with invoices.get() to show its status on an order page.
  • Client components ("use client") only ever receive an invoice’s id and clientSecret, and only if you use the React button. The clientSecret is good for that one invoice.

Keep the key in INTENTS_SECRET_KEY without the NEXT_PUBLIC_ prefix, which is what stops Next.js from inlining it into the client bundle.

Hosted page, button or headless

All three use the same route handlers. The only difference is how the customer sees the payment.

Side by side

Side by side
Compared onRedirect to the hosted page<PayButton> modaluseCheckout() headless
Next.js codeOne route handler and a form BetterA route handler plus a client componentA route handler plus your own screens
Packages@intents-fi/sdk Better@intents-fi/sdk and @intents-fi/react@intents-fi/sdk and @intents-fi/react
Customer leaves your siteYes, to pay.intents.fi, then backNoNo
What reaches the browserOnly the checkout URLThe invoice's clientSecretThe invoice's clientSecret
Safety elementsBuilt inBuilt inYou render them from required.*
Webhook handlerSameSameSame

The redirect is the shortest path and the one we recommend for a first release. If you later want the payment inside your app, add @intents-fi/react, return invoice.id and invoice.clientSecret from the route handler instead of redirecting, and render <PayButton> in a client component. The React page covers that step, the props and the theming.

The webhook route, line by line

The webhook is the part that has to be right, because it is the only thing allowed to ship an order.

  1. Read the raw body. await request.text(). In the App Router nothing parses the body before your handler, so there is nothing to switch off.
  2. Verify. verifyAndParse() checks the x-intents-signature header, an HMAC-SHA256 over the timestamp and the body, and rejects deliveries older than five minutes. It returns null on a bad signature: answer 400.
  3. Trust data.status, not type. Deliveries can repeat and arrive out of order. The event’s data is the full invoice, exactly as GET /v0/invoices/{id} returns it.
  4. Fulfil once. Key the update on reference, and make a second call a no-op.
  5. Answer 2xx quickly. A failed or slow answer is retried, up to 8 attempts over about 23 hours. Push slow work, such as sending email, to a queue.

If you still use the Pages Router, the API route must receive the unparsed body. Turn off the built-in body parser for that one route and read the stream yourself before verifying. The webhooks reference lists every event, the retry schedule and the signature format.

Running it locally and on Vercel

Step Locally On Vercel
Keys ik_test_… in .env.local Project environment variables, test key in Preview, live key in Production
Webhook URL A tunnel pointing at /api/webhooks/intents https://your-domain/api/webhooks/intents
Register it webhooks.create() once with the test key Once with the live key; live endpoints are separate
Pay Complete the test payment on the hosted page A real $5 invoice from a phone, on the chain your customers use

Each registration returns a whsec_… secret once. Store it as INTENTS_WEBHOOK_SECRET for that environment. A preview deployment with a test key and a production deployment with a live key can run side by side without touching each other’s invoices, because a test key and a live key never share data.

Before you ship

0 of 7 done

Let an agent scaffold it

The Intents Checkout agent setup prompt is written for a Next.js App Router project. Given an ik_test_ key, Claude Code, Cursor or Codex adds the client file, the checkout route, the webhook route and an order store, then runs a test payment end to end. Review the diff against the checklist above before you merge it. The SDK page explains what each call does if you want to write it by hand.

Cost and what you receive

Each settled payment costs 0.4%, with no fixed fee and no setup, monthly or payout fee. The customer’s network and routing costs are inside the amount they are quoted, so you receive the invoice amount less 0.4%. Settlement is USDC on Base, Arbitrum, Ethereum, Solana or Polygon, or USDT on Tron, Ethereum, Arbitrum or BSC, at an address only a dashboard session can change. Compare it on pricing, see the hosted checkout, or read how a subscription-style product can bill in crypto on crypto payments for SaaS.

Questions

Short answers. The docs go deeper.

How do I accept crypto payments in Next.js?

Create an invoice on the server in a route handler or server action with your secret key, redirect the customer to the invoice's checkout URL or open it with <PayButton>, and fulfil the order from a verified invoice.paid webhook in a second route handler. The customer pays in any supported token and you receive USDC, USDT or native BTC.

Should I use a route handler or a server action?

Either works for creating the invoice, because both run on the server. A route handler suits a plain HTML form or a fetch from a client component. A server action suits a form in a server component. The webhook must be a route handler, because it is called by our servers, not by your page.

Why does the webhook route use request.text() instead of request.json()?

The signature is an HMAC over the exact bytes we sent. request.json() parses the body, and turning it back into a string can change spacing or key order, so the check fails. Read the text, verify it, then parse it. verifyAndParse() does both steps for you.

Does it work on the Vercel edge runtime?

Yes. The SDK has no runtime dependencies, uses the global fetch, and verifies webhooks with WebCrypto, so the same route handlers run on the Node.js or the edge runtime. The Node.js runtime is the simpler default if your order store needs a database driver.

Can I accept crypto payments in Node.js without Next.js?

Yes. The SDK runs on Node.js 18 or later with any framework. In Express, mount the webhook route with express.raw({ type: "application/json" }) so the handler receives the raw body, and pass it to verifyAndParse() as a string.

What happens if the customer closes the tab after paying?

Nothing is lost. The payment completes on the network whether or not the browser is open, and the invoice.paid webhook still reaches your route handler. That is why fulfilment belongs in the webhook and not on the successUrl page.

Do I need a crypto wallet to test this?

No. With an ik_test_ key the hosted page shows a Complete the test payment button instead of a deposit address. It walks the invoice to paid and sends a real signed webhook, and no money moves.

Get paid in your own wallet.

Customers pay in what they hold. You receive USDC or USDT on the chain you choose, or native bitcoin. We never hold your funds.