x402 payment protocol implementation for Vara Network
bun i x402-varaAdd a paywall to a single API endpoint that charges 0.1 VARA per request
import { requirePayment } from 'x402-vara/express';
// Use the payment middleware
app.get('/api/pay/premium',
requirePayment({
price: "100000000000",
description: "Premium content access (0.10 VARA)",
network: "vara-testnet",
payTo: "your-vara-address",
}),
(req, res) => {
res.json({ message: "Access granted!" });
}
);To accept VFT tokens, specify the token program id via the optional asset field:
...
requirePayment({
price: "100000",
asset: "0xAssetProgramId...",
...
Currently the express middleware isn't as feature-complete as the Next.js implementation, but it doesn't require an external facilitator to work, which makes it great for development purposes.
If you need to define multiple payment options on a single route, it is recommended to use the Next.js middleware instead.
In Next.js 15, add the following exported const in /middleware.ts. In version 16, the file is renamed to /proxy.ts (see release notes)
import { paymentMiddleware } from "x402-vara/next";
// Configure protected routes and their payment requirements
export const middleware = paymentMiddleware(
process.env.PAYMENT_RECIPIENT_ADDRESS!,
{
"/api/protected/weather": [
{
price: "1000000",
asset: "0xAssetProgramId...",
network: "vara-testnet",
config: {
description: "Access to weather data API (pay in VFT token)",
extra: {
name: "WUSDC",
decimals: 6,
},
},
},
],
},
{
// Facilitator URL is REQUIRED for x402 protocol
url: process.env.FACILITATOR_URL!,
},
);You can directly use the public facilitator for testing purposes:
FACILITATOR_URL=https://x402-vara-next-facilitator.up.railway.app/api/facilitator
Or run your own instance by cloning varazone/x402-vara-next-facilitator
Note: the facilitator service connects to remote RPC nodes via WebSocket. However, Some serverless providers like Vercel do not support persistent WS connections. So make sure you deploy it on a platform with good support for WebSocket.
import { withX402Interceptor } from 'x402-vara/client';
import axios from 'axios';
// Create axios instance with x402 payment interceptor
const apiClient = withX402Interceptor(axios.create(), keypair);
// Make requests - payment will be handled automatically
const response = await apiClient.get('http://localhost:3001/api/pay/premium');- https://github.com/varazone/x402-vara-next-demo
- https://github.com/varazone/x402-vara-next-facilitator
- https://github.com/varazone/x402-vara-express-demo
See also: examples and scripts
requirePayment(options)- Express middleware for payment verificationfacilitatorRouter- Express router for payment facilitator endpointsdecodePaymentHeader(header)- Decode x402 payment headersuseApi(network)- Get Polkadot.js API instance for Vara network
withX402Interceptor(axiosInstance, walletClient)- Axios interceptor for automatic paymentscreateUnsignedTransaction(api, address, tx)- Create unsigned transactionsignWith(keypair, unsignedTransaction, api)- Sign transaction with wallet
MIT