---
title: "Secret injection: API keys a sandbox never holds · Pethost"
description: "Bind an API key to a variable: the sandbox sees a placeholder, and the egress proxy puts the real key in HTTPS request headers, only to the hosts you name."
---

[Agent Sandboxes](https://pethost.dev/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.

Inside the sandbox the API key is a placeholder; the egress proxy puts the real key into the request to api.anthropic.com, and a request to another host keeps the placeholder.

## 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](https://pethost.dev/sandboxes/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](https://pethost.dev/sandboxes/egress-allowlist/) must allow every name of a secret it binds, and the panel refuses settings that do not. The default, `*`, allows them all.

Python

CLI

```
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](https://pethost.dev/docs/sandboxes/secrets-and-egress/) 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](https://pethost.dev/docs/sandboxes/secrets-and-egress/)

## 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

- [A sandbox’s connections pass a gate that allows pypi.org and *.github.com and refuses example.com with 403; a DNS question for a name not on the list gets REFUSED, and UDP does not leave. Egress you allow, by name List the domains, wildcards, IPs and CIDRs a sandbox may reach; its DNS answers nothing else, no UDP leaves, and a change applies at once. How it works](https://pethost.dev/sandboxes/egress-allowlist/)
- [A prompt to a coding agent in a sandbox, and its tool calls streaming back: it reads a file, edits it, runs the tests, and the turn ends done with its token count. Coding agents, installed Claude Code, Codex and OpenCode run in every sandbox at full autonomy: one call gives one a prompt, another streams each tool call it makes. How it works](https://pethost.dev/sandboxes/coding-agents/)

## Give your agent a computer in 0.15 s

[Start the free trial](https://console.pethost.dev/welcome?kind=sandboxes)

Pethost · Stephan Gomer · NIP 1133096755

Pethost

- [Templates](https://pethost.dev/templates/)
- [About](https://pethost.dev/about/)
- [Docs](https://pethost.dev/docs/)
- [Blog](https://pethost.dev/blog/)
- [API](https://pethost.dev/docs/api/)
- [CLI](https://pethost.dev/docs/cli/)
- [Plugin](https://github.com/pethost-dev/plugin)
- [Terms](https://pethost.dev/terms/)
- [Privacy](https://pethost.dev/privacy/)
- [Refunds](https://pethost.dev/refunds/)
- [support@pethost.dev](mailto:support@pethost.dev)

Databases

- [Postgres](https://pethost.dev/postgres/)
- [Valkey](https://pethost.dev/valkey/)

Agent Sandboxes

- [Sandboxes](https://pethost.dev/sandboxes/)
- [Pricing](https://pethost.dev/sandboxes/#pricing)
- [Docs](https://pethost.dev/docs/sandboxes/)
- [API](https://pethost.dev/docs/sandboxes/api/)

Comparisons

- [Coolify vs Dokploy vs CapRover vs Dokku](https://pethost.dev/blog/coolify-vs-dokploy-vs-caprover-vs-dokku/)
- [Coolify Cloud vs Dokploy Cloud](https://pethost.dev/blog/coolify-cloud-vs-dokploy-cloud/)
- [Railway vs Render vs Fly.io pricing](https://pethost.dev/blog/railway-vs-render-vs-fly-io-pricing/)
- [Managed Docker Compose hosting](https://pethost.dev/blog/managed-docker-compose-hosting/)

Alternatives

- [Railway alternatives](https://pethost.dev/blog/railway-alternatives/)
- [Heroku alternatives](https://pethost.dev/blog/heroku-alternatives/)
- [Dokploy alternatives](https://pethost.dev/blog/dokploy-alternatives/)
- [Coolify alternatives](https://pethost.dev/blog/coolify-alternatives/)
- [Render alternatives](https://pethost.dev/blog/render-alternatives/)
- [Sliplane alternatives](https://pethost.dev/blog/sliplane-alternatives/)
