Managed credential injection
Store API keys in Agent Identity and let the egress gateway inject the real key into outbound requests, so the key never enters the sandbox.
Overview
Many services use API key authentication, such as model services, search services, and internal enterprise APIs.
Do not write real keys into sandbox code. After a key is hosted in Agent Identity, code in the sandbox sends requests without an authentication header. When a request leaves the sandbox, the egress gateway injects the real key. The key never enters the sandbox.
Read Credential injection first to understand the overall framework.
Use cases
This method applies to destination services that use API key authentication. Common authentication header formats:
-
Authorization: Bearer <api-key> -
Authorization: Token <api-key> -
X-API-Key: <api-key>
If the key is a static value that you hold, configure it directly with full header injection in Inject your own credentials. Both methods inject the key in the same way, and only the storage location of the key differs.
Prerequisites
Complete the following steps in the Agent Identity console:
-
Create a Workload Identity and associate it with an available Resource Access Management (RAM) role. If no role is associated, the creation fails;
-
Grant the role the
agentidentitydata:GetResourceAPIKeypermission. Without this permission, the API key cannot be obtained; -
Create an API key credential provider and store the real API key in it;
-
Allow the Workload Identity to use this credential provider;
-
Confirm the destination service's domain, header name, and authentication format.
Configure the sandbox
In the example, my-agent is the Workload Identity that you created in the prerequisites, model-provider is the name of the API key credential provider, and api.example.com is the domain of the destination service. The example injects the API key model-provider stored in Agent Identity into outbound requests to api.example.com, using the header Authorization:
Node.js
import { Sandbox } from "e2b";
const sandbox = await Sandbox.create("code-interpreter-v1", {
metadata: {
"fc.sandbox.identity.name": "my-agent",
},
network: {
allowOut: ["api.example.com"],
denyOut: ["0.0.0.0/0"],
rules: {
"api.example.com": [{
transform: {
headers: {
Authorization: "Bearer ${fc.sandbox.identity.apiKey:model-provider}",
},
},
}],
},
},
});
Python
from e2b import Sandbox
sandbox = Sandbox.create(
template="code-interpreter-v1",
metadata={"fc.sandbox.identity.name": "my-agent"},
network={
"allow_out": ["api.example.com"],
"deny_out": ["0.0.0.0/0"],
"rules": {
"api.example.com": [{
"transform": {
"headers": {
"Authorization":
"Bearer ${fc.sandbox.identity.apiKey:model-provider}"
}
}
}]
},
},
)
The reference expression ${fc.sandbox.identity.apiKey:<provider-name>} points to an API key in Agent Identity, and can be concatenated with fixed prefixes such as Bearer and Token .
Verify credential injection
Request the destination service directly in the sandbox, without setting an authentication header:
curl -i https://api.example.com/v1/inference \
-H 'Content-Type: application/json' \
-d '{"input":"hello"}'
When the request leaves the sandbox, the egress gateway injects Authorization: Bearer <real API key>. If an SDK requires a key, use a placeholder value. The egress gateway overwrites the corresponding header.
Update the configuration
While the sandbox is running, use updateNetwork to change the credential provider or modify the injection rules. For more information, see Update credential configuration.
Limits
For the common limits and security requirements, see Credential usage limits.