Get started

Quickstart

Stand up an agents402 publisher and a paying agent in five minutes. The reference implementation uses Next.js, Nostr Wallet Connect, and a Model Context Protocol server.


1. Install the publisher middleware

terminal
bash
npm install @agents402/server-next @getalby/sdk zod

The reference Next.js middleware exposes the manifest, signs receipts with an ed25519 service key, and ships an L402 challenge wrapper for any route handler.

2. Add the manifest

Drop a route at app/.well-known/agents402.json/route.ts:

app/.well-known/agents402.json/route.ts
ts
import { NextResponse } from "next/server";
import { listActions, getServiceKeys } from "@agents402/server-next";

export const dynamic = "force-dynamic";

export function GET() {
  return NextResponse.json({
    version: "0.1",
    service: {
      name:     "Example Pub",
      homepage: process.env.PUBLIC_URL,
    },
    actions:  listActions(),
    receipts: {
      pubkey_hex: getServiceKeys().publicKey,
      algorithm:  "ed25519",
    },
  });
}

3. Wrap an action

app/api/actions/extract.structured/route.ts
ts
import { withL402 } from "@agents402/server-next";

export const POST = withL402(
  {
    id:          "extract.structured",
    type:        "structured_data",
    price_msats: 1000,
    risk:        "low",
    input: {
      doc_id: { type: "string", maxLength: 256 },
    },
  },
  async ({ input }) => {
    const doc = await loadDoc(input.doc_id);
    return { title: doc.title, summary: doc.summary };
  },
);

The wrapper handles 402 challenge issuance, token signing, idempotency, and receipt creation. Your handler runs only after payment is confirmed.

4. Wire up a Lightning wallet

The publisher needs a Lightning wallet that can issue invoices and confirm settlement. Use any NIP-47 NWC-compatible wallet (Coinos, Alby Hub, LNbits, Primal, …):

.env.local
dotenv
PUBLISHER_NWC_URL=nostr+walletconnect://…
L402_SECRET=any-random-string
No node operations
agents402 verification is purely cryptographic ( sha256(preimage) == payment_hash). The publisher can confirm a payment without running its own Lightning node in the request path — any NWC-compatible wallet provides the few operations needed.

5. Talk to the agent side

The reference agent integration is a Model Context Protocol (MCP) server that gives Claude (or any MCP-aware client) three tools:

  • ·discover — fetches a manifest, surfaces actions and prices
  • ·pay_and_invoke — pays the L402 challenge under deterministic policy and runs the action
  • ·spend_summary— reports today's spend and remaining budget

Add the published reference server to any MCP-aware client:

Cursorone click
Add to Cursor
Opens Cursor and prompts you to confirm. Uses the documented cursor:// deeplink.
install →
show config + manual install paths
{
  "mcpServers": {
    "faregate": {
      "command": "npx",
      "args": [
        "-y",
        "@agents402/mcp"
      ]
    }
  }
}
Claude Desktop · macOS
~/Library/Application Support/Claude/claude_desktop_config.json
Claude Desktop · Windows
%APPDATA%\Claude\claude_desktop_config.json

Already have mcpServers? Merge the faregatekey in alongside your other servers — don't overwrite the whole file.

That's it — no wallet pre-configuration. The first time the agent hits a paywall, the MCP returns a structured needs_setup response and walks the user through pairing a wallet (paste an existing NWC URI, or open a browser to create a self-custodial Spark wallet).

Verify with a real payment

terminal
bash
npx agents402 test https://your-publisher.example

The conformance harness fetches the manifest, triggers a 402, pays from the configured agent wallet, retries with proof, and validates the signed receipt. Exit code 0 = end-to-end mainnet flow verified.

Next
The manifest
The single JSON file that defines an agents402 publisher.
agents402.org / 2026
Open protocol · v0.1