Skip to content

Prices in your code

Keep prices in your code if you already use x402 middleware or want them under version control. Cryptonly then only settles the payments, and you don’t use the Endpoints tab. Each route sets its own price and payTo, the address that gets paid.

First, follow steps 1 and 2 of the quickstart to add a payout wallet and create an API key. Set PAY_TO to that wallet’s address.

Terminal window
npm install @cryptonly/x402 @x402/core @x402/express @x402/evm express
import express from 'express';
import { CryptonlyFacilitatorClient } from '@cryptonly/x402';
import { ExactEvmScheme } from '@x402/evm/exact/server';
import { paymentMiddleware, x402ResourceServer } from '@x402/express';
const facilitator = new CryptonlyFacilitatorClient({
apiKey: process.env.CRYPTONLY_API_KEY!,
});
const server = new x402ResourceServer(facilitator).register(
'eip155:8453',
new ExactEvmScheme(),
);
const app = express();
app.use(
paymentMiddleware(
{
'GET /v1/weather/:city': {
accepts: [
{ scheme: 'exact', price: '$0.01', network: 'eip155:8453', payTo: process.env.PAY_TO! },
],
description: 'Current weather for a city',
},
},
server,
),
);
app.get('/v1/weather/:city', (req, res) => {
res.json({ city: req.params.city, temperatureC: 21 });
});
app.listen(4021);

CryptonlyFacilitatorClient works the same way with @x402/fastify, @x402/hono and @x402/next.

Test it with steps 5 and 6 of the quickstart. After its first payment, the route shows up on the Endpoints tab as Priced in code.

Register ExactSvmScheme from @x402/svm/exact/server for the Solana network ID and use your Solana wallet as payTo. The scheme gets Cryptonly’s fee payer from the facilitator, so there’s nothing else to configure. In NestJS, install @x402/svm instead: @X402() registers it for a Solana network.

Pass sandbox: true (Python: sandbox=True; Go: use https://sandbox-api-merchant.cryptonly.net/x402), change the network to eip155:84532 (Base Sepolia), and use a key and wallet from your sandbox account.