Guides

Wrap an API

Put a per-call price on any existing HTTP endpoint with one SDK call.

One handler

The lowest-level primitive is tael(). Give it a price, your settlement details, and a handler; it returns a payment-gated Fetch handler.

TStael()
import { tael, createMockVerifier } from "@tael/sdk";

export const handler = tael({
  price: "0.02",
  payTo: "G...",
  issuer: "G...",
  network: "stellar-testnet",
  verifier: createMockVerifier(),
  handler: () => Response.json({ ok: true }),
});

Shared defaults

Most services expose several priced routes that share the same payTo, issuer, network, and verifier. Bind those once with createTael() so each route only declares what's different — its price and handler.

TScreateTael()
import { createTael, createMockVerifier } from "@tael/sdk";

export const paid = createTael({
  payTo: process.env.TAEL_PAY_TO!,
  issuer: process.env.USDC_ISSUER!,
  network: "stellar-testnet",
  verifier: createMockVerifier(),
});

Multiple routes

Reuse the wrapper across endpoints, each with its own price:

›_routes
export const ocr = paid({
  price: "0.02",
  description: "Extract text from a document",
  handler: runOcr,
});

export const translate = paid({
  price: "0.05",
  description: "Translate a document",
  handler: runTranslate,
});
Prices are decimal strings of USDC (e.g. "0.02"). The value becomes maxAmountRequired in the 402 challenge for that route.

Reading the receipt

Your handler runs only after payment settles and receives a receipt alongside the request, so you can attribute usage or log settlement:

TShandler context
handler: async ({ request, receipt }) => {
  const input = await request.json();
  const result = await doWork(input);
  return Response.json({ result, paidBy: receipt.payer });
}

Frameworks

Because the wrapper is a plain (Request) => Promise<Response>, it works anywhere the Web Fetch standard does:

  • Next.jsexport const POST = paid({...}) in a route handler.
  • Hono — mount it as a handler on a route.
  • Bun, Deno, Cloudflare Workers — use it as the fetch entrypoint.

For the exact option shapes, see the Node.js SDK reference.