Skip to main content

Overview

Agents in an E2B sandbox often need API keys. Prompt-injected code can read a key from the sandbox and send it anywhere. With Formal, the API key never enters the sandbox:
  1. A custom E2B template installs the headless in Transparent Mode.
  2. The Endpoint runs as root and logs in with a Formal machine user API key. The agent runs as the default user.
  3. A network rule forwards requests for the API hostname to a .
  4. A on the Connector adds the API key. and logs apply to every request.
  5. E2B’s egress controls allow only Formal and the Connector.
The agent keeps calling the real API hostname, such as api.stripe.com, with no proxy settings.
This guide uses the Stripe API with a bearer key. The same setup works for any HTTP API and every Native User type.

Machine user API key

The Endpoint logs in with FORMAL_API_KEY. Your orchestrator passes it only to the Endpoint’s process when it starts the sandbox. To scope it:

E2B features used

Steps

1. Store the key on the Connector

Set STRIPE_API_KEY in the Connector’s environment. Remove the key from any E2B template or environment variable that the agent can use.

2. Create the resource, Native User, and listener

Verify: stripe appears under the Connector’s Reachable Resources.

3. Create the machine user and API key

  1. Go to Users, click Create User, and choose Machine. Name it e2b-agents.
  2. Create an API key for it. Store it in your orchestrator as FORMAL_E2B_API_KEY.
Copy the machine user’s user_... ID for the next step.

4. Forward API traffic to the Connector

Linux Endpoints ignore pre_tcp, so pre_tls checks the hostname and user.

5. Build the Endpoint template

Copy the Ubuntu/Debian (x86, headless) package link from the Control Plane (Download > Linux).
Verify: formal-endpoint appears under Templates in the E2B dashboard.

6. Run an agent

formal ca trust adds the Endpoint’s certificate authority to the system trust store. NODE_EXTRA_CA_CERTS points Node.js at the same bundle. Run your agent as the default user with the same envs. Verify: the script prints a Stripe JSON response.

7. Verify the key stays out

Run these as the default user:
In Logs, each request shows the e2b-agents machine user and the stripe resource.

Next Steps

Modal

The same pattern with a Modal Sidecar and OIDC

Daytona

The same pattern with linked Daytona sandboxes