Skip to main content

Charge AI agents per request

AI agents can pay for what they use, one request at a time. With x402, your API answers an unpaid request with a price, the agent's wallet signs a payment, and the request goes through once the payment settles on the Keeta Network. The money arrives in your Antara account a few seconds later.

No payment processor is involved. Antara is non-custodial: payments go straight from the agent's account to yours, and Alpaca never holds them.

This guide follows Keeta's own guide, Using x402 on Keeta, with the versions it was tested with.

How a paid request works​

  1. An agent calls your route without paying. Your server answers 402 Payment Required, with the price, the token and the address to pay.
  2. The agent's wallet signs a payment for exactly that amount. It signs, but doesn't publish it.
  3. Your server passes the signed payment to a facilitator. The facilitator checks it, publishes it to the Keeta Network and pays the network fee. It can't change the payment or send it anywhere else.
  4. Once the payment settles, your server answers the request. The full price is in your account.

What you need​

  • An Antara account, in the app on iPhone or Android or on app.antarafi.com.
  • A server on Node.js 24 that you can add a middleware to. The example uses Express.
  • A first run on the test network. Move to the main network once it works.

1. Copy your address​

The agent pays your Keeta address, which starts with keeta_.

  • iPhone and Android: tap Receive and copy the address.
  • Web: open the account menu in the sidebar and copy the address.

Use the address in your server, not your @name. An @name is for people. If you ever release it, someone else can claim it later, and payments would follow the name to them.

2. Try it on the test network​

Install the Keeta and x402 packages. @x402/keeta pins an older copy of one Keeta package, so set the overrides before the first install:

npm init -y && npm pkg set type=module
npm pkg set 'overrides.@keetanetwork/anchor=$@keetanetwork/anchor' 'overrides.@keetanetwork/keetanet-client=$@keetanetwork/keetanet-client'
npm install @keetanetwork/keetanet-client@0.18.8 @keetanetwork/anchor@0.0.104 @x402/core@2.28.0 @x402/keeta@2.28.0 @x402/express@2.28.0 express

Then charge for a route. This one costs 0.001 KTA on the test network:

import express from "express";
import { x402ResourceServer, HTTPFacilitatorClient } from "@x402/core/server";
import { paymentMiddleware } from "@x402/express";
import { KEETA_TESTNET_CAIP2, KTA_TESTNET_ADDRESS } from "@x402/keeta";
import { ExactKeetaScheme } from "@x402/keeta/exact/server";

const payTo = process.env.ANTARA_ADDRESS; // the keeta_ address you copied
const facilitator = new HTTPFacilitatorClient({ url: "https://facilitator.x402.keeta.com" });
const server = new x402ResourceServer(facilitator);
server.register(KEETA_TESTNET_CAIP2, new ExactKeetaScheme());

// Stop early if the facilitator doesn't settle this network
const { kinds } = await facilitator.getSupported();
if (!kinds.some((kind) => kind.scheme === "exact" && kind.network === KEETA_TESTNET_CAIP2)) {
throw new Error("The facilitator doesn't settle exact payments on this network");
}

const app = express();
app.use(paymentMiddleware({
"GET /weather": {
accepts: [
// A Keeta token and an amount in its smallest units: 1000 is 0.001 KTA
{ scheme: "exact", price: { asset: KTA_TESTNET_ADDRESS, amount: "1000" }, network: KEETA_TESTNET_CAIP2, payTo },
],
description: "Current weather for any location",
mimeType: "application/json",
},
}, server));

app.get("/weather", (_req, res) => res.send({ weather: "sunny", temperature: 21 }));
app.listen(4021);

To see a payment arrive, pay the route from a test wallet with Keeta's buyer example in Using x402 on Keeta. The faucet on the test network gives KTA. To watch the payment come in, switch Antara to the test network in settings.

Price test routes in KTA. The USDC that @x402/keeta 2.28.0 names for the test network doesn't exist there.

3. Go live on the main network​

Change three things:

  • Use KEETA_MAINNET_CAIP2 in server.register and in each entry under accepts.
  • Set your price. On the main network a plain price in dollars is paid in USDC: price: "0.01" asks for 0.01 USDC. You can also name any Keeta token with an amount in its smallest units, as above. Read the token's decimals from the network rather than assuming them.
  • Set payTo to your address. It's the same address on both networks.

Where the money shows up​

Each paid request arrives as its own incoming payment from the agent's account, in your Antara activity a few seconds after the request.

USDC shows in Antara as its own balance. Verified accounts outside the EU can convert it to US Dollars in Convert. Conversions and bank transfers are provided by Bivo Inc. (NMLS #2572288).

Good to know​

  • The facilitator at facilitator.x402.keeta.com is run by an independent third party under the Keeta Grant Program. Neither Keeta, Inc. nor Alpaca operates it.
  • Agents pay one request at a time from each account. A Keeta account signs its payments in order, so agents queue their calls or pay from several accounts.
  • Your route decides what a payment buys. Prices, refunds and what you deliver are between you and whoever pays. Alpaca is not a payment processor and doesn't take part in the sale.
  • Taxes and records are yours to keep. Every payment is on the Keeta Network, with the paying account and the amount.

Further reading​