The first API key most teams create for a new service is the one with every permission, made in a hurry to get a demo working. Six months later it is in three CI pipelines, a staging server and a teammate’s shell history, and nobody remembers which of them actually needed which permission.
For a service that runs untrusted code, that key is worth more to an attacker than most. This post goes through how to scope keys for sandbox access: what each permission covers, which ones common jobs need, and how expiry and rotation fit in. The specifics are Sandscale’s, from the authentication docs and the Sandboxes API reference, but the reasoning carries over to any provider.
The rules the server enforces
Some of the hygiene is not optional on Sandscale, which helps. Per the authentication docs:
- A key acts as you, with the permissions you pick. It can only hold permissions you hold yourself when you create it; asking for more is refused with
403 scope_exceeds_creator. - Every key expires.
expires_atis required, at most 90 days ahead. - There is a cap. You can hold up to 10 live keys; one more is refused with
409 personal_key_limit. - Only a person manages keys. Keys are created, rotated and revoked from your own signed-in session. An API key cannot create or rotate a key (
403 person_required). - The secret is shown once. The answer contains the key, starting with
nia_, a single time. Store it in your secret manager straight away.
The 90-day limit means a forgotten key dies on its own. The person-only rule means a leaked key cannot mint a replacement for itself. Both close holes that teams usually only discover in an incident review.
What each sandbox permission covers
The API reference lists a permission for every endpoint. Grouped by what a job does, the sandbox ones look like this:
| Permission | What it allows (from the reference) |
|---|---|
sandboxes:create |
Start a sandbox (POST /instances), see launch options and the quota |
sandboxes:use |
List and read sandboxes, run commands, read and write files, upload a project, stream logs, read run records, take and list snapshots |
sandboxes:manage |
Stop (DELETE /instances/{id}), pause and resume a sandbox, delete a template |
sandboxes:read |
Read the organization’s sandbox policy and kill switch state, template details |
sandboxes:write |
Change sandbox policy, create templates, approve or reject artifacts |
A few things stand out. Running code and moving files sit under use, not create, so a component that only drives existing sandboxes does not need to start new ones. Changing policy is a separate permission, write, which no automated job should hold. And reading policy is read, which a job needs only if it wants to check limits before trying.
Key plans for common jobs
A CI job that runs tests in a sandbox
It needs to start a sandbox, put code in, run commands, read results and stop it. The run code guide asks for sandboxes:create and sandboxes:use. The reference lists stopping under sandboxes:manage, so if your job stops its own sandboxes explicitly, check whether your organization requires that permission as well; if a stop is refused, the sandbox still ends when its lifetime runs out.
Two practical choices make this key safer:
- Always set a lifetime on create (
ttl_secondsin REST,timeoutSecondsin the SDK; the server default is 3600 seconds). A job that crashes or loses permission to stop still leaves nothing running for long. - One key per pipeline, named after it. When a key shows up somewhere it should not, the name tells you which pipeline to look at, and revoking it breaks one thing.
An agent backend that hands sandboxes to a model
The same two permissions, create and use, usually cover it. What changes is where the key lives: on the server that calls the API, never inside the sandbox and never in a prompt. The model asks your backend to run something; your backend makes the call with its key. If the key were in the sandbox, the code you are trying to contain could read it.
A dashboard or monitor
Read-only views need sandboxes:use for listing sandboxes and reading run records. If it shows the kill switch state or policy, add sandboxes:read. It needs neither create nor manage.
An admin script that changes policy
sandboxes:write belongs to a person, not a key in a pipeline. Changes like widening a policy also ask you to confirm it is you with a passkey, authenticator app or emailed code, and the API answers 403 elevation_required until you do. Make those changes from the console.
Creating a scoped key
Keys are created from your signed-in session, in the console under your account settings, or with the API using your own access token. The docs show the request:
curl -X POST https://api.sandscale.dev/api/v1/account/api-keys \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "ci-tests-web", "scopes": ["sandboxes:create", "sandboxes:use"], "expires_at": "2027-01-31T00:00:00Z"}'
Give it a name that says where it is used, the smallest scope list that works, and an expiry that matches how often you are willing to rotate.
Rotation without downtime
POST /api/v1/account/api-keys/{id}/rotate issues a new key with the same name and scopes. Pass overlap_seconds to keep the old key working for a while, so you can roll the new one out to every place that uses it before the old one stops. Rotation asks you to confirm it is you, so do it from the console.
A rotation routine that works:
- Rotate with an overlap long enough to update every consumer, for example an hour.
- Update the secret in your CI or secret manager.
- Trigger a run in each consumer and confirm it works with the new key.
- Let the overlap expire.
GET /api/v1/account/api-keys lists your keys with their state (active, expired or revoked) and never returns a secret, so it is safe to use for an inventory check. DELETE /api/v1/account/api-keys/{id} revokes a key at once, which is the first move when one leaks.
When a key leaks
Revoke first, investigate second. Then use the audit log to see what the key did between the leak and the revocation: every action in your organization is recorded with who did it, when and where. A narrowly scoped key makes this short. A key that could only start and use sandboxes could not have changed your policy, and the log will confirm it.
Agents without keys
For coding agents connected over MCP, there may be no key at all. Per the authentication docs, an agent connected through Sandscale’s MCP server acts with your sign-in, so it gets no credential of its own and can do only what you can. That keeps long-lived secrets out of the agent’s configuration entirely, which is the best kind of key hygiene: the key that does not exist.
A checklist
- One key per pipeline or service, named for where it is used.
sandboxes:createandsandboxes:usefor jobs that run code; nothing more unless a call is refused.- No
sandboxes:writeon any automated key. - A lifetime on every sandbox the key starts.
- Expiry well under the 90-day maximum, with a rotation routine that uses overlap.
- Keys on servers only, never inside sandboxes or prompts.