← Back to Blog
TutorialMarch 2025NEW

Build a Paid API in 5 Minutes

Turn any API endpoint into a revenue stream. Accept USDC payments from AI agents and apps with zero infrastructure.

What You'll Build

By the end of this tutorial, you'll have a live API endpoint that:

  • Returns HTTP 402 with payment requirements when accessed without payment
  • Verifies on-chain USDC payments automatically
  • Serves premium content after payment is confirmed
  • Works with any AI agent framework (LangChain, CrewAI, etc.)
  • Accepts payments from 8 blockchain networks

Prerequisites

  • Node.js 18+ installed
  • A Solana wallet address (to receive payments)
  • Basic knowledge of Express.js or Next.js
1

Create a Next.js Project

npx create-next-app@latest my-paid-api --typescript --app
cd my-paid-api

Or use Express if you prefer — the SDK supports both.

2

Install the SDK

npm install nory-x402-client
3

Create Your Paid Endpoint

Create app/api/premium/route.ts:

import { NextRequest, NextResponse } from 'next/server';
import { Nory } from 'nory-x402-client';

// Initialize with your wallet address
const nory = new Nory({
  merchantWallet: process.env.MERCHANT_WALLET!,
  network: 'solana-mainnet',
});

export async function GET(request: NextRequest) {
  const paymentProof = request.headers.get('X-Payment');

  // No payment? Return 402 with requirements
  if (!paymentProof) {
    const requirement = nory.createRequirement({
      amount: '0.01',        // $0.01 USDC per request
      resource: '/api/premium',
      description: 'Premium market data',
    });

    return NextResponse.json(requirement, {
      status: 402,
      headers: {
        'X-Payment': Buffer.from(
          JSON.stringify(requirement)
        ).toString('base64'),
      },
    });
  }

  // Verify the payment on-chain
  const result = await nory.verifyAndSettle(paymentProof);

  if (!result.success) {
    return NextResponse.json(
      { error: 'Payment verification failed' },
      { status: 402 }
    );
  }

  // Payment verified! Return premium content
  return NextResponse.json({
    data: {
      market: 'bullish',
      insight: 'SOL breaking resistance at $180',
      confidence: 0.85,
      generatedAt: new Date().toISOString(),
    },
    payment: {
      txId: result.transactionId,
      settledAt: result.settledAt,
    },
  });
}

That's the entire endpoint. The SDK handles payment requirement formatting, multi-chain support, and on-chain verification.

4

Set Your Wallet Address

Add your Solana wallet address to .env.local:

MERCHANT_WALLET=YourSolanaWalletAddressHere

This is the wallet where payments will be sent. You can use any Solana wallet — Phantom, Solflare, etc.

5

Test It

npm run dev

Then hit your endpoint:

curl http://localhost:3000/api/premium

You'll get a 402 response with payment requirements:

{
  "x402Version": 2,
  "accepts": [
    {
      "network": "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
      "amount": "10000",
      "currency": "USDC",
      "payTo": "YourWalletAddress..."
    }
  ],
  "resource": "/api/premium",
  "description": "Premium market data"
}

How the x402 Flow Works

402

1. Agent requests your API

No X-Payment header → your endpoint returns HTTP 402 with payment requirements (amount, wallet, supported networks).

$

2. Agent creates a USDC payment

The agent reads the requirements, creates a USDC transfer to your wallet, and submits it to Nory for settlement.

3. Nory verifies on-chain (~400ms)

The payment is verified and settled on Solana in under 400ms. The transaction signature becomes the payment proof.

200

4. Agent retries with proof

The agent retries the request with X-Payment: tx_signature. Your endpoint verifies it and returns the premium data.

Express.js Version

Prefer Express? The SDK includes a one-line middleware:

import express from 'express';
import { Nory, noryMiddleware } from 'nory-x402-client';

const app = express();
const nory = new Nory({
  merchantWallet: process.env.MERCHANT_WALLET!,
  network: 'solana-mainnet',
});

// One line = paid endpoint
app.use('/api/premium', noryMiddleware(nory, {
  amount: '0.01',
  description: 'Premium market data',
}));

app.get('/api/premium', (req, res) => {
  res.json({
    data: { market: 'bullish', confidence: 0.85 },
    payment: req.payment,
  });
});

app.listen(3000);
6

Deploy to Vercel

npx vercel --prod

Set MERCHANT_WALLET in your Vercel environment variables, and your paid API is live. AI agents anywhere in the world can now pay for your data.

Revenue Potential

x402 micropayments unlock revenue from traffic that was previously unprofitable:

$0.01

per API call

1,000 calls/day = $300/mo

$0.05

per lead enrichment

500 leads/day = $750/mo

$0.10

per AI analysis

200 calls/day = $600/mo

Nory charges 0.1% per transaction — you keep 99.9% of revenue.

Works With Every AI Framework

Because x402 is built on standard HTTP, any AI agent that can make HTTP requests can pay for your API:

LangChain
CrewAI
AutoGen
Claude MCP
OpenAI Functions
Daydreams
Custom Agents
Any HTTP Client

Start Earning

Your API is valuable. Let AI agents pay for it.