Use sandboxes
Provider keys
Give Claude Code, Codex, OpenCode and anything else in your sandboxes an AI provider's API key that never enters a sandbox, with cloudex keys.
cloudex keys set stores an AI provider’s API key in your deployment. Sandboxes then use the key without holding it: they get a placeholder, and the real key is added to their requests to the provider as the requests leave the sandbox. The key isn’t in a sandbox’s files, environment, saves, backups or forks, so an agent can’t print it, send it anywhere or leave it behind.
cloudex keys set anthropic # asks for the key, hidden
cloudex keys set openai --from-env # reads $OPENAI_API_KEY here
cloudex keys # lists them by last 4 characters
cloudex keys rm openai
| Provider | cloudex keys set |
Its API | Variables in the sandbox |
|---|---|---|---|
| Anthropic | anthropic |
api.anthropic.com |
ANTHROPIC_API_KEY |
| OpenAI | openai |
api.openai.com |
OPENAI_API_KEY, CODEX_API_KEY |
| OpenRouter | openrouter |
openrouter.ai |
OPENROUTER_API_KEY |
| Google (Gemini) | google |
generativelanguage.googleapis.com |
GEMINI_API_KEY, GOOGLE_API_KEY, GOOGLE_GENERATIVE_AI_API_KEY |
| Groq | groq |
api.groq.com |
GROQ_API_KEY |
| Mistral | mistral |
api.mistral.ai |
MISTRAL_API_KEY |
| xAI | xai |
api.x.ai |
XAI_API_KEY |
| DeepSeek | deepseek |
api.deepseek.com |
DEEPSEEK_API_KEY |
--from-env reads the provider’s first variable on your computer, such as $OPENROUTER_API_KEY, or $GEMINI_API_KEY for Google.
cloudex keys set also reads the key from standard input when it’s piped in.
Before it stores a key, the deployment asks the provider whether the key works. A key the provider rejects isn’t stored, and cloudex keys set prints the provider’s reason. A Claude subscription token, which starts with sk-ant-oat, isn’t an API key, and the check rejects it. If the provider can’t be reached, the key is stored without the check.
Then start a sandbox and give an agent a task, without signing in:
cloudex new
cloudex prompt "make the tests pass"
Agents
Claude Code uses the Anthropic key with no sign-in, in cloudex prompt, a shell or claude -p.
Codex uses the OpenAI key with no sign-in. Interactive Codex only skips its sign-in screen when it has a login, so a sandbox with an OpenAI key and no Codex login of its own gets one that holds the placeholder. It goes away when the key does.
OpenCode uses any of them with no sign-in, with --model provider/model, or a model it picks from the providers that have a key. See OpenCode.
While a provider’s key is stored, the agents in a sandbox use it even if someone signs in there with a subscription, such as claude login or Codex’s ChatGPT sign-in. To use a subscription instead, remove the key with cloudex keys rm.
Everything else that reads those variables, such as the providers’ SDKs, works the same way, and so does curl with the placeholder in its header. Google’s key also works as a ?key= parameter, as older clients send it.
How it works
When a sandbox starts while a provider has a key:
- Cloudex turns on Cloudflare’s HTTPS interception for the API hosts of the providers with a key, from the table above, and nothing else. The sandbox trusts the interception certificate: it’s added to the system’s trusted certificates, and
NODE_EXTRA_CA_CERTS,SSL_CERT_FILEandREQUESTS_CA_BUNDLEpoint at them. - Shells and SSH sessions get placeholder keys in those providers’ variables: login shells from
/etc/profile.d/cloudex-keys.sh, and every SSH session, including commands and editors’ remote servers, from/etc/environment. - Requests to the provider pass through your deployment’s API Worker on their way out. When a request carries the placeholder, or no key at all, the Worker adds the real key.
A request that carries a key of its own, such as one you set in the sandbox, goes through unchanged. So does all other traffic. If the provider rejects the stored key, the program gets an error that says so and names cloudex keys set.
The deployment keeps each key in its D1 database, encrypted with AES-GCM under a key derived from the admin key. cloudex keys and the API show only the last 4 characters; nothing returns the key itself.
On a node
A sandbox on one of your nodes gets the same placeholders, and the key is added in the same Worker, but the requests take another way out:
- When the node joins, its agent makes a certificate authority of its own, which can sign only the providers’ API host names. Its private key stays on the node. Sandboxes on the node trust it, and their
/etc/hostspoints the providers’ hosts at the agent. - The agent receives each request to a provider and passes it to your deployment, signed with the node’s key and naming the sandbox. The Worker checks it, adds the real key and sends it to the provider. The answer comes back the same way, streamed.
So the key never reaches the node. The node’s agent does see the requests, with the placeholder, and the provider’s answers in plain text. A request body over 100 MB is refused, with an error that says so. A sandbox started on Cloudflare from a save made on a node trusts only Cloudflare’s certificate again.
When changes take effect
| Change | Running sandboxes | Sandboxes started later |
|---|---|---|
| A key for a new provider | Use it after their next start | Use it |
| A changed key | Use it within a minute | Use it |
| A removed key | Lose it within a minute; the placeholder is gone after their next start | Don’t get it |
cloudex info lists the providers a sandbox uses this way, and the API and SDKs report them as providerKeys. A fork starts like any sandbox, so it uses the keys stored at that moment.
Calls to a provider count as activity for idle stop, so a sandbox stays up while its agent works, even when the agent prints nothing for a while.
Limits
- Only the providers above, at those hosts. Others, and clouds’ AI services such as Bedrock, Vertex AI or Azure OpenAI, aren’t covered; give a sandbox those keys yourself, or reach their models through OpenRouter.
- Anything in a sandbox can spend the key. It can’t read the key, but it can make calls that bill to it. So can a node, for its own sandboxes. Set a spend limit with the provider if that matters.
- HTTPS only. WebSocket connections to the provider are refused, so Codex uses its HTTPS transport.
- A program with its own list of trusted certificates, one that ignores the system’s list and the variables above, can’t reach the provider while a key is set. Docker containers in the sandbox are one example: they have their own.
- The keys are tied to the admin key. After
cloudex deploy --yesreplaces the admin key, Cloudex can’t read the stored keys. Set them again.