paygate
v7.0.1
Published
Accept payment from AI agents in two lines of code. Gate any Express or Fastify route. Agents pay in fiat (EUR, GBP, USD) through ArisPay.
Downloads
561
Maintainers
Readme
paygate
Accept payment from AI agents in two lines of code. paygate gates an
Express or Fastify route. A request without a payment gets
402 Payment Required. An agent pays in fiat (EUR, GBP, USD) from its
ArisPay funding account, and ArisPay settles the payment before your handler
runs.
You need a merchant account at https://paygate.arispay.app. It gives you a
merchantId.
Quick start
npx paygate init --framework express --merchant-id <your-merchant-id> \
--route /api/data --price 10 --yes
cd paygate-express-seller
npm install && cp .env.example .env && npm start
curl -i localhost:3000/api/data # HTTP 402 with the pricepaygate init writes a small project: a /health route, one paid route, a
.env.example, and a README.
| Flag | Meaning |
|---|---|
| --framework | express or fastify |
| --merchant-id | Your merchant id |
| --route | Paid route path (default /api/paid) |
| --method | GET, POST, PUT or DELETE (default GET) |
| --price | Price in integer cents (default 1) |
| --currency | EUR, GBP or USD (default USD) |
| --directory | Target directory |
| --yes | Never prompt |
| --dry-run | Print the file plan and write nothing |
Install
npm install paygateExpress and Fastify are optional peer dependencies. Install the one you use.
Express
import express from "express";
import { paygate } from "paygate/express";
const app = express();
const pw = paygate({ merchantId: process.env.PAYGATE_MERCHANT_ID });
app.get("/api/data", pw({ priceCents: 10 }), (req, res) => {
res.json({ data: "premium content" });
});
// Price in euro, and a price that depends on the request
app.post(
"/api/analyse",
pw({ priceCents: (req) => (req.body.deep ? 50 : 10), currency: "EUR" }),
handler,
);Put the body parser before paygate when the price reads req.body.
Fastify
import Fastify from "fastify";
import paygate from "paygate/fastify";
const app = Fastify();
await app.register(paygate, { merchantId: process.env.PAYGATE_MERCHANT_ID });
// Price in the route config
app.get("/api/data", { config: { paygate: { priceCents: 10 } } }, async () => ({
data: "premium content",
}));
// Or in the handler
app.get("/api/premium", async (req, reply) => {
const { paid } = await req.paygatePay({ priceCents: 25 });
if (!paid) return reply;
return { data: "premium content" };
});How it works
- A request arrives with no
X-PAYMENTheader.paygateanswers 402. The body offers onearispay-fiatentry with your merchant id, the currency, and the price in cents. - The agent (for example
npx payagent pay <url>) gets a single-use authorization from ArisPay and sends the request again withX-PAYMENT. paygatesettles the authorization at ArisPay (POST /v1/fiat/settle) with the terms of this route. It trusts the answer only when ArisPay echoes the same merchant, currency and amount.- Your handler runs. The response carries an
X-Paygate-Receiptheader:{ "paid": true, "rail": "fiat", "paymentId": "...", "merchant": "..." }. The same object is onreq.paygate.
The payment settles before the handler runs, so you never serve an unpaid request. A fiat payment can be refunded at ArisPay.
Re-delivery
If your handler fails after the payment settled, the agent can send the same
payment again. ArisPay replays the settlement a few times inside 10 minutes
and your handler runs again. The receipt then has replayed: true. If your
handler must run once per payment, set redelivery: false. A repeat is then
a 402.
Config
paygate(config) and the Fastify plugin options:
| Option | Default | Meaning |
|---|---|---|
| merchantId | required | Your merchant id |
| currency | "USD" | Default price currency: "EUR", "GBP" or "USD" |
| redelivery | true | Serve a replayed settlement again |
| apiUrl | https://api.arispay.app | ArisPay API URL |
| timeout | 30000 | ArisPay call timeout in ms |
| discoveryRateLimit | 10 per minute | Limit of unpaid probes per client |
| cacheTtlMs | 5 minutes | Manifest cache time |
Route options:
| Option | Meaning |
|---|---|
| priceCents | Price in integer cents, or a function of the request |
| currency | Price currency for this route |
| description | Text shown in the 402 |
| mimeType | MIME type of the paid response (default application/json) |
Prices are integer cents. priceCents: 10 is 0.10. Never pass a float.
Manifest
The Fastify plugin serves your hosted capability manifest at
/.well-known/arispay.json. On Express, mount it yourself:
import { arispayManifest } from "paygate/express";
app.get("/.well-known/arispay.json", arispayManifest(config));Version 7
Version 7 accepts fiat only. It removes the stablecoin rail, the
wallet / network / price config, and the wrap, register, verify
and doctor commands. See CHANGELOG.md.
License
MIT
