Code execution
POST /api/code-runExecute Python or JavaScript code in a secure, isolated cloud sandbox. Send POST /api/code-run with the required field code and pay $0.020 per call over x402 or MPP (there is no free tier). It returns a JSON object with language, stdout, stderr, result and error.
Returns stdout, stderr, and the expression result. No setup needed; pay per call via x402. 30s timeout, 10k char code limit. A run that hits the time limit is returned with timedOut: true and the output it printed, and is charged.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
code | string | yes | Source code to execute (max 10,000 chars) Also accepted as source, src, barcode, upc, ean, gtin. |
language | string | no | Language: python (default) or javascript |
Example request
curl -i -X POST https://agent402.tools/api/code-run \
-H "Content-Type: application/json" \
-d '{"code":"print('\''Hello from Agent402!'\'')","language":"python"}'
Without payment this returns HTTP 402 Payment Required with the exact price for code-run; any x402 v2 or MPP client pays it and retries.
Example response
{
"language": "python",
"stdout": "Hello from Agent402!\n",
"stderr": "",
"result": null,
"error": null
}
| Field | Type | Always present | In the example |
|---|---|---|---|
language | string | yes | python |
stdout | string | yes | Hello from Agent402! |
stderr | string | yes | |
result | null | no | null |
error | null | no | null |
From an MCP client
catalog.call {
"slug": "code-run",
"params": {
"code": "print('Hello from Agent402!')",
"language": "python"
}
}
The hosted connector at https://agent402.tools/mcp needs a payment for code-run; the stdio package pays it from a wallet or from AGENT402_CREDITS_KEY. Local install: npx -y agent402-mcp.
Errors and behavior
codeis required. An input the tool rejects returns an HTTP 4xx whose body carrieserror,tool,expected,requiredandexample, so the caller can correct it.- A paid call that ends in any status of 400 or above is not charged over x402, MPP or a prepaid credits key: settlement is cancelled when the tool fails. The exception is a Tempo push credential, a transfer the buyer sent before the call: it settles before the tool runs, so if the tool then fails the payment is recorded as a refund owed to the paying wallet.
- Wallet-only: this tool reaches the network or stored state, so it has no proof-of-work tier. A prepaid card-credits key issued earlier (
Authorization: Bearer a402_...) also pays it. - A
GETorHEADto /api/code-run returns the same 402 quote, so the price can be read without a body. - An
Idempotency-Keyheader makes a retried paid call replay the first 200 instead of charging again (an answer larger than 1 MB is not replayed).
Paid call (JavaScript agent)
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
const client = new x402Client();
client.setSpendControls?.(false); // keep your own spending ceiling in code
registerExactEvmScheme(client, { signer: privateKeyToAccount(KEY) });
const payFetch = wrapFetchWithPayment(fetch, client);
const res = await payFetch("https://agent402.tools/api/code-run", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
"code": "print('Hello from Agent402!')",
"language": "python"
}),
});
Related tools
Code execution (Pro)
POST /api/code-run-proExecute Python or JavaScript code in a secure, isolated cloud sandbox (Pro tier). Same as /api/code-run but with 60s tim…
Decisions (OpenAI wire)
POST /v1/decisionsOpenAI's Decisions API on its own wire: point an OpenAI SDK's base URL here and client.decisions.create works unchanged,…
Text embeddings
POST /api/embedGenerate a text embedding vector using OpenAI text-embedding-3-small (1536 dimensions). Ideal for semantic search, RAG, …
Text embeddings (Large)
POST /api/embed-largeGenerate a text embedding vector using OpenAI text-embedding-3-large (3072 dimensions). Higher accuracy than the small m…
Image generation
POST /api/image-genGenerate an image from a text prompt using GPT Image 2 (low quality, 1024x1024). No API key needed; pay per call via x40…
Image generation (HD)
POST /api/image-gen-hdGenerate a higher-quality image from a text prompt using GPT Image 2 (medium quality, 1024x1024). No API key needed; pay…