Example: AI API Reseller
Resell access to the Anthropic Claude API. Clients pay per-request via x402 — pricing adjusts automatically based on which model they request.
Use case
Section titled “Use case”You hold an Anthropic API key and want to monetize it. Instead of managing API keys, subscriptions, or billing dashboards, you put tollbooth in front of the Anthropic API and charge per-request in USDC. Cheaper models cost less, expensive models cost more.
There are two ways to price this: flat per-request or token-based. You can pick whichever fits your business.
For production operations (model table updates, fine-tune mapping, safe rollouts, and metrics), see LLM Pricing Operations.
Option A: Flat per-request pricing
Section titled “Option A: Flat per-request pricing”Charge a fixed amount per API call, regardless of how many tokens the request uses.
gateway: port: 3000 discovery: true
wallets: base: "0xYourWalletAddress"
accepts: - asset: USDC network: base
upstreams: anthropic: url: "https://api.anthropic.com" headers: x-api-key: "${ANTHROPIC_API_KEY}" anthropic-version: "2023-06-01" timeout: 120
routes: "POST /v1/messages": upstream: anthropic path: "/v1/messages" match: - where: { body.model: "claude-haiku-*" } price: "$0.005" - where: { body.model: "claude-sonnet-*" } price: "$0.015" - where: { body.model: "claude-opus-*" } price: "$0.075" fallback: "$0.015"What’s going on
Section titled “What’s going on”- Single upstream pointing at
api.anthropic.comwith your API key injected via env var. - One route (
POST /v1/messages) mirrors the Anthropic Messages endpoint. - Match rules inspect
body.modelusing glob patterns to set per-model pricing — Haiku is cheap, Opus is premium. - Fallback catches any model that doesn’t match a rule (e.g. new models Anthropic releases).
Option B: Token-based pricing
Section titled “Option B: Token-based pricing”Price per-request based on which model is used. Set type: token-based and tollbooth reads the model field from the request body automatically — no match rules needed.
gateway: port: 3000 discovery: true
wallets: base: "0xYourWalletAddress"
accepts: - asset: USDC network: base
upstreams: anthropic: url: "https://api.anthropic.com" headers: x-api-key: "${ANTHROPIC_API_KEY}" anthropic-version: "2023-06-01" timeout: 120
routes: "POST /v1/messages": upstream: anthropic path: "/v1/messages" type: token-based models: claude-haiku-4-5-20251001: "$0.001" claude-sonnet-4-5-20250929: "$0.005" claude-opus-4-6: "$0.02" fallback: "$0.005"What’s going on
Section titled “What’s going on”type: token-basedtells tollbooth to extract themodelfrom the request body and look up a per-request price. (openai-compatibleis also accepted as an alias.)modelsmap sets a flat per-request price for each model. The price is charged once per API call, not per token.- Fallback applies a default per-request price for any model not explicitly listed.
- No match rules required — the gateway handles model detection automatically.
Which should you pick?
Section titled “Which should you pick?”| Flat per-request (match rules) | Token-based (model lookup) | |
|---|---|---|
| Simplicity | You write explicit match rules for each model family | Gateway auto-detects model and looks up price — no match rules |
| Flexibility | Glob patterns match model families (e.g. claude-sonnet-*) | Exact model names from a built-in table + your overrides |
| Maintenance | Update match rules when new models launch | Built-in table covers common models; add overrides as needed |
| Pricing | Both charge a flat per-request price | Both charge a flat per-request price |
Rule of thumb: use match rules when you want to price by model family with glob patterns. Use token-based when you want automatic per-model pricing without writing match rules.
Run it
Section titled “Run it”export ANTHROPIC_API_KEY="sk-ant-..."npx tollbooth startExpected flow
Section titled “Expected flow”Client Tollbooth Anthropic │ │ │ │ POST /v1/messages │ │ │ { model: "claude-sonnet-…"} │ │ │─────────────────────────────>│ │ │ │ match body.model → │ │ │ price: $0.015 │ │ 402 + payment instructions │ │ │<─────────────────────────────│ │ │ │ │ │ (sign $0.015 USDC payment) │ │ │ │ │ │ POST /v1/messages │ │ │ + payment-signature header │ │ │─────────────────────────────>│ │ │ │ verify + settle payment │ │ │ │ │ │ POST /v1/messages │ │ │──────────────────────────────>│ │ │ │ │ │ { content: "..." } │ │ │<──────────────────────────────│ │ 200 + Claude response │ │ │<─────────────────────────────│ │Try it with curl
Section titled “Try it with curl”First request — get the 402:
curl -s http://localhost:3000/v1/messages \ -X POST \ -H "Content-Type: application/json" \ -d '{"model":"claude-sonnet-4-20250514","max_tokens":256,"messages":[{"role":"user","content":"Hello"}]}'The response includes a 402 Payment Required status with payment instructions in the headers. An x402-compatible client (or wallet SDK) reads these instructions, signs the USDC payment, and resends the request with the payment proof attached.