Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions docs/es/quickstart-x402.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Cobra a Agentes de IA en 5 Minutos con x402
# Paga una API x402 en 5 Minutos

Esta guía de inicio rápido muestra cómo configurar y llamar un endpoint pagado de Nirium desde un script TypeScript. El SDK maneja la negociación x402, la firma de entradas de autorización de Stellar y el reintento pagado después de que el endpoint devuelve `402 Payment Required`.
Esta guía de inicio rápido muestra cómo un agente o script paga un endpoint x402 de Nirium desde TypeScript. Para cobrar por tu propia API, mira [`x402Serve()` en el README](../../README.md#quickstart-charge-for-your-api). El SDK maneja la negociación x402, la firma de entradas de autorización de Stellar y el reintento pagado después de que el endpoint devuelve `402 Payment Required`.

## Lo que Necesitas

Expand Down Expand Up @@ -174,12 +174,14 @@ npx tsx charge-agent.ts

Después de una llamada exitosa:

1. Busca el hash de transacción en los logs o en la respuesta del facilitador
2. Abre `https://stellar.expert/explorer/testnet/tx/<TRANSACTION_HASH>`
1. Abre tu cuenta en Stellar Expert: `https://stellar.expert/explorer/testnet/account/<TU_LLAVE_PUBLICA>`
2. Busca la transferencia de USDC más reciente que salió de tu cuenta y abre su transacción
3. Confirma:
- Cuenta de origen: tu cuenta
- Destino: endpoint del vendedor
- Monto: $0.05 USDC para `/api/v1/premium/market`
- Cuenta de origen de la transacción: el facilitador, que paga el fee
- La transferencia de USDC dentro de ella: de tu cuenta al `payTo` del vendedor
- Monto: 0.05 USDC para `/api/v1/premium/market`

Un `200` con datos no prueba el pago por sí solo; lo prueba la transacción liquidada en el ledger.

## Errores Comunes

Expand All @@ -195,13 +197,13 @@ Después de una llamada exitosa:

## Endpoints Disponibles

| Endpoint | Precio | Red |
| --- | --- | --- |
| `/api/v1/premium/signals` | $0.02 USDC | Testnet solamente |
| `/api/v1/premium/market` | $0.05 USDC | Testnet y pubnet |
| `/api/v1/premium/execute` | $0.25 USDC | Testnet solamente |
| Endpoint | Método | Precio | Red |
| --- | --- | --- | --- |
| `/api/v1/premium/signals` | GET | 0.02 USDC | Solo testnet |
| `/api/v1/premium/market` | GET | 0.05 USDC | Testnet y pubnet |
| `/api/v1/premium/execute` | POST | 0.25 USDC | Solo testnet |

**Nota**: En mainnet, `/api/v1/premium/signals` devuelve `501` (no hay fuente de señales en mainnet); el agente de mainnet no tiene clave de firma por diseño.
Precios leídos del reto `402` de cada endpoint en vivo el 22-nov-2026. En mainnet, `signals` y `execute` devuelven `501` (el agente de mainnet no tiene llave de firma, por diseño).

## Python / LangChain

Expand Down
37 changes: 25 additions & 12 deletions docs/quickstart-x402.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Charge AI Agents in 5 Minutes with x402
# Pay for an x402 API in 5 Minutes

This quickstart shows how to call a paid Nirium endpoint from a TypeScript script. The SDK handles the x402 negotiation, Stellar auth-entry signing, and paid retry after the endpoint returns `402 Payment Required`.
This quickstart shows how an agent or script pays a Nirium x402 endpoint from TypeScript. To charge for your own API instead, see [`x402Serve()` in the README](../README.md#quickstart-charge-for-your-api). The SDK handles the x402 negotiation, Stellar auth-entry signing, and paid retry after the endpoint returns `402 Payment Required`.

## What You Need

- Node.js >=22.12.0 - `@stellar/stellar-sdk` 16.x (a dependency of `nirium`) requires it; an older Node silently resolves an incompatible older SDK instead of failing.
- A funded Stellar testnet secret key. Use a throwaway testnet account only.
- A Stellar testnet account with XLM (from Friendbot), a USDC trustline and testnet USDC from [faucet.circle.com](https://faucet.circle.com). Use a throwaway testnet account only. The steps are in the [Spanish guide, section 2](./es/quickstart-x402.md#2-crear-y-financiar-una-clave-stellar); the commands are the same.
- A clean project where you can install `nirium`.

Do not use a mainnet secret key while testing this flow.
Expand All @@ -24,7 +24,7 @@ Add a local environment file:
```bash
cat > .env <<'EOF'
STELLAR_SECRET_KEY=SXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
NIRIUM_PREMIUM_URL=https://nirium-agent.fly.dev/api/v1/premium/signals
NIRIUM_PREMIUM_URL=https://nirium-agent.fly.dev/api/v1/premium/market
EOF
```

Expand All @@ -41,7 +41,7 @@ import "dotenv/config";
const secretKey = process.env.STELLAR_SECRET_KEY;
const premiumUrl =
process.env.NIRIUM_PREMIUM_URL ??
"https://nirium-agent.fly.dev/api/v1/premium/signals";
"https://nirium-agent.fly.dev/api/v1/premium/market";

if (!secretKey) {
throw new Error("Set STELLAR_SECRET_KEY in .env before running this script.");
Expand All @@ -54,6 +54,7 @@ const agent = new Agent({
agent.initX402({
secretKey,
network: "stellar:testnet",
maxAmountPerPayment: "$1", // refused before signing if the server asks for more
});

const response = await agent.x402Fetch(premiumUrl);
Expand All @@ -73,19 +74,19 @@ Run it with your `.env` loaded:
npx tsx charge-agent.ts
```

The first request receives a `402 Payment Required` challenge. `agent.x402Fetch()` signs the Stellar payment authorization, retries the request, and returns the paid response.
The first request receives a `402 Payment Required` challenge. `agent.x402Fetch()` signs the Stellar payment authorization, retries the request, and returns the paid response. If the server asks for more than `maxAmountPerPayment` (default `$1`), the SDK throws `X402SpendCapError` and signs nothing.

## 3. Verify the Testnet Payment

After a successful call, verify the transaction on Stellar testnet:

1. Find the transaction hash or payment reference returned by the endpoint or facilitator logs.
2. Open `https://stellar.expert/explorer/testnet/tx/<TRANSACTION_HASH>`.
3. Confirm the source account matches your testnet account.
4. Confirm the destination and amount match the endpoint price.
5. Save the explorer link with your integration notes.
1. Open your account on Stellar Expert: `https://stellar.expert/explorer/testnet/account/<YOUR_PUBLIC_KEY>`.
2. Find the newest USDC transfer out of your account and open its transaction.
3. The transaction's source account is the facilitator, which pays the fee; the USDC transfer inside it goes from your account to the seller's `payTo`.
4. Confirm the amount matches the endpoint price (0.05 USDC for `/api/v1/premium/market`).
5. Save the transaction link with your integration notes.

If the response does not include a transaction hash, check the facilitator or server logs for the settled payment record. The paid response should still prove the x402 flow completed because the endpoint only returns protected data after payment authorization.
A `200` with data is not proof of payment on its own: the settled transaction on the ledger is. If you cannot find one, the payment did not settle; see Troubleshooting. The endpoint only returns protected data after payment authorization.

## 4. Change the Price or Endpoint

Expand All @@ -109,8 +110,20 @@ npx tsx charge-agent.ts
| `Set STELLAR_SECRET_KEY` | Add a funded testnet secret key to `.env`. |
| `402 Payment Required` is returned to your code | Use `agent.x402Fetch()` instead of `fetch()` so the SDK can sign and retry the payment. |
| Transaction cannot be found | Make sure you are checking Stellar testnet and that the payment was not rejected before settlement. |
| `X402SpendCapError` | The endpoint asks for more than your `maxAmountPerPayment`. Raise it on purpose, e.g. `maxAmountPerPayment: "$5"`. |
| `HostError: Error(Contract, #13)` | Your account has no USDC trustline. Add it, then get testnet USDC from the faucet. |
| Mainnet funds at risk | Stop and switch back to `network: "stellar:testnet"` with a testnet account. |

## Endpoints

| Endpoint | Method | Price | Network |
| --- | --- | --- | --- |
| `/api/v1/premium/signals` | GET | 0.02 USDC | Testnet only |
| `/api/v1/premium/market` | GET | 0.05 USDC | Testnet and mainnet |
| `/api/v1/premium/execute` | POST | 0.25 USDC | Testnet only |

Prices read from each endpoint's live `402` challenge on 22 Nov 2026. On mainnet, `signals` and `execute` answer `501`.

## Python / LangChain

LangChain agents can pay the same endpoint through `NiriumX402Tool`, which wraps `init_x402` / `x402_fetch` and never exposes the Stellar secret to the model:
Expand Down