services
Get a key

library / AI and blockchain / SI0006

Agent RPC key safety

A React app cannot hide an RPC key, because the browser sends it and anyone can read it. Keep the key on a server, and scan each build with a short script to catch a copy that shipped.

By Carbium · Agent sign-up and keys · SI0006 · updated 2026-11-22

01

Is the RPC URL hidden in a React app?

No, and it cannot be. A React app runs in the visitor's browser, so every request it makes can be read by the person using it. Open the Network tab, find the call to the RPC endpoint, and the full URL, the request headers and the body are all there. Minifying the bundle, splitting the string in two or encoding it in base64 changes what a text search finds in your files. The request that leaves the browser still carries the key in the clear.

Two things get called the endpoint URL. The host is public: the RPC endpoint is https://services.carbium.sh and the WebSocket endpoint is wss://services.carbium.sh, the same for every account. Knowing the host gives a stranger nothing. The secret is the key that goes with it, and a browser that calls the endpoint directly has to hold that key.

Build tools make this easy to get wrong. Vite copies every variable named VITE_* into the bundle as a literal string, and Create React App does the same for REACT_APP_*. Both tools treat a variable with that prefix as public. A key in VITE_CARBIUM_KEY is in your JavaScript after the build, and in the source map if you ship one.

02

Where the key travels

How the key is sentWhat a visitor sees
x-api-key header on an RPC callthe request headers in the Network tab
authorization: Bearer header on an RPC callthe same request headers
?api-key= on the RPC URLthe whole URL, which browser history and any log that records URLs also keep
?api-key= on the WebSocket URLthe socket's URL in the Network tab
Sec-WebSocket-Protocol: api-key. plus the keythe handshake request headers

Each row is a form Carbium accepts, and the visitor can read every one, because a browser cannot keep a secret from its own user. A browser WebSocket also cannot set custom headers, so from a page the key can only ride in the URL or in the subprotocol. Both show up in the developer tools.

03

What a leaked key costs

Limits are per account, and an account's keys share one budget, so a stranger's calls count against yours. Carbium meters each call in cells, and a stranger picks the expensive methods. getTransaction and getSignaturesForAddress cost 50 cells each. getMultipleAccounts charges 10 cells per pubkey and takes up to 300 on Pro and 100 below it, so one full call is 1,000 to 3,000 cells. A JSON-RPC request can carry a batch of up to 100 calls, which makes one batch of getTransaction 5,000 cells. A cached read such as getSlot costs 1.

The symptom shows in your own app first. Your users see 429 answers while nothing in your code changed. RPC rate-limit alerts covers alerting on that signal. A wave of 429 answers with no matching traffic from you is a reason to scan your build.

04

Keep the key on a server

The browser calls your own backend, and the backend calls Carbium with the key. The key lives in an environment variable on the server and never appears in a response body. The page asks for /api/slot or /api/balance, and your server decides whether that user may ask and how often.

That route does the work the leaked key did for free. It authenticates your user, maps each route to one fixed JSON-RPC method and caps calls per user. Read-only model tools builds the fixed allowlist for a model's tool calls, and the same code serves a React front end.

!A route that forwards whatever method and params the browser names is the leaked key again, one hop later, with your server paying for the hop.

Sockets get the same treatment. Your server opens the upstream socket with ?api-key= and relays what the page needs. A WebSocket subscription opens at 5, 25 or 200 credits by filter weight, then costs 20 credits per MiB sent, and Free carries 5 sockets. One upstream subscription shared by every browser session that wants the same feed uses far fewer sockets than one per visitor.

05

Agents hold keys too

The same rule covers an AI agent, because a model's context leaks the way a browser does. Whatever sits in a prompt, a tool argument or a tool result can be echoed to a user, written to a trace or stored in a transcript. Keep CARBIUM_KEY in the process that makes the call. The model gets tool names and results, and never the key or a URL with ?api-key= in it. An HTTP error message often quotes the request URL, so log the method and the status code and leave the URL out. That is one reason to prefer the header over the query form on a server.

Coding agents that scaffold a React app are the likeliest source of this leak, because the quickest working code puts the key in a VITE_ variable. Put the rule in the agent's instructions: no key in code that ships to a browser.

06

Audit a build for the key

The script scans a folder (the current directory by default) for four things: the key itself, the text api-key= in a URL, the x-api-key header name and the RPC host. It matches the key as text and never prints it. It skips node_modules, .git, files over 5 MB and itself. Then it calls getSlot with the key from the server side, which shows the key is live and what a stranger could use.

