Documentation

Getting started

CortexPay lets AI agents pay for services on their own, inside spending policies you set. Every payment passes ten policy checks before anything is signed. Start in the sandbox: balances are simulated sUSDC and nothing is broadcast to a blockchain, so you can build the whole integration before any real money is involved.
Code samples in

1. Open a sandbox#

A sandbox workspace comes with demonstration agents, spending policies and a catalogue of sandbox services that speak real x402. Open one in the browser; no wallet or sign-up is needed.

Open a sandbox and go to API keys — this calls GET /api/session/guest, which starts a guest session and redirects back.

Guest sandboxes are temporary
A guest workspace is removed seven days after it was created, with everything in it. Connect a wallet (Sign-In with Ethereum) to keep it; see Authentication.

2. Create an API key#

In Developer Console → API keys, create a key with the Agent runtime preset (agents:read, payments:read, payments:write, services:read). Keys in a sandbox workspace start with cpk_test_; keys in a live workspace start with cpk_live_.

  1. The secret is shown once. CortexPay stores only its SHA-256 hash and the prefix.
  2. Send it as Authorization: Bearer cpk_test_…. Keep it on your server, never in a browser.
  3. Only an owner or admin in a signed-in session can create keys. A key can never create another key.

3. Check the key#

GET /api/me returns the workspace the key belongs to and which platform features are configured on this deployment.

const res = await fetch("https://cortexpay.vercel.app/api/me", {
  headers: { Authorization: `Bearer ${process.env.CORTEXPAY_API_KEY}` },
});
const me = await res.json();
console.log(me.org.name, me.org.mode, me.apiKey?.scopes);

4. Find a service#

Agents discover paid services in the marketplace catalogue. Filter by category, price, network or asset; every listing carries its x402 payment terms.

import { CortexPay } from "@cortexpay/sdk";

const cortex = new CortexPay({ apiKey: process.env.CORTEXPAY_API_KEY!, baseUrl: "https://cortexpay.vercel.app" });

const { data } = await cortex.services.list({ category: "data", sort: "price_asc", limit: 5 });
for (const s of data) console.log(s.id, s.name);

5. Pay for a request#

POST /api/services/request runs the whole flow in one call: it reads the service's x402 challenge, quotes the price, evaluates the agent's policy, then pays and calls the service, or stops for an owner's approval. Send an Idempotency-Key so a retry can never pay twice.

const BASE_URL = "https://cortexpay.vercel.app";

type ServiceResult = {
  requestId: string;
  status: "delivered" | "awaiting_approval" | "rejected" | "failed" | "expired" | string;
  approvalId: string | null;
  paymentId: string | null;
  evaluation: { decision: string; reasonCode: string | null; reason: string } | null;
  delivery: { httpStatus: number; data: unknown } | null;
  error: { code: string; message: string } | null;
};

const res = await fetch(`${BASE_URL}/api/services/request`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.CORTEXPAY_API_KEY}`,
    "Content-Type": "application/json",
    // Required. Reuse the same key when you retry this exact request.
    "Idempotency-Key": crypto.randomUUID(),
  },
  body: JSON.stringify({
    agentId: "AGENT_ID",
    serviceId: "SERVICE_ID",
    input: { query: "Summarise stablecoin flows for today" },
  }),
});
const result = (await res.json()) as ServiceResult;

if (result.status === "awaiting_approval") console.log("Waiting for an owner:", result.approvalId);
else if (result.status === "delivered") console.log(result.delivery?.data);
else console.log(result.status, result.evaluation?.reason ?? result.error?.message);
Sandbox first
In a sandbox workspace the payment settles on CortexPay's simulated ledger and the settlement reference starts with sim_. Live workspaces pay real USDC from the owner's wallet, after the owner approves and signs each payment; see Sandbox vs live.

6. Read the outcome#

The response is the request's current state. It is 200 for a final outcome and 202 when the payment waits for an owner's decision (status: "awaiting_approval", with an approvalId).

StatusMeaningFinal
requestedThe agent requested a paid service; a price quote is being evaluated.No
awaiting_approvalThe spending policy requires an owner decision before payment.No
authorizedThe policy engine authorized this payment; it has not been submitted yet.No
submittedThe payment authorization was sent for verification and settlement.No
confirmedSettlement is confirmed (on-chain, or in the sandbox ledger when simulated). Service delivery is tracked separately.No
deliveredPayment settled and the service returned a successful response.Yes
rejectedThe policy engine or the owner rejected the payment. No funds moved.Yes
failedVerification, settlement or delivery failed. See the detail for whether funds moved.Yes
expiredThe quote or authorization expired before it was used.Yes

Next steps#

  1. Give each agent a policy that fits its job: Spending policies.
  2. Get told when payments settle or need approval: Webhooks.
  3. Use the typed client: TypeScript SDK.