English
Get started

Agent Sandboxes Keys that never enter

API keys the sandbox never holds

An agent needs your API key to call its model, and nothing in the sandbox needs to see the key itself. With secret injection, the sandbox holds a placeholder, and the egress proxy puts the real key into each request on its way out, only to the hosts you name.

How secret injection works

You set a secret once for the workspace: a name, a value and the hosts it is for, as in set_secret("anthropic", value=…, hosts=["api.anthropic.com"]). A sandbox binds it to an environment variable, EnvVar(name="ANTHROPIC_API_KEY", secret="anthropic"). Inside the sandbox the variable holds a placeholder that begins with phs_, and programs read it as they would read the key.

When a request leaves for one of the secret’s hosts, the egress proxy terminates its TLS with a certificate authority of that server, which the sandbox trusts: Node, Python, curl and git are pointed at it. The proxy replaces the placeholder with the value in every request header, inside Authorization: Basic credentials too, so Bearer tokens, Basic credentials and x-api-key all work. Then it sends the request on to the host over TLS. A request to any other host carries the placeholder unchanged, which is no key anywhere else.

This is how the coding agents in a sandbox sign in without holding your key: bind ANTHROPIC_API_KEY or OPENAI_API_KEY to a secret. The sandbox’s egress events name the secrets swapped into each host’s requests.

Where the value lives

The panel keeps the value sealed with AES-GCM, and no call returns it. Servers hold it in memory only, where the workspace has sandboxes. It never enters a sandbox, so it is in none of its files or processes, and no template or fork of it can carry it: they carry the placeholder. Set a new value, and running sandboxes and their forks use it at once. A secret’s hosts change with its value, in the same call, and a secret cannot be deleted while a sandbox that binds it exists.

A secret’s hosts are domains or *.domain, with an optional port, 443 by default. The sandbox’s egress allow-list must allow every name of a secret it binds, and the panel refuses settings that do not. The default, *, allows them all.

import os

from pethost.sandboxes import EnvVar, Environment, SandboxSettings, Sandboxes

sandboxes = Sandboxes()  # reads PETHOST_TOKEN
sandboxes.set_secret("anthropic", value=os.environ["ANTHROPIC_API_KEY"], hosts=["api.anthropic.com"])

variable = EnvVar(name="ANTHROPIC_API_KEY", secret="anthropic")
sandbox = sandboxes.create_sandbox(settings=SandboxSettings(environment=Environment(variables=[variable])))

print(sandboxes.run_command(sandbox.sandbox_id, command="echo $ANTHROPIC_API_KEY").stdout)
# phs_… : the placeholder
models = (
    "curl -s -o /dev/null -w '%{http_code}' https://api.anthropic.com/v1/models"
    " -H \"x-api-key: $ANTHROPIC_API_KEY\" -H 'anthropic-version: 2023-06-01'"
)
print(sandboxes.run_command(sandbox.sandbox_id, command=models).stdout)
# 200: the proxy sent the real key
printenv ANTHROPIC_API_KEY | pethost sandbox secret set anthropic --host api.anthropic.com   # the value from stdin
pethost sandbox new --name agent --env ANTHROPIC_API_KEY=secret:anthropic
pethost sandbox run agent -- 'echo $ANTHROPIC_API_KEY'                                       # phs_…

What it covers, and what it does not

  • Request headers only. A key that an API takes in a request body or in its URL is not swapped yet.
  • HTTPS through the sandbox’s trust. Between the sandbox and the proxy, a secret’s host speaks HTTP/1.1, so a client that offers only HTTP/2 fails its handshake. A program with a certificate store of its own must be pointed at the system’s.
  • Up to 100 secrets a workspace, each named with a-z, 0-9 and hyphens.
  • The key still works for the sandbox. A program in it calls the secret’s hosts with your key, which is the point. It cannot read the key, or send it to another host.

The secrets and egress guide has every rule.

How it compares

On 21 November 2026, boat.dev’s AI credentials proxy cost $1 for each day it is on, plus $1 per 1,000 requests. E2B injected secrets with its egress proxy, and Modal’s Outbound Policy, which does the same for HTTPS requests, was in beta. Pethost’s secrets cost nothing, in every plan and the trial.

Read the docs

Questions

With secret injection, can the agent read the key?

No. Wherever it looks, in the environment, in files or in memory, it finds the placeholder. The key is added to a request only after the request has left the sandbox.

Which headers are swapped?

Every request header’s value, Authorization included, and inside Basic credentials too.

Are request bodies swapped?

Not yet: headers only. An API that takes its key in a body or a URL needs the key in the sandbox for now.

What do other hosts receive?

The placeholder, unchanged.

What does a secret cost?

Nothing. A workspace keeps up to 100 of them.

Related

Give your agent a computer in 0.15 s

Start the free trial