Skip to main content
This guide shows how to create paid APIs using the Faremeter middleware for SKALE Base Sepolia testnet. You’ll learn to add payment requirements to HTTP endpoints using the x402 protocol on the SKALE network.

Prerequisites

Ensure you have Node.js and npm installed on your machine and a funded wallet with your receiving address.

Step 1: Create a New Project

Initialize a new project using the official Hono template:
npm create hono@latest x402-faremeter-seller
Select the nodejs template when prompted. Then navigate to your project:
cd x402-faremeter-seller
npm install
Install the Faremeter middleware:
npm install @faremeter/middleware

Step 2: Configure Your Receiving Address

Create a .env file with your wallet address:
RECEIVING_ADDRESS=0x_your_wallet_address_here

Step 3: Create Your Paid API

Replace the contents of src/index.ts with a simple string reversal service:
import { Hono } from 'hono';
import { createMiddleware } from "@faremeter/middleware/hono";

const app = new Hono();
const RECEIVING_ADDRESS = process.env.RECEIVING_ADDRESS;

if (!RECEIVING_ADDRESS) {
  throw new Error("RECEIVING_ADDRESS environment variable is required");
}

// Free health check endpoint
app.get("/health", (c) => c.json({ status: "healthy" }));

// Free info endpoint
app.get("/info", (c) => c.json({
  name: "Simple String Reverser API (SKALE)",
  description: "Paid string reversal service on SKALE Base Sepolia testnet",
  endpoints: ["/tools/paid/reverse-string"]
}));

// Create payment middleware for SKALE Base Sepolia testnet
const skaleMiddleware = await createMiddleware({
  facilitatorURL: "https://facilitator.dirtroad.dev",
  accepts: [
    {
      scheme: "exact",
      network: "eip155:2140350733", // SKALE Base Sepolia
      maxAmountRequired: "10000", // 0.00001 AxiosUSD
      maxTimeoutSeconds: 5000,
      payTo: RECEIVING_ADDRESS,
      asset: "0x61a26022927096f444994dA1e53F0FD9487EAfcf", // Axios USD
      description: "string reversal service",
      mimeType: "application/json"
    }
  ]
});

// SKALE testnet paid endpoint
app.post("/tools/paid/reverse-string", skaleMiddleware, async (c) => {
  const { str } = await c.req.json();

  if (!str || typeof str !== 'string') {
    return c.json({ error: "str parameter is required and must be a string" }, 400);
  }

  const result = str.split('').reverse().join('');
  return c.json({
    original: str,
    result,
    network: "skale-base-sepolia"
  });
});

export default app;

Step 4: Start the Server

The Hono template creates a ready-to-run server. Start your server:
npm run dev
Expected output:
🚀 Simple String Reverser API running on http://localhost:3000
💰 Receiving payments at: 0x_your_wallet_address_here

Step 5: Test Your Paid Endpoints

Test the free endpoints first:
curl http://localhost:3000/info
Expected output:
{
  "name": "Simple String Reverser API (SKALE)",
  "description": "Paid string reversal service on SKALE Base Sepolia testnet",
  "endpoints": ["/tools/paid/reverse-string"]
}
Test the paid endpoint (this will return 402 Payment Required):
curl -X POST http://localhost:3000/tools/paid/reverse-string \
  -H "Content-Type: application/json" \
  -d '{"str": "Hello World"}'
Expected output:
{
  "error": "402 Payment Required",
  "payment": {
    "scheme": "exact",
    "network": "eip155:2140350733",
    "maxAmountRequired": "10000",
    "maxTimeoutSeconds": 5000,
    "payTo": "0x_your_wallet_address_here",
    "asset": "0x61a26022927096f444994dA1e53F0FD9487EAfcf",
    "description": "string reversal service",
    "mimeType": "application/json"
  }
}

How It Works

This tutorial creates a simple string reversal API for SKALE Base Sepolia testnet:
  1. SKALE Route (/tools/paid/reverse-string): Uses AxiosUSD on SKALE testnet via facilitator.dirtroad.dev
  2. Payment Flow: Clients pay with AxiosUSD tokens, facilitator processes the payment gaslessly
  3. Service Access: Once payment is confirmed, the API processes the string reversal request

Middleware Configuration

The middleware is configured for SKALE Base Sepolia testnet:
  • Network: SKALE Base Sepolia (Chain ID: 2140350733)
  • Token: AxiosUSD at 0x61a26022927096f444994dA1e53F0FD9487EAfcf
  • Facilitator: facilitator.dirtroad.dev
  • Gasless: EIP-3009 forwarder covers all gas costs

Configuration Options

The API is configured for SKALE Base Sepolia testnet:

SKALE Configuration

  • Network: SKALE Base Sepolia (Chain ID: 2140350733)
  • Token: AxiosUSD at 0x61a26022927096f444994dA1e53F0FD9487EAfcf
  • Facilitator: facilitator.dirtroad.dev
  • Price: 10,000 wei (0.00001 AxiosUSD)
  • Gasless: EIP-3009 forwarder covers gas fees

Error Handling

Common issues and solutions:
  1. “RECEIVING_ADDRESS not configured”
    • Ensure your .env file contains the RECEIVING_ADDRESS variable
    • Restart your server after adding the environment variable
  2. “Facilitator connection failed”
    • Check that the facilitator URL is accessible
    • Verify you’re using the correct facilitator for each network
  3. “Payment validation failed”
    • Ensure the receiving address matches your wallet
    • Verify token contracts are correct for each network
  4. “Invalid string parameter”
    • Ensure you’re sending a JSON object with a “str” field
    • The str field must be a string value

Next Steps

Once your SKALE string reversal API is running: For more advanced features, see the Faremeter documentation and x402 specification.