---
name: nodecode-api-key-via-crypto
description: Get a Nodecode API key with no account and no browser, paid in crypto, then call the API and refill it on 402.
---

# Nodecode: an API key for an agent, paid in crypto

Nodecode serves OpenAI- and Anthropic-compatible inference at `https://api.nodecode.ai/v1`. An agent with no account gets a key in six HTTP calls: no browser, no email, no sign-up. A person or a wallet pays one on-chain invoice; the agent does everything else.

Machine-readable: [OpenAPI 3.1](https://nodecode.ai/openapi.json). Models and live prices: `GET https://api.nodecode.ai/v1/models` (public, no key).

Every step below is also an MCP tool on the remote Streamable HTTP server `https://nodecode.ai/mcp` (no package install needed; see "With MCP" below). The A2A agent card: https://nodecode.ai/.well-known/agent-card.json. The main pages answer in markdown too: send `Accept: text/markdown`, or read https://nodecode.ai/cyber.md and https://nodecode.ai/pricing.md.

## 1. Discover

- `GET https://nodecode.ai/api/v1/agent/coins`: the coins open now (`coin`, network, token standard, confirmations), the order bounds and the endpoints.
- `GET https://api.nodecode.ai/v1/models`: every public model with `pricing` per million tokens.

## Or: one call with x402 (USDC on Base or Arbitrum One)

An agent with its own USDC wallet on Base or Arbitrum One can skip steps 2 to 6. `POST https://nodecode.ai/api/v1/agent/x402/key` answers `402` with the offer: base64 JSON in the `PAYMENT-REQUIRED` header ([x402](https://x402.org) v2, networks `eip155:8453` and `eip155:42161`), and the same offer in v1's shape in the body (Base only). Sign the EIP-3009 `transferWithAuthorization` it asks for and send the same call again with the payment in `PAYMENT-SIGNATURE` (v1: `X-PAYMENT`). The answer is `200` with `api_key`, `base_url`, `balance_micros`, `tx_hash` and the settlement in the `PAYMENT-RESPONSE` header. The wallet pays no gas: a facilitator submits the transfer, and nothing is credited until two independent chain readers confirm it.

The body is optional: `{ amount_usd, currency, country, key_name }`, 5 to 500, `$5` in dollars by default. A payment is settled once: the same payment sent again within 15 minutes (say, after a crash) returns the same key and pays nothing more.

```js
import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm";
import { privateKeyToAccount } from "viem/accounts";
const wallet = privateKeyToAccount(walletPrivateKey); // the agent's own USDC wallet on Base
const pay = wrapFetchWithPaymentFromConfig(fetch, {
  schemes: [{ network: "eip155:8453", client: new ExactEvmScheme(wallet) }],
  spendControls: { maxAmountPerPayment: "$10" }, // the client pays at most $1 a call unless told more
});
const res = await pay("https://nodecode.ai/api/v1/agent/x402/key", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ amount_usd: 10 }) });
const { api_key, base_url } = await res.json(); // store api_key now
```

## 2. Solve the proof of work

`GET https://nodecode.ai/api/v1/agent/challenge` returns `{ challenge, difficulty, algorithm, expires_at }`. Find a `nonce` (a decimal count works) such that SHA-256 of `challenge + ":" + nonce` starts with `difficulty` zero bits (18 today, about 2^18 hashes, well under a second). A challenge holds 300 seconds and opens one order.

Python 3, standard library only (solves and opens the order):

```python
import hashlib, itertools, json, urllib.request
SITE = "https://nodecode.ai"
c = json.load(urllib.request.urlopen(SITE + "/api/v1/agent/challenge"))
bits = c["difficulty"]
nonce = next(str(i) for i in itertools.count()
             if int.from_bytes(hashlib.sha256(f'{c["challenge"]}:{i}'.encode()).digest(), "big") >> (256 - bits) == 0)
body = json.dumps({"coin": "usdc_base", "amount_usd": 10, "challenge": c["challenge"], "nonce": nonce}).encode()
req = urllib.request.Request(SITE + "/api/v1/agent/orders", body, {"content-type": "application/json"})
order = json.load(urllib.request.urlopen(req))
print(order["address"], order["amount_coin"], order["claim_token"])
```

Node 18+ (an `.mjs` file):

```js
import { createHash } from "node:crypto";
const SITE = "https://nodecode.ai";
const c = await (await fetch(SITE + "/api/v1/agent/challenge")).json();
const solved = (i) => BigInt("0x" + createHash("sha256").update(`${c.challenge}:${i}`).digest("hex")) >> BigInt(256 - c.difficulty) === 0n;
let n = 0;
while (!solved(n)) n++;
const res = await fetch(SITE + "/api/v1/agent/orders", { method: "POST", headers: { "content-type": "application/json" },
  body: JSON.stringify({ coin: "usdc_base", amount_usd: 10, challenge: c.challenge, nonce: String(n) }) });
const order = await res.json();
console.log(order.address, order.amount_coin, order.claim_token);
```

## 3. Open the order

`POST https://nodecode.ai/api/v1/agent/orders` with `{ coin, amount_usd, currency?, country?, key_name?, challenge, nonce }` (JSON). `amount_usd` is whole units of balance, 5 to 500, in `currency` (`usd` by default, or `eur`); VAT goes on top where it is due. No promo codes. At most 2 unpaid orders from one address at once and 5 an hour.

The `201` answer: `order_id`, `claim_token` (`nco_...`), `status`, `coin`, `network`, `address`, `amount_coin`, `payment_uri`, `payment_url` (a read-only status & QR page at `https://nodecode.ai/tx/$ORDER_ID` safe to share with a human; never carries secrets), `subtotal`, `vat`, `total`, `currency`, `expires_at`, `poll_url`, `ack_url`.

**Keep `claim_token` somewhere durable now.** It is said once, it is the only way to the key, and the order keeps only a hash of it.

### Or pay with Stripe (card, Apple Pay, Google Pay, iDEAL, Pix, SEPA)

Prefer a card or bank payment for a human to complete? Call `POST https://nodecode.ai/api/v1/agent/orders/stripe` with `{ amount_usd, currency?, country?, key_name?, challenge, nonce }` (5 to 25 whole dollars or euros for a cold first order; expires in 60 minutes). It returns `order_id`, `claim_token`, `checkout_url`, and `payment_url` (`https://nodecode.ai/tx/$ORDER_ID`). Hand `payment_url` or `checkout_url` to a human; poll `GET https://nodecode.ai/api/v1/agent/orders/$ORDER_ID` with `Authorization: Bearer $CLAIM_TOKEN` and acknowledge with `/ack` identically to crypto.

## 4. Pay

Send exactly `amount_coin` of `coin` on `network` to `address` before `expires_at` (the quote holds 30 minutes). Share `payment_url` (live QR + confirmation page) or `payment_uri` (BIP21, EIP-681, Solana Pay, monero:) with a human's wallet, or pay from the agent's own. Only confirmed coin counts; nothing is credited at 0 confirmations.

## 5. Poll and claim the key

```sh
curl -s https://nodecode.ai/api/v1/agent/orders/$ORDER_ID -H "Authorization: Bearer $CLAIM_TOKEN"
```

Poll every 15 to 60 seconds. `status` goes `open` -> `seen` (coin arrived, confirming) -> `paid`; `underpaid` and `expired` are the other ends. Once `paid` the answer carries `api_key` (`sk-nc-...`), `base_url` (`https://api.nodecode.ai/v1`), `balance_micros`, `balance_url` and `refill_url`. If `key_pending` is true, poll again. A crash before the key was stored is safe: the same key comes back on every poll for 24 hours.

## 6. Acknowledge

Once the key is stored, `POST https://nodecode.ai/api/v1/agent/orders/$ORDER_ID/ack` with the same `Authorization: Bearer $CLAIM_TOKEN`. The sealed copy is wiped; later polls say only `api_key_prefix`.

## 7. Call the API

```sh
curl -s https://api.nodecode.ai/v1/chat/completions -H "Authorization: Bearer $NODECODE_API_KEY" -H "content-type: application/json" \
  -d '{"model": "nodecode-cyber-v1", "messages": [{"role": "user", "content": "hello"}]}'
```

OpenAI SDKs: `base_url="https://api.nodecode.ai/v1"`. Anthropic Messages: `POST https://api.nodecode.ai/v1/messages` with `x-api-key`. OpenAI Responses: `POST https://api.nodecode.ai/v1/responses`.

## 8. Refill itself on 402

When a call answers `402` with `error.code` `insufficient_balance`, `error.refill` (and the `x-nodecode-refill-url` header) says where to top up with the same key:

```sh
curl -s https://nodecode.ai/api/v1/agent/balance -H "Authorization: Bearer $NODECODE_API_KEY"
curl -s -X POST https://nodecode.ai/api/v1/agent/topups/crypto -H "Authorization: Bearer $NODECODE_API_KEY" \
  -H "content-type: application/json" -d '{"coin": "usdc_base", "amount_usd": 20}'
```

Pay the invoice's `address` as in step 4 and poll its `poll_url` (`GET https://nodecode.ai/api/v1/agent/topups/crypto/$INVOICE_ID`, same key) until `status` is `paid`; the balance is credited then. No proof of work and no claim token: the key is the account.

With a USDC wallet on Base or Arbitrum One, refill in one call instead: `POST https://nodecode.ai/api/v1/agent/x402/topup` with the same `Authorization: Bearer $NODECODE_API_KEY` answers `402` with the x402 offer (`error.refill.x402_topup_url` names it too); pay it as in "Or: one call with x402" above, and the `200` answer carries the new balance.

To refill via Stripe Checkout instead (card, Apple Pay, Google Pay, iDEAL, Pix, SEPA): `POST https://nodecode.ai/api/v1/agent/topups/stripe` with `Authorization: Bearer $NODECODE_API_KEY` and `{ amount_usd, currency?, country? }`. Share `payment_url` (`https://nodecode.ai/tx/$TOPUP_ID`) or `checkout_url` with a human and poll `GET https://nodecode.ai/api/v1/agent/topups/stripe/$TOPUP_ID` until `status` is `paid`.

## With MCP

The same flow as tools over remote, stateless Streamable HTTP at `https://nodecode.ai/mcp` (no local package install needed): `list_models`, `list_crypto_coins`, `get_pow_challenge`, `create_crypto_order`, `create_stripe_order`, `check_order_and_claim_key`, `ack_order_key`, `get_balance`, `topup_crypto`, `check_crypto_topup`, `topup_stripe` and `check_stripe_topup`:

```json
{ "mcpServers": { "nodecode": { "url": "https://nodecode.ai/mcp", "headers": { "Authorization": "Bearer sk-nc-..." } } } }
```

Omit `headers` for discovery and creating a new key (call `get_pow_challenge`, solve it, and pass `challenge` and `nonce` to `create_crypto_order` or `create_stripe_order`). Pass the account's key only as the HTTP header `Authorization: Bearer sk-nc-...`, never as a tool argument.

## Errors

Every refusal here is `{ "error": code, "message": words }`: `400` for a coin, amount, promo code or proof of work that does not hold; `401`/`404` for a missing or wrong token; `429` past a cap (wait `retry-after` seconds); `503` while orders are closed or a price cannot be read.
