Accept agent payments

Let AI agents pay for your API. Add middleware — agents pay per request, you collect everything in one transaction.

Install

npm install @spx/sdk

Add payment middleware

import express from "express";
import { spxMiddleware } from "@spx/sdk/server";

const app = express();

app.use("/api", spxMiddleware({
  onPayment: (payment, amount) => {
    console.log(`Received $${amount} payment`);
  },
}));

app.get("/api/data", (req, res) => {
  res.json({ result: "your content here" });
});

Requests without a valid payment get 402 Payment Required. Payment verification is local — no blockchain call, no external API, microseconds.

How verification works

When an agent sends a request with a payment header:

  1. The middleware extracts the 110-byte authorization and signature
  2. Verifies the Ed25519 signature against the known agent key
  3. Confirms the payment is addressed to your service
  4. Confirms the amount is correct and increasing
  5. Stores the latest authorization

All of this happens locally. No network call. The agent gets a response in the same HTTP roundtrip.

Collect your payments

When you're ready to cash out, submit one transaction — no matter how many payments you received.

// 4,821 API calls from agents -> 1 transaction -> $149.25
const payments = await db.getLatestPayments();
await spx.collect(payments);

Collection strategies

Batch (recommended) — collect hourly, daily, or at a dollar threshold. Minimizes blockchain fees.

Immediate — collect after every payment. Best when you need instant liquidity.

You always submit only the latest authorization per agent. Earlier ones are automatically superseded.

What you earn

Collection pays the difference between the total owed and what you've already collected, minus a 0.5% protocol fee.

owed           = $100.00
already_collected = $60.00
this_collection  = $40.00
fee (0.5%)       = $0.20
you_receive      = $39.80

Why agents will pay you

  • Zero friction — agents pay with a single HTTP header. No account creation, no API key, no billing setup.
  • Guaranteed funds — the agent's funding source is pre-deposited on-chain. The money is there before the agent makes a single call.
  • No chargebacks — cryptographic authorization is unforgeable. Once the agent authorizes payment, it can't be reversed.
  • Instant verification — you verify the payment and serve the response in the same request. No waiting for confirmation.

Next steps