Update network configuration
While the sandbox is running, call updateNetwork (Node.js) or update_network (Python) to update the egress rules and credential injection rules at the same time, without recreating the sandbox.
When to update the network configuration
If you only adjust the egress allowlist of a running sandbox, you must also retain the required credential rules in the same update request. Conversely, when you rotate credentials, you must also resubmit the egress rules to be retained. For more information, see Network Access Control and Credential injection.
Procedure
updateNetwork uses full-replacement semantics and does not merge with the existing configuration. Each call must submit the final allowInternetAccess, allowOut, denyOut, and rules values to be retained:
-
When you update credential injection rules, submit the egress rules to be retained at the same time.
-
When you update egress rules, submit the credential injection rules to be retained at the same time.
-
Credential injection rules and egress rules that you do not submit are not retained automatically. Omitting
rules, or passingnullor{}, clears the existing credential injection rules. This does not apply to the M2M (Machine-to-Machine) options table and the user-delegated OAuth options table, which follow independent update rules. For more information, see OAuth configuration update rules.
The Python example uses Python 3.10 or later and E2B Python SDK 2.31.0. Run python3 -m pip install e2b==2.31.0 to install the SDK.
The following example updates the egress rules and the API key injection rules at the same time:
Node.js
await sandbox.updateNetwork({
allowInternetAccess: true,
allowOut: ["api.example.com"],
denyOut: ["0.0.0.0/0"],
rules: {
"api.example.com": [{
transform: {
headers: {
Authorization: "Bearer ${fc.sandbox.identity.apiKey:new-model-provider}",
},
},
}],
},
});
Python
sandbox.update_network({
"allow_internet_access": True,
"allow_out": ["api.example.com"],
"deny_out": ["0.0.0.0/0"],
"rules": {
"api.example.com": [{
"transform": {
"headers": {
"Authorization":
"Bearer ${fc.sandbox.identity.apiKey:new-model-provider}"
}
}
}]
},
})
To configure egress rules, see Network Access Control. To configure credential injection rules, see Credential injection.
Update egress configuration at runtime
After the sandbox is created, use updateNetwork (Node.js) or update_network (Python) to adjust egress rules without recreating the sandbox. The following example creates a sandbox that has no credential injection configured, and then blocks a single IP address, switches to a domain allowlist, and turns off egress. Standard network configuration is a full replacement: fields that you do not submit are cleared, so each call must submit the complete allowInternetAccess, allowOut, denyOut, and rules values. If the sandbox already has credential injection rules, submit the rules to be retained as well.
Node.js
import { Sandbox } from "e2b";
const sandbox = await Sandbox.create({
apiKey: process.env.E2B_API_KEY,
apiUrl: process.env.E2B_API_URL,
domain: process.env.E2B_DOMAIN,
});
console.log(`[create] sandbox created: ${sandbox.sandboxId}`);
try {
// Tighten: block a single IP
await sandbox.updateNetwork({
allowInternetAccess: true,
allowOut: [],
denyOut: ["8.8.8.8"],
rules: {},
});
console.log("[update] blocked 8.8.8.8");
// Replace with a pure allowlist
await sandbox.updateNetwork({
allowInternetAccess: true,
allowOut: ["api.example.com"],
denyOut: ({ allTraffic }) => [allTraffic],
rules: {},
});
console.log("[update] switched to allowlist: only api.example.com allowed");
// Directly toggle the master egress switch
await sandbox.updateNetwork({
allowInternetAccess: false,
allowOut: [],
denyOut: ["0.0.0.0/0"],
rules: {},
});
console.log("[update] master egress switch turned off");
} finally {
// Destroy the sandbox after finishing to avoid leftover usage and billing
await sandbox.kill();
console.log("[cleanup] sandbox destroyed");
}
Python
import os
from e2b import Sandbox
sandbox = Sandbox.create(
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
)
print(f"[create] sandbox created: {sandbox.sandbox_id}")
try:
# Tighten: block a single IP
sandbox.update_network({
"allow_internet_access": True,
"allow_out": [],
"deny_out": ["8.8.8.8"],
"rules": {},
})
print("[update] blocked 8.8.8.8")
# Replace with a pure allowlist
sandbox.update_network({
"allow_internet_access": True,
"allow_out": ["api.example.com"],
"deny_out": lambda ctx: [ctx.all_traffic],
"rules": {},
})
print("[update] switched to allowlist: only api.example.com allowed")
# Directly toggle the master egress switch
sandbox.update_network({
"allow_internet_access": False,
"allow_out": [],
"deny_out": ["0.0.0.0/0"],
"rules": {},
})
print("[update] master egress switch turned off")
finally:
# Destroy the sandbox after finishing to avoid leftover usage and billing
sandbox.kill()
print("[cleanup] sandbox destroyed")
Points to note:
-
updateNetworkis a full replacement and does not merge with existing rules; passing an empty objectupdateNetwork({})clears all allow and deny rules that were set at creation. -
The egress-related
allowInternetAccess/allowOut/denyOut/rulescan be updated (when the sandbox is created, the rules field is configured asnetwork.rules).allowPublicTrafficandmaskRequestHostare creation-time-locked parameters and cannot be modified after creation. For more information about how to configurerules, see Credential injection.
Usage notes
How updates take effect
-
Only running sandboxes support updates. A paused sandbox must be resumed first.
-
If the request parameters fail validation, the original configuration remains in effect. After a server error or a request timeout, the running sandbox may have already applied the new policy. First query the saved network configuration, and then send requests from the current sandbox to verify whether the target is allowed or denied as expected. To retry, resubmit the complete target configuration.
-
The network configuration is retained after the sandbox is paused and resumed, and is reapplied on the resumed instance.
OAuth configuration update rules
M2M OAuth and user-delegated OAuth are updated independently, which differs from the full replacement used for standard network configuration:
-
The M2M options form a parameter table keyed by provider name. The table is handled as a whole: omitting the field retains the original table, submitting the field replaces the entire M2M provider table, and passing
{}clears the session-level parameters of the table. Therefore, submit the other providers that you want to retain together, and also submit the scopes and customParameters to retain for the same provider. For more information, see M2M OAuth injection. -
For user-delegated OAuth, provide either a JWT (JSON Web Token) or a UserId. If you omit both, the current user identity is retained. If you submit one of them, the current identity is replaced and the other is cleared. Each submitted JWT is re-validated, even if it is the same JWT. Switching to another user identity is allowed, but the new JWT must pass validation and the required OAuth authorization must already be completed. For more information, see User-delegated OAuth injection.
Limits
-
allowPublicTrafficandmaskRequestHostcan be set only when the sandbox is created. -
The Workload Identity, Role Policy, and re-sign domains used by managed STS (Security Token Service) cannot be modified by
updateNetwork. To change them, create a new sandbox.
More operations
Query the current configuration
getInfo or GET /sandboxes/{id} returns the saved network configuration, which you can use to construct the next complete update request.
The bring-your-own credential method returns the real credential values (value) in the credential injection rules in plaintext. Do not write query responses into ordinary logs, tickets, or frontend reporting. The method managed by Agent Identity returns only reference expressions, not real credentials.