Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DeepSeek-on-x402

A pay-per-call LLM API. Users pay 0.01 USDC on Base per request (x402 exact scheme, facilitated by Coinbase) and receive a DeepSeek v4 Flash response with adaptive thinking. Payments settle gaslessly into the CDP server wallet (feeRecipient): 0xD5EA89421bD1a23E4Fca0139802507C3052142c9. Note: the recipient cannot be the payer's own wallet (self_send_not_allowed).

Upstream cost is tiny: $0.14 / 1M input tokens, $0.28 / 1M output tokens (incl. reasoning) — a typical request costs ≈ $0.00005. max_tokens caps the whole completion (reasoning + answer), so worst case ≈ $0.0073/request, still a ~27% margin at the 0.01 USDC price.

Built with Hono + Cloudflare Workers + @coinbase/x402 + @x402/core.

How it works

client ── GET /service/chat?prompt=… ──▶ API
        ◀─ HTTP 402 + `payment-required: <base64(PaymentRequired v2)>` ──
client ── pays 0.01 USDC on Base to feeRecipient (via any x402 wallet) ──▶ chain
client ── GET /service/chat + `x-payment: <base64(PaymentPayload)>` ──▶ API
        API ── POST api.cdp.coinbase.com/platform/v2/x402/verify (JWT auth) ──▶ CDP
        ◀─ isValid=true ──
        API ── POST api.cdp.coinbase.com/platform/v2/x402/settle (JWT auth) ──▶ CDP
        ◀─ success + tx hash ── API calls DeepSeek, returns answer + `payment-response`

Settlement moves the USDC from payer → feeRecipient gaslessly (the payer never needs ETH): the payment-required header advertises the eip2612GasSponsoring extension at the top level, so the wallet also signs an EIP-2612 permit that the facilitator uses to approve Permit2 and settle in one atomic transaction.

Replay protection (optional): with a KV namespace bound to PAID, each payment signature/transaction can only be redeemed once.

Endpoints

Method Path Description
GET / Service info + payment instructions
GET/POST /service/chat?prompt=… or { "prompt": … } Paid LLM call
GET /.well-known/llms.txt Plain-text service description for LLM crawlers (llmstxt.org)
GET /.well-known/ai-plugin.json AI-plugin manifest (links the OpenAPI spec)
GET /openapi.json OpenAPI 3.0 spec for programmatic discovery

These discovery endpoints make the API findable by AI agents, bots and LLM crawlers so they can call it (and pay).

Local development

npm install
# Secrets for local dev (never commit):
cat > .dev.vars <<EOF
CDP_API_KEY_ID=…
CDP_API_KEY_SECRET=…
DEEPSEEK_API_KEY=…
EOF
npm run dev          # http://localhost:8787

Smoke checks:

# 1. Paywall responds with the payment-required header:
curl -si 'http://localhost:8787/service/chat?prompt=hi'

# 2. Agentic Wallet discovers the requirements:
awal x402 details http://localhost:8787/service/chat

# 3. Fund the Agentic Wallet with USDC on Base, then pay + call:
awal x402 pay 'http://localhost:8787/service/chat?prompt=hello'

Deploy to Cloudflare

npm run deploy                        # requires `wrangler login` or CLOUDFLARE_API_TOKEN
npx wrangler secret put CDP_API_KEY_ID
npx wrangler secret put CDP_API_KEY_SECRET
npx wrangler secret put DEEPSEEK_API_KEY
npx wrangler kv namespace create PAID_SIGNATURES   # optional replay protection
# then paste the returned id into wrangler.toml and redeploy

Deployment is free (Workers free tier). Add a custom domain with: npx wrangler domains add <your-domain>.

Client usage (any x402 wallet)

# Coinbase Agentic Wallet (awal):
awal x402 details <service-url>
awal x402 pay <service-url>

# Any other x402 client (@x402/core, @coinbase/x402) works too.

Configuration

All knobs live in src/config.ts:

Constant Default Meaning
FEE_RECIPIENT CDP wallet 0xD5EA…c9 (Base) Where USDC lands (must ≠ payer's wallet; CDP rejects self-sends)
USDC_BASE 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 USDC on Base
NETWORK eip155:8453 Base mainnet
PRICE_USD_ATOMIC 10000 Price in USDC atomic units (6 decimals; 0.01 USDC)
MAX_TIMEOUT_SECONDS 300 Payment validity window
MAX_PROMPT_CHARS 50000 Max prompt length (rejected with 400 beyond this)
MAX_OUTPUT_TOKENS 1024 Max completion tokens (max_tokens; reasoning counts toward this)
DEEPSEEK_MODEL deepseek-v4-flash DeepSeek model
DEEPSEEK_THINKING { type: "adaptive" } v4 thinking mode (adaptive/enabled/disabled)

About

Pay-per-call DeepSeek v4 Flash LLM API on Base via x402 (private)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages