Inject your own credentials

Updated at:

Provide your own real credentials and use network.rules to inject or replace headers on outbound requests, so that real credentials never enter the sandbox.

Use cases

Code inside the sandbox needs credentials to access external services. Do not write real credentials into the environment variables or the code of the sandbox, because the code and its dependencies inside the sandbox can read them.

network.rules lets you keep the credentials on a trusted system that you control, such as your backend or continuous integration (CI) secrets. Declare the injection rules when you create the sandbox: when the sandbox accesses a specified domain, the platform injects the credential into a specified header. Code inside the sandbox sends requests as usual, and the platform injects the real credentials when the requests leave the sandbox, so the credentials never enter the sandbox.

Configure the sandbox

Method

Use case

Request sent by the sandbox

Full header injection

The trusted system that you control can build the complete header, for example Authorization: Bearer ....

The request can omit the header entirely.

Header value placeholder replacement

A third-party SDK or the code inside the sandbox must build the header itself, and only the sensitive part of it needs to be hidden.

The request carries the header structure and non-sensitive placeholders.

If both methods apply to the same header, the platform performs placeholder replacement first and then full header injection; the value from full header injection takes final precedence.

Full header injection

The trusted system that you control reads the real credential and injects it into the Authorization header when the sandbox is created.

Node.js

import { Sandbox } from "e2b";

const token = process.env.API_TOKEN;
if (!token) throw new Error("API_TOKEN is required");

const sandbox = await Sandbox.create({
  network: {
    allowOut: ["api.example.com"],   // Allow egress to this domain
    denyOut: ["0.0.0.0/0"],
    rules: {                         // Inject credentials
      "api.example.com": [{
        transform: {
          headers: { Authorization: `Bearer ${token}` },
        },
      }],
    },
  },
});
// When code inside the sandbox calls https://api.example.com, the platform injects
// the Authorization header automatically; the business code does not set it.

Python

import os
from e2b import Sandbox

token = os.environ["API_TOKEN"]

sandbox = Sandbox.create(
    network={
        "allow_out": ["api.example.com"],
        "deny_out": ["0.0.0.0/0"],
        "rules": {
            "api.example.com": [
                {"transform": {"headers": {"Authorization": f"Bearer {token}"}}}
            ]
        },
    },
)

Header value placeholder replacement

This method applies when a third-party SDK or the code inside the sandbox must build the header itself, and only the sensitive part of it needs to be hidden.

The current E2B SDK does not provide a separate replacement field. This product carries the replacement rules in a reserved header key whose value is a JSON string of the replacement array. The key name is as follows:

fc.sandbox.network.header-value-replacements

This key is used only to configure the rules and is never sent to the target service as a real header.

Node.js

import { Sandbox } from "e2b";

const token = process.env.API_TOKEN;
if (!token) throw new Error("API_TOKEN is required");

const replacements = JSON.stringify([
  { placeholder: "TOKEN_PLACEHOLDER", value: token },
]);

const sandbox = await Sandbox.create({
  network: {
    allowOut: ["api.example.com"],
    denyOut: ["0.0.0.0/0"],
    rules: {
      "api.example.com": [{
        transform: {
          headers: {
            "fc.sandbox.network.header-value-replacements": replacements,
          },
        },
      }],
    },
  },
});

Python

import json, os
from e2b import Sandbox

token = os.environ["API_TOKEN"]
carrier = json.dumps([{"placeholder": "TOKEN_PLACEHOLDER", "value": token}])

sandbox = Sandbox.create(
    network={
        "allow_out": ["api.example.com"],
        "deny_out": ["0.0.0.0/0"],
        "rules": {
            "api.example.com": [
                {"transform": {"headers": {
                    "fc.sandbox.network.header-value-replacements": carrier
                }}}
            ]
        },
    },
)

Replacement rules: Matching is case-sensitive, and all matches within the same header value are replaced. The value is scanned only once, so replaced content does not trigger further replacements. Unmatched placeholders are sent to the target service unchanged.

Verify credential injection

With full header injection, the code inside the sandbox requests the target service directly without setting an authentication header:

curl -i https://api.example.com/v1/resource

With header value placeholder replacement, the request inside the sandbox keeps the header structure and the placeholder:

curl -i https://api.example.com/v1/resource \
  -H 'Authorization: Bearer TOKEN_PLACEHOLDER'

The egress gateway injects or replaces the real credentials when the requests leave the sandbox.

Update the configuration

While the sandbox is running, use updateNetwork to change the injection rules. For the procedure, see Update credential configuration.

Limits

For the general limits and security requirements, see Credential usage limits.