> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# CI and API keys

> Use sandboxes from CI with a scoped, expiring API key.

## Create a key for CI

Create keys in the [dashboard](https://compute.anonalabs.com) under **API keys** (owners and admins). Give CI its own key, with the least access it needs and an expiry:

| Preset | Scopes | For |
| - | - | - |
| Read only | `sandboxes:read`, `templates:read`, `usage:read` | Dashboards, monitoring, cost reports |
| Use existing | `sandboxes:read`, `sandboxes:use` | Driving sandboxes someone else created |
| Create and use | `sandboxes:read`, `sandboxes:use`, `sandboxes:create`, `sandboxes:delete`, `templates:read` | CI jobs that create and clean up their own sandboxes |
| Full access | `sandboxes:*`, `templates:*`, `usage:read`, `billing:read` | Trusted automation |
| Custom | Any of `sandboxes:read`, `sandboxes:use`, `sandboxes:create`, `sandboxes:delete`, `templates:read`, `templates:write`, `usage:read`, `billing:read`, `integrations:manage`, `vault:bind` | `integrations:manage` (webhooks, telemetry export) and `vault:bind` are only in custom keys |

Options on every key:

* **Expiry**: 7, 30, 90 or 365 days, or never.
* **Sandbox list**: limit the key to specific sandbox IDs. Other sandboxes answer "not found" to it.

`vault:bind` lets a key make new [credential vault bindings](/docs/concepts/networking.md#bindings-and-who-can-make-them): create a sandbox, or update its network, with a header rule that sends a stored secret to a host. No preset has it, not even Full access, because a key with it can send any of your secrets to any host on that secret's allowed hosts. It does nothing on a key with a sandbox list.

An **unrestricted API key** has no scopes and no sandbox list. It may do everything an API key can, including change the team egress settings and the credential vault, which a scoped key may not. Keys you create on the dashboard always have scopes, so they are never unrestricted.

The secret is shown once. Revoking a key stops it at once. Keys created before the `usage:read` and `billing:read` scopes existed do not have them. Key management is a dashboard action: API keys cannot create, list or revoke keys (the `/v1/api-keys` routes accept dashboard sessions only, see the [API reference](/docs/api-reference/introduction.md)).

## Use it in a job

Store the key as a CI secret and name it `ANONA_API_KEY`. The workflow below uses the stock `e2b` SDK, so it maps the secret to `E2B_API_KEY` and sets `E2B_DOMAIN`:

```yaml
# .github/workflows/sandbox.yml
name: sandbox
on: [push]
jobs:
  test-in-sandbox:
    runs-on: ubuntu-latest
    env:
      E2B_DOMAIN: compute.anonalabs.com
      E2B_API_KEY: ${{ secrets.ANONA_API_KEY }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: pip install e2b
      - run: python ci/run_in_sandbox.py
```

A job script that always cleans up, even when the test fails:

```python run_in_sandbox.py
import sys
from e2b import CommandExitException, Sandbox

with Sandbox.create("base", timeout=900, metadata={"ci": "true"}) as sbx:  # deleted on exit
    sbx.files.write("/home/user/test.sh", "set -e\necho running tests\ntest 2 -gt 1\n")
    try:
        r = sbx.commands.run("bash /home/user/test.sh", timeout=600)
        print(r.stdout)
    except CommandExitException as e:
        print(e.stdout, e.stderr, file=sys.stderr)
        sys.exit(e.exit_code)
```

```bash run_in_sandbox.sh
# anona-compute run creates, runs, and deletes the sandbox (also on failure or Ctrl-C),
# and exits with the command's exit code.
anona-compute run -t base --timeout 15m --metadata ci=true -- 'echo running tests && test 2 -gt 1'
```

Set a timeout on every CI sandbox, so a cancelled job cannot leave one running. Tag CI sandboxes with metadata, then sweep leftovers:

```bash sweep.sh
anona-compute list -s all --metadata ci=true --json | jq -r '.[].sandbox_id' | while read -r id; do
  anona-compute delete "$id"
done
```

## The CLI with a key

The CLI takes `--api-key`, else `ANONA_API_KEY`, else the profile (other accepted names are in [Configuration](/docs/cli/configuration.md)); a key wins over a browser sign-in. To keep one in a profile:

```bash key_profile.sh
printenv ANONA_API_KEY | anona-compute --profile ci login --with-api-key
anona-compute --profile ci whoami
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.