|
| 1 | +# Quickstart for Sellers |
| 2 | + |
| 3 | +This guide walks you through integrating with x402 |
| 4 | +to enable payments for your API or service. By the end, |
| 5 | +your API will be able to charge buyers and AI agents for access. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +Before you begin, ensure you have: |
| 10 | + |
| 11 | +* A crypto wallet to receive funds (any EVM-compatible wallet) |
| 12 | +* [Node.js](https://nodejs.org/en) and npm (or Python and pip) installed |
| 13 | +* An existing API or server |
| 14 | + |
| 15 | +**Note** |
| 16 | + |
| 17 | +There are pre-configured examples available in the Coinbase repo for |
| 18 | + [Node.js](https://github.com/coinbase/x402/tree/main/examples/typescript/servers). |
| 19 | +There is also an [advanced example](https://github.com/coinbase/x402/tree/main/examples/typescript/servers/advanced) |
| 20 | +that shows how to use the x402 SDKs to build a more complex payment flow. |
| 21 | +The following example shows how to adapt these for Polygon. |
| 22 | + |
| 23 | +## 1. Install Dependencies |
| 24 | + |
| 25 | +Install the [x402 Express middleware package](https://www.npmjs.com/package/x402-express). |
| 26 | + |
| 27 | +```bash |
| 28 | +npm install x402-express |
| 29 | +``` |
| 30 | + |
| 31 | +Install the [x402 Next.js middleware package](https://www.npmjs.com/package/x402-next). |
| 32 | + |
| 33 | +```bash |
| 34 | +npm install x402-next |
| 35 | +``` |
| 36 | + |
| 37 | +```bash |
| 38 | +npm install x402-hono |
| 39 | +``` |
| 40 | + |
| 41 | +### 2. Add Payment Middleware |
| 42 | + |
| 43 | +Integrate the payment middleware into your application. You will need |
| 44 | +to provide: |
| 45 | + |
| 46 | +* The Facilitator URL or facilitator object. For testing, |
| 47 | +use `https://facilitator.x402.rs/` which works on Polygon Mainnet and Amoy. |
| 48 | +* For a facilitator access on Polygon, please contact us [here.](https://t.me/PolygonHQ/32) |
| 49 | +* The routes you want to protect. |
| 50 | +* Your receiving wallet address. |
| 51 | + |
| 52 | + |
| 53 | +Full example in the repo [here](https://github.com/AkshatGada/x402_Polygon/tree/feature/facilitator-amoy/demo/quickstart-local). |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +```javascript |
| 58 | +import express from "express"; |
| 59 | +import { paymentMiddleware } from "x402-express"; |
| 60 | + |
| 61 | +const app = express(); |
| 62 | + |
| 63 | +app.use(paymentMiddleware( |
| 64 | + "0xCA3953e536bDA86D1F152eEfA8aC7b0C82b6eC00", // receiving wallet address |
| 65 | + { // Route configurations for protected endpoints |
| 66 | + "GET /weather": { |
| 67 | + // USDC amount in dollars |
| 68 | + price: "$0.001", |
| 69 | + network: "polygon-amoy", |
| 70 | + // Optional: Add metadata for better discovery in x402 Bazaar |
| 71 | + config: { |
| 72 | + description: "Get current weather data for any location", |
| 73 | + inputSchema: { |
| 74 | + type: "object", |
| 75 | + properties: { |
| 76 | + location: { type: "string", description: "City name" } |
| 77 | + } |
| 78 | + }, |
| 79 | + outputSchema: { |
| 80 | + type: "object", |
| 81 | + properties: { |
| 82 | + weather: { type: "string" }, |
| 83 | + temperature: { type: "number" } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + url: process.env.FACILITATOR_URL || "https://facilitator.x402.rs", // Polygon Amoy facilitator |
| 91 | + } |
| 92 | +)); |
| 93 | + |
| 94 | +// Implement your route |
| 95 | +app.get("/weather", (req, res) => { |
| 96 | + res.send({ |
| 97 | + report: { |
| 98 | + weather: "sunny", |
| 99 | + temperature: 70, |
| 100 | + }, |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +app.listen(4021, () => { |
| 105 | + console.log(`Server listening at http://localhost:4021`); |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +Full example in the repo [here](https://github.com/AkshatGada/x402_Polygon/tree/feature/facilitator-amoy/demo/quickstart-local). |
| 110 | +Since this is a fullstack example, we recommend using the example to build |
| 111 | +this yourself, and treat the code snippet below as a reference to adapt for |
| 112 | +Polygon. |
| 113 | + |
| 114 | +```javascript |
| 115 | +import { paymentMiddleware, Network } from 'x402-next'; |
| 116 | + |
| 117 | +// Configure the payment middleware |
| 118 | +export const middleware = paymentMiddleware( |
| 119 | + "0xYourAddress", // your receiving wallet address |
| 120 | + { // Route configurations for protected endpoints |
| 121 | + '/protected': { |
| 122 | + price: '$0.01', |
| 123 | + network: "polygon-amoy", |
| 124 | + config: { |
| 125 | + description: 'Access to protected content' |
| 126 | + } |
| 127 | + }, |
| 128 | + } |
| 129 | + { |
| 130 | + url: "https://facilitator.x402.rs/", // Facilitator URL for Polygon testnet and mainnet |
| 131 | + } |
| 132 | +); |
| 133 | + |
| 134 | +// Configure which paths the middleware should run on |
| 135 | +export const config = { |
| 136 | + matcher: [ |
| 137 | + '/protected/:path*', |
| 138 | + ] |
| 139 | +}; |
| 140 | +``` |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +```javascript |
| 145 | +import { Hono } from "hono"; |
| 146 | +import { paymentMiddleware, Network } from "x402-hono"; |
| 147 | + |
| 148 | +const app = new Hono(); |
| 149 | + |
| 150 | +// Configure the payment middleware |
| 151 | +app.use(paymentMiddleware( |
| 152 | + "0xYourAddress", |
| 153 | + { |
| 154 | + "/protected-route": { |
| 155 | + price: "$0.01", |
| 156 | + network: "polygon-amoy", |
| 157 | + config: { |
| 158 | + description: "Access to premium content", |
| 159 | + } |
| 160 | + } |
| 161 | + }, |
| 162 | + { |
| 163 | + url: 'https://facilitator.x402.rs' // 👈 Facilitator URL |
| 164 | + } |
| 165 | +)); |
| 166 | + |
| 167 | +// Implement your route |
| 168 | +app.get("/protected-route", (c) => { |
| 169 | + return c.json({ message: "This content is behind a paywall" }); |
| 170 | +}); |
| 171 | + |
| 172 | +serve({ |
| 173 | + fetch: app.fetch, |
| 174 | + port: 3000 |
| 175 | +}); |
| 176 | +``` |
0 commit comments