---
title: "Authenticate E2B sandboxes with workload identity federation"
description: "Let code in E2B sandboxes call Limrun with the sandbox's identity instead of an API key."
sidebar:
  label: "E2B"
  order: 6
---

E2B gives each sandbox a workload identity JWT but never shows it to code inside the sandbox. Its egress proxy adds the JWT to outbound requests you configure. You set up Limrun to trust E2B's tokens, then create sandboxes whose proxy hands the token to Limrun's token service, so no Limrun API key enters the sandbox.

## Before you start

You need:

- The `admin` role in your Limrun organization.
- Your E2B project ID. Every E2B token's subject has the form `spiffe://id.e2b.dev/<project-id>/<sandbox-id>/<execution-id>`, and the `project_id` claim carries the same ID.
- A [CLI or SDK version](/access/workload-identity-federation#configure-your-workload) with workload identity support in the sandbox.

## Create the provider

E2B signs tokens for all of its customers, so the provider condition must check that a token belongs to your project.

1. Open [console.limrun.com](https://console.limrun.com), go to **Settings**, then **Workload identity federation**, and click the **+** button next to **Identity providers**.
2. In **Issuer**, choose **E2B**. The preset fills in the issuer `https://id.e2b.dev` and its signing keys at `https://id.e2b.dev/.well-known/jwks.json`.
3. Keep **Token audience** as `limrun`. The sandbox requests its token with this audience.
4. In **Provider condition (CEL)**, replace `PROJECT_ID` with your project ID:

   ```text
   assertion.sub.startsWith('spiffe://id.e2b.dev/PROJECT_ID/')
   ```

5. Click **Create provider**. Limrun fetches E2B's signing keys before it saves the provider.

## Create a binding

1. In the provider's card, click the **+** button next to **Bindings**.
2. Enter a name and a **Claim condition (CEL)**. `true` admits every sandbox in your project. E2B's discovery document also lists `sandbox_id`, `execution_id`, and `template_id` claims. Decode a token from one of your sandboxes to see their values before you narrow the condition with them.
3. Enter the permissions the sandboxes need, one per line, such as `xcode:*:all`. To give each sandbox its own assets or instances, put its ID in a name target. See [Give each sandbox its own instance](#give-each-sandbox-its-own-instance).
4. Click **Create binding**, expand **Setup**, and copy the binding ID.

## Create a sandbox that sends its token to Limrun

When you create the sandbox, define a workload token with the audience `limrun` and a network rule that adds it to requests for `sts.limrun.com` in the `X-Limrun-Subject-Token` header:

```ts TypeScript
import { Sandbox, Secret } from 'e2b';

const sandbox = await Sandbox.create({
  iam: {
    tokens: { limrun: Secret.iamToken({ audience: 'limrun', tokenType: 'JWT-SVID' }) },
  },
  network: {
    rules: {
      'sts.limrun.com': [
        { transform: ({ iam }) => ({ headers: { 'X-Limrun-Subject-Token': iam.tokens.limrun } }) },
      ],
    },
  },
});
```

```python Python
from e2b import Sandbox, Secret

sandbox = Sandbox.create(
    iam={
        "tokens": {"limrun": Secret.iam_token(audience="limrun", token_type="JWT-SVID")},
    },
    network={
        "rules": {
            "sts.limrun.com": [
                {"transform": lambda ctx: {"headers": {"X-Limrun-Subject-Token": ctx.iam.tokens["limrun"]}}},
            ],
        },
    },
)
```

`iam.tokens.limrun` is a placeholder. E2B's proxy replaces it with a signed token on the way out, so the value never passes through code in the sandbox. The rule adds the token without restricting other traffic.

To restrict the sandbox's egress to Limrun, allow these hosts and deny the rest. E2B rejects an `allowOut` list of domains unless `denyOut` covers all other traffic:

```ts TypeScript
network: {
  allowOut: ['*.limrun.com', '*.limrun.net', '*.r2.cloudflarestorage.com'],
  denyOut: ({ allTraffic }) => [allTraffic],
  rules: {
    'sts.limrun.com': [
      { transform: ({ iam }) => ({ headers: { 'X-Limrun-Subject-Token': iam.tokens.limrun } }) },
    ],
  },
},
```

```python Python
network={
    "allow_out": ["*.limrun.com", "*.limrun.net", "*.r2.cloudflarestorage.com"],
    "deny_out": lambda ctx: [ctx.all_traffic],
    "rules": {
        "sts.limrun.com": [
            {"transform": lambda ctx: {"headers": {"X-Limrun-Subject-Token": ctx.iam.tokens["limrun"]}}},
        ],
    },
},
```

`*.limrun.com` covers the token service and the API, and `*.limrun.net` covers the hosts that serve each instance. Signed URLs for uploading and downloading assets point at Cloudflare R2, so `lim asset push`, `--install`, and other file transfers also need `*.r2.cloudflarestorage.com`. Add any hosts your own code needs, such as a package registry.

## Run lim in the sandbox

Inside the sandbox, tell the CLI to rely on the injected token and point Node at the certificate authority E2B's proxy uses:

```bash
export LIMRUN_WORKLOAD_IDENTITY_TOKEN_INJECTED=true
export LIMRUN_WORKLOAD_IDENTITY_BINDING_ID=binding_REPLACE_WITH_BINDING_ID
export NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt
lim whoami
```

`lim whoami` prints your organization and the binding's permissions. E2B's proxy decrypts traffic to `sts.limrun.com` with its own certificate authority, which E2B adds to `/etc/ssl/certs/ca-certificates.crt`. Node reads `NODE_EXTRA_CA_CERTS` only at startup, so set it before you start the CLI or your application. For the Python SDK, set `SSL_CERT_FILE` to the same file instead. The Go SDK uses the system bundle.

The same variables configure the SDKs, or pass `workloadTokenInjected`, `workload_token_injected`, or `TokenInjected` explicitly. Each `lim` command, and each SDK renewal, exchanges a token again, and E2B's proxy attaches a valid token each time.

## Give each sandbox its own instance

A coding agent in a sandbox usually needs one device of its own, and no other sandbox should reach it. Grant a folder named after the sandbox in the binding:

```text
ios:name/e2b/${assertion.sandbox_id}/*:all
```

Every sandbox receives this permission with its own `sandbox_id` filled in. Saving it requires `ios:*:all`. The same works for `android`, `xcode`, `gradle`, and `macos`.

Inside the sandbox, `lim whoami` shows the folder this sandbox received:

```bash
lim whoami
```

Its `permissions` list holds `ios:name/e2b/<sandbox-id>/*:all`, with the sandbox's ID in place of `<sandbox-id>`.

To start the sandbox's device, or get it back if it is already running, create it with `--reuse-if-exists` and a label:

```bash
lim ios create --reuse-if-exists --label role=agent
```

The token holds exactly one iOS folder, so Limrun names the new instance inside it, `e2b/<sandbox-id>/<instance-id>`. On later runs in the same sandbox, the same command returns that instance instead of starting another. A different sandbox holds a different folder, so it can neither see this instance nor reuse it.

The folder isolates sandboxes from each other, but it does not cap how many instances one sandbox starts. A create without `--reuse-if-exists`, or with different labels, starts another instance in the same folder, and Limrun has no per-sandbox limit to stop it.

To give the instance the sandbox's exact name instead of a folder, grant `android:name/e2b/${assertion.sandbox_id}:all` without the trailing `/*`. E2B sandbox IDs are lowercase letters and digits, such as `i7ukmivelrxcz505lstke`, so they are valid instance names without changes. A create from the sandbox must then set `metadata.name` to `e2b/<sandbox-id>` in the API request. The CLI does not set instance names, and a create without one returns HTTP `400`. With `reuseIfExists=true`, that create returns the sandbox's running instance by its exact name, with no labels needed.

Two sandboxes, A and B, that share one binding with `asset:name/e2b/${assertion.sandbox_id}/*:all` and `android:name/e2b/${assertion.sandbox_id}:all` get these results from sandbox A, and the mirror image from B:

| Request from sandbox A | Result |
| --- | --- |
| Exchange, then `GET /v1/whoami` | `asset:name/e2b/<A>/*:all` and `android:name/e2b/<A>:all`, with A's sandbox ID filled in |
| Create the asset `e2b/<A>/probe.txt` | `200` |
| Create the asset `e2b/<B>/probe.txt` | `403` |
| Create an Android instance named `e2b/<A>` with `reuseIfExists=true`, twice | `200` both times, with the same instance ID |
| Create an Android instance named `e2b/<B>` | `403` |

A name, like a folder, does not limit the count: a create without `reuseIfExists` starts another instance with the same name.

## Troubleshoot

| Error | Cause |
| --- | --- |
| `invalid_request` | The exchange reached Limrun without a token. Check that the rule's host is exactly `sts.limrun.com` and that `allowOut` includes it. |
| `invalid_grant` | Limrun rejected the token or the binding. Check the binding ID, that the token's audience matches the provider's, and that the project ID in the provider condition is correct. |
| A certificate error | Node does not trust E2B's proxy. Set `NODE_EXTRA_CA_CERTS` before the process starts. |

See [E2B workload identity](https://docs.e2b.dev/iam/workload-identity) for the E2B side of the setup.
