shopQuickstart for Merchants / Sellers

This guide walks you through integrating RailBridge x402 payments into your Express.js application to accept same-chain or cross-chain payments.

1. Prerequisites

  • Node.js 18+ and npm

  • An existing Express.js application (or create a new one)

  • Access to a hosted RailBridge facilitator service

  • A funded merchant wallet on your destination chain

2. Install x402 Packages

In your project, install the required x402 packages:

npm install @x402/express @x402/core @x402/evm @x402/paywall

3. Configure Environment Variables

Set these environment variables in your production environment:

  • FACILITATOR_URL - The hosted RailBridge facilitator service URL (e.g., https://facilitator.railbridge.io)

  • FACILITATOR_ADDRESS - The facilitator's address on the source chain (for cross-chain payments). Get this from the facilitator's /supported endpoint or from the facilitator service documentation.

  • MERCHANT_ADDRESS - Your merchant address on the destination chain

4. Basic Setup

Start by creating the core components:

import express from "express";
import { paymentMiddleware } from "@x402/express";
import { x402ResourceServer } from "@x402/core/server";
import { HTTPFacilitatorClient } from "@x402/core/http";
import { registerExactEvmScheme } from "@x402/evm/exact/server";
import { createPaywall } from "@x402/paywall";
import { evmPaywall } from "@x402/paywall/evm";

const FACILITATOR_URL = process.env.FACILITATOR_URL || "https://facilitator.railbridge.io";
const MERCHANT_ADDRESS = process.env.MERCHANT_ADDRESS as `0x${string}`;

// Create facilitator client
const facilitatorClient = new HTTPFacilitatorClient({ url: FACILITATOR_URL });

// Create resource server
const resourceServer = new x402ResourceServer(facilitatorClient);

// Register EVM scheme
registerExactEvmScheme(resourceServer, {
  networks: [
    "eip155:84532", // Base Sepolia
    "eip155:8453",  // Base Mainnet
    "eip155:1",     // Ethereum Mainnet
    "eip155:11155111", // Ethereum Sepolia
    "eip155:137",   // Polygon
  ],
});

// Create paywall for browser requests
const paywall = createPaywall()
  .withNetwork(evmPaywall)
  .withConfig({
    appName: "Your App Name",
    testnet: true, // Set to false for mainnet
  })
  .build();

const app = express();
app.use(express.json());

5. Same-Chain Payment Integration

For same-chain payments (user and merchant on the same chain):

6. Cross-Chain Payment Integration

For cross-chain payments (user pays on source chain, merchant receives on destination chain):

Note: For cross-chain payments, you'll need to copy the crossChain.ts extension file from this repository's src/extensions/ directory into your project, or install it as a package if available.

7. Multiple Payment Options

You can offer multiple payment options (different networks or prices):

Note: The cross-chain extension applies to all accepts entries. If you want some same-chain and some cross-chain options, define separate routes.

8. Accessing Payment Information

After a successful payment, you can access settlement details from response headers:

9. Error Handling

The payment middleware automatically handles:

  • 402 Payment Required: When no payment is provided

  • 400 Bad Request: When payment is invalid

  • 500 Internal Server Error: When facilitator is unreachable or settlement fails

You can add custom error handling:

10. Advanced: Logging and Monitoring

Add logging hooks to track payment flow:

11. Complete Example: Same-Chain

12. Complete Example: Cross-Chain

Note: Copy the crossChain.ts extension file from this repository's src/extensions/ directory into your project.

13. Testing Your Integration

  1. Test without payment: Make a request without payment headers - you should get 402 Payment Required

  2. Test with invalid payment: Send an invalid payment signature - you should get 400 Bad Request

  3. Test with valid payment: Use a client that supports x402 to make a payment - you should get 200 OK with your content

14. Common Issues and Solutions

Issue: Payment middleware not intercepting requests

  • Solution: Ensure paymentMiddleware is registered before your route handlers

Issue: 402 Payment Required even after payment

  • Solution: Check that FACILITATOR_URL is correct and the facilitator is reachable

Issue: Cross-chain payments not working

  • Solution: Verify FACILITATOR_ADDRESS is set correctly and matches the facilitator's address on the source chain

Issue: Wrong network in payment requirements

  • Solution: Ensure the networks in registerExactEvmScheme include all networks you want to support

Last updated