Documentation
Getting started
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.
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_.
- The secret is shown once. CortexPay stores only its SHA-256 hash and the prefix.
- Send it as
Authorization: Bearer cpk_test_…. Keep it on your server, never in a browser. - 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);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).
| Status | Meaning | Final |
|---|---|---|
requested | The agent requested a paid service; a price quote is being evaluated. | No |
awaiting_approval | The spending policy requires an owner decision before payment. | No |
authorized | The policy engine authorized this payment; it has not been submitted yet. | No |
submitted | The payment authorization was sent for verification and settlement. | No |
confirmed | Settlement is confirmed (on-chain, or in the sandbox ledger when simulated). Service delivery is tracked separately. | No |
delivered | Payment settled and the service returned a successful response. | Yes |
rejected | The policy engine or the owner rejected the payment. No funds moved. | Yes |
failed | Verification, settlement or delivery failed. See the detail for whether funds moved. | Yes |
expired | The quote or authorization expired before it was used. | Yes |
Next steps#
- Give each agent a policy that fits its job: Spending policies.
- Get told when payments settle or need approval: Webhooks.
- Use the typed client: TypeScript SDK.