Skip to content

gear-foundation/x402-vara

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

x402-vara

x402 payment protocol implementation for Vara Network

npm version License: MIT

Installation

bun i x402-vara

Usage

Server-side (Express)

Add 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.

Server-side (Next.js)

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!,
  },
);

Facilitator (optional)

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.

Client-side (Axios)

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');

Used by

See also: examples and scripts

API Reference

Server Exports

  • requirePayment(options) - Express middleware for payment verification
  • facilitatorRouter - Express router for payment facilitator endpoints
  • decodePaymentHeader(header) - Decode x402 payment headers
  • useApi(network) - Get Polkadot.js API instance for Vara network

Client Exports

  • withX402Interceptor(axiosInstance, walletClient) - Axios interceptor for automatic payments
  • createUnsignedTransaction(api, address, tx) - Create unsigned transaction
  • signWith(keypair, unsignedTransaction, api) - Sign transaction with wallet

License

MIT

About

x402 protocol implementation for Vara Network

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published