main.ts
// SI0006 · Agent RPC key safety// https://carbium.sh/library/si/agent-rpc-key-safety// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MIT// Scan a build folder for a leaked Carbium key, then confirm the key is live. Run: CARBIUM_KEY=... node main.ts [dir]import { readdirSync, readFileSync, statSync, type Dirent } from "node:fs";import { join, resolve } from "node:path";import { 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 url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const root = process.argv[2] ?? ".";// What to look for. The key is matched as text and never printed.const checks: Record<string, string | RegExp> = {  "the key itself": key,  "a key in a URL": /api[-]key=/,  "the key header name": /x-api-[k]ey/i,  "the RPC host": new URL(url).hostname,};function* walk(dir: string): Generator<string> {  let entries: Dirent[] = [];  try {    entries = readdirSync(dir, { withFileTypes: true });  } catch {}  for (const entry of entries) {    if (entry.name === "node_modules" || entry.name === ".git") continue;    const path = join(dir, entry.name);    if (entry.isDirectory()) yield* walk(path);    else if (entry.isFile() && resolve(path) !== import.meta.filename && statSync(path).size < 5_000_000) yield path;  }}let files = 0;const hits: string[] = [];for (const path of walk(root)) {  files++;  const text = readFileSync(path, "latin1");  for (const [what, needle] of Object.entries(checks)) {    if (typeof needle === "string" ? text.includes(needle) : needle.test(text)) hits.push(`${path}: ${what}`);  }}console.log(`scanned ${files} files under ${root}`);for (const hit of hits) console.log(`FOUND ${hit}`);if (!hits.length) console.log("no key, key header or RPC host in these files");// The key stays on the server side: this is the call a stranger could make with a leaked copy.const rpc = createSolanaRpcFromTransport(createDefaultRpcTransport({ url, headers: { "x-api-key": key } }));const slot = await rpc.getSlot().send();console.log(`key is live: getSlot answered ${slot} at confirmed commitment`);if (hits.some((hit) => hit.endsWith("the key itself"))) process.exitCode = 2;

run: node main.ts dist

Build your app, then run node main.ts dist. Each hit prints as FOUND path: what. Finding the key itself sets exit code 2. The host alone means the browser calls the endpoint directly, so that page has to carry a key somewhere: look in the Network tab. A clean scan prints one line saying so, and the last line prints the current slot.

A clean result means something only after the check has caught a known positive. Put the key in a scratch file in a scratch folder, scan that folder and confirm the FOUND line.

07

Where it ends

  • The scan reads files, and the deployed site can differ. A CDN may still serve an older build, so open the Network tab on the live page once.
  • It matches plain text. A key inside a gzip or Brotli file, split into string pieces or percent-encoded passes the scan. If the page sends the key at all, the Network tab shows it.
  • It cannot tell who already copied a key that shipped. A key that has been in a public bundle, a source map or a git history is exposed, and the script does not read git history.
  • A server in front keeps the key out of the browser. Your own users can still spend your budget through the route, so per-user caps are yours to write.
  • The host in a bundle is no leak by itself. It tells you the browser calls Carbium directly, and that call needs a key.
08

When it goes wrong

what goes wrongwhat to do
The key sits in a VITE_ or REACT_APP_ variable.Move it to a server environment variable without the prefix, scan the build again, and treat the key that shipped as exposed.
The page calls the RPC endpoint straight from the browser with ?api-key= or an x-api-key header.Call your own route and let the server add the header. Anything the browser sends, the visitor can read.
A server route forwards any method and params the browser sends.Map each route to one fixed JSON-RPC method, as the allowlist in Read-only model tools does, and cap calls per user.
429 answers arrive with no matching traffic from your app.Scan your builds, source maps and repos for the key. Limits are per account, so any copy of a key spends the same budget as yours.
The scan is clean on a build that still leaks.Encoded or compressed keys pass a text scan. Open the Network tab on the live page and read the request headers and URLs.
The key appears in an agent's transcript or log.Keep it in the calling process and send it in the header. Log the method and status, never the URL, and keep it out of prompts, tool arguments and tool results.
09

Questions

Is the Carbium RPC URL a secret?

No. https://services.carbium.sh is the same for every account. The key is the secret, and it is the key that must stay off the browser.

Is the header or the query form safer in a browser?

Neither, since both show in the developer tools. On a server the header keeps the key out of URLs that clients and proxies log.

Can a browser WebSocket use the header?

No. A browser cannot set custom headers on a WebSocket, so a page would have to put the key in the URL or the subprotocol, and both are visible. Open the upstream socket from your server.

What does the audit script cost to run?

One getSlot, a cached read at 1 cell. The file scan makes no network call.

Does a key in an agent's tool code count as exposed?

Only if the model or a user can read it. Keep it in the process, out of prompts, tool arguments and results. Read-only model tools shows that layout.