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:- A custom E2B template installs the headless in Transparent Mode.
- The Endpoint runs as
rootand logs in with a Formal machine user API key. The agent runs as the defaultuser. - A network rule forwards requests for the API hostname to a .
- A on the Connector adds the API key. and logs apply to every request.
- E2B’s egress controls allow only Formal and the Connector.
api.stripe.com, with
no proxy settings.
Machine user API key
The Endpoint logs in withFORMAL_API_KEY. Your orchestrator passes it only to
the Endpoint’s process when it starts the sandbox. To scope it:
- Create a dedicated machine user and API key for E2B.
- Never bake the key into a template.
- Restrict the machine user’s Control Plane access.
- Rotate the key on a schedule.
E2B features used
Steps
1. Store the key on the Connector
SetSTRIPE_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
stripe appears under the Connector’s Reachable Resources.
3. Create the machine user and API key
- Go to Users, click Create User, and
choose Machine. Name it
e2b-agents. - Create an API key for it. Store it in your
orchestrator as
FORMAL_E2B_API_KEY.
user_... ID for the next step.
4. Forward API traffic to the Connector
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).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 defaultuser:
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