Quickstart

Build your first monetized API in 5 minutes.

Step 1: Create a New Project

terminal
mkdir my-monetized-api
cd my-monetized-api
npm init -y
npm install express @xapi-fi/sdk

Step 2: Create Your API

Create index.js:

terminal
const express = require('express');
const XAPIFi = require('@xapi-fi/sdk/server');

const app = express();
const xapi = new XAPIFi({
  apiKey: process.env.XAPI_FI_KEY
});

app.use(express.json());

app.post('/api/generate',
  xapi.protect({ price: 0.001 }),
  async (req, res) => {
    res.json({
      message: 'Success!',
      prompt: req.body.prompt
    });
  }
);

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Step 3: Run Your Server

terminal
XAPI_FI_KEY=your_key_here node index.js

What You Built

You've created a monetized API endpoint that:

  • Requires 0.001 USD payment per call
  • Uses the x402 payment protocol
  • Automatically verifies payments via the x-payment header
  • Works with any Web3 wallet

How Payments Work

When a client calls your endpoint:

  1. First request returns 402 Payment Required with payment details
  2. Client's wallet creates and signs a payment transaction
  3. Client retries request with x-payment header containing the payment proof
  4. Your endpoint verifies the payment and processes the request

Next Steps

  • Add more endpoints with different pricing
  • Implement dynamic pricing based on request parameters
  • Add analytics tracking to monitor revenue
  • Deploy to production