Get started

Quickstart

Gate any HTTP handler behind a USDC payment in a few minutes.

Install

Add the SDK to your project. It ships as ESM + types and has no framework dependency.

›_terminal
pnpm add @tael/sdk
# or: npm i @tael/sdk

Wrap a handler

createTael binds your service-wide settlement details once, then returns a paid() wrapper you put around any Fetch-style handler. Here it is as a Next.js route handler:

TSapp/api/ocr/route.ts
import { createTael, createMockVerifier } from "@tael/sdk";

const paid = createTael({
  payTo: process.env.TAEL_PAY_TO!,   // your Stellar address (G...)
  issuer: process.env.USDC_ISSUER!,  // USDC issuer on your network
  network: "stellar-testnet",
  verifier: createMockVerifier(),    // swap for the Stellar verifier in prod
});

export const POST = paid({
  price: "0.02",                     // 0.02 USDC per call
  description: "Extract text from a document",
  handler: async ({ request, receipt }) => {
    const body = await request.json();
    // ...your real work; runs only after payment settles...
    return Response.json({ text: "INVOICE #1042 ...", paidBy: receipt.payer });
  },
});
The wrapper returns a standard (Request) => Promise<Response>, so the same code drops into Hono, Bun, Deno, and Cloudflare Workers unchanged — not just Next.js.

Run it

Start your app and call the route with no payment. Tael answers with a 402 Payment Required challenge instead of running your handler:

›_terminal
curl -i -X POST http://localhost:3000/api/ocr

HTTP/1.1 402 Payment Required
content-type: application/json

{
  "x402Version": 1,
  "accepts": [
    {
      "scheme": "exact",
      "network": "stellar-testnet",
      "maxAmountRequired": "0.02",
      "payTo": "G...",
      "asset": { "code": "USDC", "issuer": "G..." },
      "resource": "/api/ocr"
    }
  ]
}

What happens on each call

  • No X-PAYMENT header → Tael replies 402 with the challenge above.
  • The agent signs a Stellar USDC payment and retries with the X-PAYMENT proof.
  • Tael verifies the payment through your verifier, runs your handler, and echoes an X-PAYMENT-RESPONSE receipt.

Next steps