library / AI and blockchain / SI0025
API key setup for agents
Where a Solana API key comes from, which plan fits an agent, how the key travels on each endpoint, and a preflight script that confirms RPC and WebSocket answer with your key before the agent starts any work.
Getting a key
A Solana API key is the credential an RPC provider checks on every request. Carbium issues keys from your account, and one key covers everything on services.carbium.sh: RPC over HTTPS, WebSocket subscriptions, the gRPC stream and quotes. Sign up, open the keys page, create a key, and put it where your agent's process can read it as CARBIUM_KEY. A secrets manager or a deploy-time environment variable both work. A key committed to a repository or pasted into a prompt is the usual way these end up in the wrong hands.
Keys belong to the account, and limits are per account. All keys on an account draw from one allowance; a key per agent or deployment lets you replace one that leaks without touching the rest.
How the key travels
| Endpoint | Where the key goes |
|---|---|
| RPC, https://services.carbium.sh | the x-api-key header, or authorization: Bearer <key>, or ?api-key=<key> on the URL |
| WebSocket, wss://services.carbium.sh | ?api-key=<key> on the URL, or the subprotocol api-key.<key> |
| gRPC stream, https://services.carbium.sh:9443 | the x-token header |
In code that runs on a server, use the header for RPC. A URL with the key in it gets copied into logs, proxy records and error messages, and a header does not. A WebSocket client in Node has the query form as its simplest option, so build that URL from the environment variable and never print it. Agent RPC key safety goes through how a key ends up in a model's context or a browser bundle, and how to keep it out.
Which plan an agent needs
| Plan | Price per month | Credits |
|---|---|---|
| Free | $0 | 500K once, at sign-up |
| Dev | $20 | 10M a month |
| Business | $250 | 50M a month |
| Pro | $500 | 200M a month |
Credits meter RPC, WebSocket, stream and webhook work. Each paid month adds credits, and unspent credits stay. A separate limit sits in front: every call carries an admission charge in cells, and each plan has a rate in calls per second for the class of method. A cached read such as getSlot or getHealth runs at 5 req/s on Free, 30 on Dev, 100 on Business and 500 on Pro. Account reads such as getAccountInfo and getTokenAccountBalance run at 0.5 req/s on Free, which is one call every two seconds, then 15, 50 and 400.
Agents loop, so the rate decides whether a plan fits before the credits do. A class absorbs a burst of 1,024 cells or one second of its rate, whichever is larger. For account reads below Pro the burst is 1,024 cells, about 100 reads, and Pro's account-read class absorbs 4,000 cells. A tool that calls getTokenAccountBalance for each token account in a list of twenty sends 200 cells at once, so on Free the whole list fits inside the burst. Once the burst is spent, each further batch of twenty reads takes 40 s at the Free rate of 0.5 req/s. Past its burst and rate a call gets 429 with a retry-after hint, and a Free key can get a 429 as well when its network is over a shared budget. Write the agent's tool wrapper to read that hint and back off.
WebSockets add two costs. Your plan caps sockets and subscriptions per account, and Free carries 5 sockets. A subscription costs 5, 25 or 200 credits to open by filter weight, then 20 credits per MiB sent. A refused subscription costs nothing.
Preflight before the agent starts
Run one short script when an agent boots or when you rotate a key. It reads the key from the environment, calls three cached reads over RPC (getHealth, getVersion, getSlot), then opens a WebSocket and closes it. Each check runs on its own, and a failure in one prints and leaves the others to finish.
// SI0025 · API key setup for agents// https://carbium.sh/library/si/api-key-setup-for-agents// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MIT// Preflight for an agent's Carbium key: three cached RPC reads and a WebSocket open with the key. Run: CARBIUM_KEY=... node main.tsimport { createDefaultRpcTransport, createSolanaRpcFromTransport } from "@solana/kit";const key = process.env.CARBIUM_KEY;if (!key) { console.error("Set CARBIUM_KEY to your Carbium API key."); process.exit(1);}const apiKey: string = key;const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const wsUrl = process.env.CARBIUM_WS_URL ?? "wss://services.carbium.sh";const rpc = createSolanaRpcFromTransport(createDefaultRpcTransport({ url, headers: { "x-api-key": apiKey } }));// Open a socket with the key in the URL, then close it. No subscription is made.function socketOpens(): Promise<string> { return new Promise((resolve, reject) => { const ws = new WebSocket(`${wsUrl}?api-key=${encodeURIComponent(apiKey)}`); const timer = setTimeout(() => { ws.close(); reject(new Error("no open within 8 s")); }, 8000); ws.onopen = () => { clearTimeout(timer); ws.close(); resolve("opened and closed"); }; ws.onerror = () => { clearTimeout(timer); reject(new Error("socket refused")); }; });}const shown = (value: unknown) => (typeof value === "object" ? JSON.stringify(value) : String(value));const checks: Array<[string, () => Promise<unknown>]> = [ ["getHealth", () => rpc.getHealth().send()], ["getVersion", () => rpc.getVersion().send()], ["getSlot (confirmed)", () => rpc.getSlot().send()], ["websocket", socketOpens],];const results = await Promise.allSettled(checks.map(([, run]) => run()));results.forEach((result, i) => { const name = checks[i][0]; if (result.status === "fulfilled") { console.log(`ok ${name}: ${shown(result.value)}`); } else { const message = String(result.reason?.message ?? result.reason); console.log(`FAILED ${name}: ${message.split(apiKey).join("[key]")}`); }});if (results.some((result) => result.status === "rejected")) process.exitCode = 1;
run: node main.ts
Run it with CARBIUM_KEY=... node main.ts. A healthy setup prints ok on all four lines. getSlot leaves commitment out, so @solana/kit fills in confirmed, while the JSON-RPC server's own default is finalized. The three RPC calls are cached reads at 1 cell each, and the socket opens without a subscription.
A failure line has the key replaced with [key] before it prints, which keeps a message that quotes a URL from leaking it into your logs. The exit code is 1 when any check fails, so a deploy script can stop on it.
Run the preflight from the same launcher, unit or container the agent starts from. That run catches the common slip, an agent started somewhere that never had the variable, and the script stops with its message before any call goes out.
Where it ends
- A passing check shows the calls and the socket went through with this key at this moment. It does not show the key survives the agent's real load, so size the plan from the rates above.
- It does not test the QUOTE scope. A key that reads accounts fine can still get 401 -32001 on quote until the scope is given.
- The socket check opens one connection and closes it. On Free, with 5 sockets, a running agent that already holds all five will see the check refused.
- A check cannot tell who else holds the key. If it has been in a prompt, a transcript, a bundle or a git history, treat it as exposed and replace it.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| The agent starts and every call answers 401 UNKNOWN_KEY. | Attach the whole key. A copy cut short or a stale value from an old shell gets refused at the door. Run the preflight and read the failure line. |
| quote answers 401 with code -32001 while account reads work. | The key lacks the QUOTE scope. Give the key that scope in your account and call again. |
| 429 answers with a retry-after hint during a burst of tool calls. | Back off by the hint and cut parallel calls. On Free, account reads run at 0.5 req/s, so a plan with a higher rate is the fix when the agent needs more. |
| The gRPC stream refuses a Free key. | The stream opens on Dev, Business and Pro. Send the key in x-token on one of those plans, or read the same data over RPC or WebSocket. |
| getTokenLargestAccounts or getProgramAccountsV2 answers -32601. | These methods are not served, and a retry gives the same answer. Remove the call from the agent's tool list. |
| The key shows up in a log line or an agent transcript. | Log the method and status and leave the URL out. Keep the key in the process that makes the call, and replace the exposed key. |
Questions
Is there a free Solana API key?
Yes. The Free plan costs $0 and starts with 500K credits once, at sign-up. It gets the same RPC and WebSocket endpoints as the paid plans, at lower rates, and no gRPC stream.
Do I need a separate key for each agent?
You do not need one, since limits are per account and keys share one budget. Separate keys keep each agent's secret apart, so one leak means replacing one key.
Which header does the key go in?
For RPC, x-api-key. A bearer token in authorization and ?api-key= also work. The gRPC stream reads x-token.
Where do I put the key in a WebSocket client?
In the URL as ?api-key=<key>, or in the subprotocol as api-key.<key>. Build the URL from the environment variable and keep it out of logs.
Does the key cost credits just for connecting?
The preflight's three RPC calls are metered in credits, and no subscription charge applies because the socket opens and closes without subscribing. First Solana RPC API call covers a first call.