x402-starter

x402-starter

Enables AI agents to pay-per-call for Cloudflare Worker APIs using USDC micropayments, with automatic MCP tool discovery and Bazaar listing.

Category
访问服务器

README

x402-starter

Paywall your Cloudflare Worker for AI agents in 5 minutes.

Drop-in x402 starter. Agents pay per call in USDC — no signup, no API keys, no human in the loop. Comes with Coinbase x402 Bazaar discoverability and an MCP manifest pre-wired so Coinbase-connected agents can find your API automatically.

What you get out of the box

  • A Cloudflare Worker that returns HTTP 402 Payment Required for paid routes until the caller settles a USDC micropayment
  • Bazaar-discoverable routes (the non-obvious bit — without discoverable: true + schemas, even settled payments never list)
  • A /mcp manifest so MCP clients and Coinbase AgentKit auto-discover your tools
  • One example paid endpoint (GET /api/echo) you replace with your real API
  • HTTPS-forced redirects, JSON-or-HTML root discovery, health check

5-minute quick start

git clone <this repo> my-paid-api
cd my-paid-api
npm install
npm run dev          # paywall OFF by default — iterate locally

Hit http://localhost:8787/api/echo?msg=hi — you'll get a 200 with the echo. That's your free dev loop.

Flip the paywall on (testnet)

  1. Edit wrangler.jsonc:
    "X402_ENABLED": "true",
    "X402_NETWORK": "base-sepolia",
    "PAY_TO": "0xYOUR_TESTNET_WALLET",
    "PRICE_PER_CALL": "$0.01"
    
  2. npm run deploy
  3. Hit /api/echo — you'll get HTTP 402 with the payment challenge.

base-sepolia uses the default public facilitator. No keys needed. This is the right setup for development and demos.

Flip to mainnet (real USDC)

  1. Get a Coinbase CDP Secret API key at portal.cdp.coinbase.com.
  2. Set the two secrets:
    npx wrangler secret put CDP_API_KEY_ID      # paste the key id when prompted
    npx wrangler secret put CDP_API_KEY_SECRET  # paste the secret when prompted
    
  3. Set X402_NETWORK to "base" in wrangler.jsonc, set PAY_TO to your real Base wallet, and redeploy.

Without the CDP keys on mainnet, the 402 challenge still issues but payments cannot settle. Set both secrets before going live.

Adding your own paid endpoints

Two places to edit src/index.ts:

  1. The handler — add a route under /api/*:

    app.get("/api/wash", async (c) => {
      // your logic
      return c.json({ ... });
    });
    
  2. The paywall config — add the same path to routes in applyPaywall() so it gets paywalled AND Bazaar-discoverable:

    "/api/wash": {
      price: env.PRICE_PER_CALL,
      network: env.X402_NETWORK,
      config: {
        discoverable: true,
        description: "Wash/sybil screen for any Base address",
        inputSchema: { queryParams: { address: { type: "string" } } },
        outputSchema: { type: "object", properties: { sybil_score: { type: "number" } } },
      },
    },
    
  3. The MCP manifest — add a tools[] entry with the same name/endpoint/price_usd so MCP clients see it.

That's it. The middleware path matcher (/api/*) catches everything under /api, so only step 1 is structurally required — but skipping steps 2 and 3 means agents won't find your endpoint, which is most of the value.

Going beyond echo

This template is intentionally minimal — one example endpoint, no business logic. The patterns it shows (Bazaar config, MCP manifest, free landing + JSON discovery, HTTPS forcing, lazy app build) are the load-bearing parts. Wire in your D1 / KV / Workers AI / Durable Objects as needed.

Pricing

x402-hono accepts dollar-formatted strings: "$0.01", "$0.05", "$1.00". Internally x402 settles in micro-USDC (six decimals), so "$0.01" becomes 10000 units. Different routes can charge different prices — set them per-route in the routes config.

License

MIT. Use it, fork it, ship it.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选