Skip to main content

x402 — Pay-Per-Request for AI Agents

Call any model and pay per request in USDC — no account, no API key. Your wallet is the identity, over the open x402 payment protocol. A free testnet sandbox is live now.

POST https://x402.tokligence.ai/v1/chat/completions

OpenAI-compatible request body. No Authorization header — you pay per call instead. Prefer a normal API key? Use the standard API.

How it works

Every request runs a short pay-then-serve loop — your client sends the request twice: once to get a price, once with payment attached.

  1. Send your request (OpenAI chat body). With no payment you get 402 Payment Required and a price quote in accepts — a USDC ceiling for this exact request.
  2. Sign a payment authorizing up to that ceiling (a Permit2 signature).
  3. Resend the same request, byte-for-byte, with an X-PAYMENT header carrying the signature.
  4. Get 200 + the completion. You are charged the actual usage (always ≤ the ceiling); it settles on-chain and our facilitator pays the gas — so you never need gas beyond the one-time setup below.

Try it free — testnet sandbox

Live now on Base Sepolia. The payment loop is fully real (real signature, real on-chain settle), but the model response is a fixed placeholder — so it costs nothing and you are testing your payment integration, not model output.

  1. Fund a wallet you control on Base: test USDC from faucet.circle.com (select Base Sepolia), plus a little Base Sepolia ETH for the one-time approval below.

  2. Approve Permit2 once so it can move your USDC at settle time (one transaction, then never again):

    token          : 0x036CbD53842c5426634e7929541eC2318f3dCF7e   (USDC, Base Sepolia)
    call : approve(spender, amount)
    spender (arg) : 0x000000000022D473030F116dDEE9F6B43aC78BA3 (the Permit2 contract)
    amount : max uint256

    This approves the Permit2 contract on your USDC. It is separate from the per-payment signature below, whose Spender field names the x402 proxy — you approve Permit2 once here, then Permit2 pulls funds via each signed permit.

  3. Run the loop against the endpoint — get the 402 quote, sign it, resend, receive a 200.

Sandbox → mainnet is a config switch

The payment contract you integrate against is identical. Going live means the same code against Base mainnet with real USDC and real model responses.

The 402 response — your quote

An unpaid request returns the price to sign. accepts[0] is what you pay under:

HTTP/1.1 402 Payment Required
{
"x402Version": 2,
"accepts": [{
"scheme": "upto", // authorize a ceiling, pay actual usage
"network": "eip155:84532", // Base Sepolia (sandbox); eip155:8453 = mainnet
"amount": "43", // the ceiling, USDC min units (6 decimals, string)
"asset": "0x036CbD…dCF7e", // USDC
"payTo": "0x…", // where the payment lands
"maxTimeoutSeconds": 600, // how long this quote is valid
"extra": { "facilitatorAddress": "0x…" }
}]
}

Paying — the X-PAYMENT header

Sign a Permit2 PermitWitnessTransferFrom for the quoted ceiling, then resend the byte-identical body with the signature base64-encoded in an X-PAYMENT header. Signing essentials:

FieldValue
Schemeupto — you authorize the quoted amount (the ceiling); the server settles the real usage-based amount ≤ that
Permit2 contract0x000000000022D473030F116dDEE9F6B43aC78BA3 (same on every EVM chain)
EIP-712 domain{ name: "Permit2", chainId, verifyingContract: <Permit2> } — no version field
Spender0x4020A4f3b7b90ccA423B9fabCc0CE57C6C240002 (x402 upto proxy, fixed on every EVM chain)
Witness{ to: payTo, facilitator: extra.facilitatorAddress, validAfter }
X-PAYMENT bodybase64 JSON with a top-level accepted object (the scheme/network you pay under) alongside the signed payload
Two rules that trip people up
  1. The paid request body must be byte-identical to the one you quoted — the quote is bound to a hash of the raw bytes; re-serializing (key order, whitespace) invalidates it.
  2. The accepted object sits at the top level of the X-PAYMENT JSON, next to payload — not inside it.

The x402 protocol spec and reference client tooling live at github.com/coinbase/x402.

Discovering models & prices

GET https://x402.tokligence.ai/v1/models

Lists every payable model with live per-million-token prices, context length, and max output. The 402 quote for a given request is the authoritative price for that exact call.

Errors

StatusMeaning & what to do
402Payment required (or invalid/expired). Sign accepts[0] and resend the same body with X-PAYMENT.
400Malformed body, a non-payable model, or stream: true (not supported on this rail). Fix the request.
403The quote exceeds the per-request cap, or the payer address is blocked. Lower max_tokens, or stop.
429Rate limited. Back off and retry.
503Model not currently payable, or the rail is momentarily at capacity. Retry later.