Skip to content

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.

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.

Charge a fixed amount per API call, regardless of how many tokens the request uses.

tollbooth.config.yaml
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"
  • Single upstream pointing at api.anthropic.com with your API key injected via env var.
  • One route (POST /v1/messages) mirrors the Anthropic Messages endpoint.
  • Match rules inspect body.model using 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).

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.

tollbooth.config.yaml
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"
  • type: token-based tells tollbooth to extract the model from the request body and look up a per-request price. (openai-compatible is also accepted as an alias.)
  • models map 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.
Flat per-request (match rules)Token-based (model lookup)
SimplicityYou write explicit match rules for each model familyGateway auto-detects model and looks up price — no match rules
FlexibilityGlob patterns match model families (e.g. claude-sonnet-*)Exact model names from a built-in table + your overrides
MaintenanceUpdate match rules when new models launchBuilt-in table covers common models; add overrides as needed
PricingBoth charge a flat per-request priceBoth 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.

Terminal window
export ANTHROPIC_API_KEY="sk-ant-..."
npx tollbooth start
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 │ │
│<─────────────────────────────│ │

First request — get the 402:

Terminal window
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.


Next: Video Streaming Paywall →