Build x402 payment-enabled APIs on Cloudflare Workers in minutes
// In your Cloudflare Worker
import { createX402Tool } from 'https://searchx402.com/sdk/index.mjs';
// wrangler.toml: name = "my-x402-tool"
import { createX402Tool } from 'https://searchx402.com/sdk/index.mjs';
export default createX402Tool({
name: 'My AI Tool',
price: 0.05, // $0.05 USDC per request
wallet: '0x...your-wallet-address',
handler: async (request, env) => {
const { prompt } = await request.json();
// Your tool logic here
return { result: 'Generated content...' };
}
});
Automatic payment requirement responses following x402 protocol
Built-in receipt parsing and validation helpers
Cross-origin requests work out of the box
Full TypeScript definitions included
createX402Tool // Factory function to create x402 apps
X402Response // HTTP 402 response class
verifyPayment // Payment receipt verification
parsePaymentReceipt
encodePaymentReceipt
toUsdcUnits / fromUsdcUnits
jsonResponse / errorResponse
generateRequestId