Skip to content
Aurabase Logo
aurabase

API keys

Choose the right key for each place your code runs, create and revoke keys from the CLI or the management API, rotate a leaked key without downtime, and recognize the errors that protect the service_role key.

How-to guide9 min readLevel beginner

Every call to the data plane of a project (https://<slug>.<domain>/v1/db/…, the SDK, the REST API) carries a project key in the apikey or X-API-Key header. The key tells the gateway which project is called and which PostgreSQL role the request runs as. Keys are created per project, shown once, stored as a SHA-256 hash, and revoked one by one.

Prerequisites#

  • A linked project: aura link <project-id>, and a console session (aura login) of an owner or admin of the organization: managing keys is refused to other members.

  • The example schema, installed with the service role key of the project:

    export AURABASE_SERVICE_ROLE_KEY=aura_sk_…
    aura db seed --example docs

    It creates profiles, orgs, org_members, posts, todos, the view open_todos and the function todo_count(p_user uuid). See Example schema.

The examples create keys named sec-keys-…, then revoke them. They never print a key: the commands pass each new key through sed to replace it with … on this page; in your terminal, the key is printed in full, once.

Three scopes#

ScopePrefixRuns asauth.uid()Where it may live
anonaura_anon_aura_anon; aura_authenticated when a user token is sent with itNULL, or the signed-in userBrowser, mobile app, anywhere. It reaches the rows that policies open to visitors, plus what row level security does not cover, such as materialized views: see What row level security does not protect
authenticatedaura_auth_aura_authenticated, without a userNULLServer code that must read what policies open to every signed-in user. It is not refused in a browser, but anyone holding it gets that access: do not publish it.
service_roleaura_sk_aura_service_role (SDK, REST), the administrator role of the project (aura db raw, schema API)NULLServers, CI, your terminal only. It bypasses row level security and can read the Auth data of the project.

A key is the prefix followed by 43 characters (32 random bytes). The first 16 characters are stored in clear as key_prefix, so you can recognize a key in a list. Two keys are created with each project, Default anon key and Default service_role key; aura projects create --json prints them once.

List the keys#

aura db api-keys list shows the active keys of the linked project, newest first, with their scope, creation date and last use; revoked keys are not listed. --json gives the full id:

aura db api-keys list --json | node -e '
  for (const k of JSON.parse(require("fs").readFileSync(0, "utf8")))
    console.log(k.name, "|", k.scope, "|", k.key_prefix.replace(/^(aura_[a-z]+_).*/, "$1…"))'
Default service_role key | service_role | aura_sk_…
Default anon key | anon | aura_anon_…

last_used_at is updated in the background on each validation that is not served from the gateway cache.

Create a key#

aura db api-keys create <name> --scope anon|authenticated|service_role creates a key (scope anon by default) and prints it once. Store it in your secret manager right away: only its hash is kept.

aura db api-keys create sec-keys-backend --scope service_role \
  | sed -E 's/aura_(anon|auth|sk)_[A-Za-z0-9_-]+/aura_\1_…/; s/ID: [0-9a-f]{8}/ID: …/'
» Creating API key 'sec-keys-backend'
  ✔ Key created: sec-keys-backend [service_role]

  ⚠ API Key (save it now — it will only be shown once):
  aura_sk_…

  • ID: …

The CLI sends a new Idempotency-Key header with each creation. The server remembers it: a request replayed with the same header and the same body returns the key already created (without its value, which is no longer available) instead of creating a second one, and the same header with another body is refused.

Rotate and revoke a key#

To replace a key, create the new one, deploy it everywhere the old one is used, then revoke the old one. Revocation takes effect at once on every replica of the gateway, which drops the key from its cache:

NEW_KEY=$(aura db api-keys create sec-keys-rotation --scope anon | grep -oE 'aura_anon_[A-Za-z0-9_-]+')
curl -s -o /dev/null -w 'before revoke: %{http_code}\n' "$AURABASE_URL/v1/db/$PROJECT_ID/todos?select=id" -H "apikey: $NEW_KEY"
aura db api-keys revoke sec-keys-rotation --force
curl -s "$AURABASE_URL/v1/db/$PROJECT_ID/todos?select=id" -H "apikey: $NEW_KEY" \
  | node -e 'const e = JSON.parse(require("fs").readFileSync(0, "utf8")).error; console.log("after revoke:", e.code, "-", e.message)'
before revoke: 200
  ✔ API key sec-keys-rotation revoked.
after revoke: API_KEY_INVALID - The API key is invalid, expired or revoked. Send your project's key in the `apikey` header (Studio › Settings › Connection).

aura db api-keys revoke accepts the full id, the first characters of the id (the 8 shown by list), or the name; when several active keys match, it refuses with AMBIGUOUS_API_KEY. Revocation cannot be undone; the key stays in the audit trail with its revocation date. Without --force, the command asks for confirmation and stops when it has no terminal.

There is no expiry date to set on a key: rotate on a schedule of your own, and right away when a key may have leaked. A service_role key that leaked must be treated like a database password: revoke it, then check what it could have reached (every row, and the Auth data of the project).

What the gateway refuses#

curl -s "$AURABASE_URL/v1/db/$PROJECT_ID/todos" -H "apikey: $AURABASE_SERVICE_ROLE_KEY" -H 'Origin: https://app.example.com' \
  | node -e 'const e = JSON.parse(require("fs").readFileSync(0, "utf8")).error; console.log(e.code, "-", e.message)'
curl -s -o /dev/null -w 'anon key with Origin: %{http_code}\n' "$AURABASE_URL/v1/db/$PROJECT_ID/todos" -H "apikey: $AURABASE_ANON_KEY" -H 'Origin: https://app.example.com'
curl -s "$AURABASE_URL/v1/db/$PROJECT_ID/todos?apikey=$AURABASE_ANON_KEY" \
  | node -e 'const e = JSON.parse(require("fs").readFileSync(0, "utf8")).error; console.log(e.code, "-", e.message)'
SECRET_KEY_IN_BROWSER - A secret key (service_role) was used from a browser: it bypasses RLS and must never be sent client-side. Use the anon (public) key in the browser and keep the service_role key on the server.
anon key with Origin: 200
UNAUTHORIZED - Unauthorized: An API key in the query string is reserved for real-time streams (WS/SSE); use the apikey or X-API-Key header.
GuardWhereWhat it checks
401 SECRET_KEY_IN_BROWSERGatewayA service_role key on a request that has an Origin header. Browsers always send Origin on cross-origin requests and a page cannot remove it; servers, curl and the SDK on a server do not send it.
Key in the URLGateway?apikey= is accepted only on real-time streams (WebSocket, SSE), and never for a service_role key (403, A service_role key must never travel in a URL.).
createClient() in a browserSDKThrows when given an aura_sk_ key in a browser or a browser worker (option refuseServiceRoleInBrowser, true by default; false only logs a warning).
createAdminClient() in a browserSDKAlways throws.

An aura_auth_ key is not refused in a browser: the guards only protect the service_role scope.

Use the service_role key on a server#

  • Read it from the environment (AURABASE_SERVICE_ROLE_KEY), never from code committed to a repository; never prefix it with NEXT_PUBLIC_ or any variable a bundler exposes to the browser.
  • Create a separate key per server or service, named after it, so you can revoke one without touching the others.
  • On a server that acts for a user, prefer the anon key with the user's token: the policies then apply. Keep the service_role key for work that must see every row (webhooks, back office, migrations).
  • A request made with the service_role key and a user token runs as that user: the token wins.

Management API#

The CLI calls the management API with the console session (Authorization: Bearer <session token>); the Studio (Settings › API keys) does the same:

Method and pathBodyAnswer
GET /v1/control/projects/{project_id}/api-keys—Active keys: id, name, key_prefix, scope, created_at, last_used_at
POST /v1/control/projects/{project_id}/api-keys{"name": "…", "scope": "anon"}, optional header Idempotency-Key (255 characters at most)The key, with key (shown once) and key_available
DELETE /v1/control/projects/{project_id}/api-keys/{id-or-name}—Revocation

The routes GET, POST /v1/db/{project_id}/api-keys and DELETE /v1/db/{project_id}/api-keys/{id} serve the same keys on the management API with the same session. A project key cannot manage keys, not even the service_role key:

curl -s -X GET "$AURABASE_URL/v1/db/$PROJECT_ID/api-keys" -H "apikey: $AURABASE_SERVICE_ROLE_KEY" \
  | node -e 'const e = JSON.parse(require("fs").readFileSync(0, "utf8")).error; console.log(e.code, "-", e.message)'
curl -s -X POST "$AURABASE_URL/v1/db/$PROJECT_ID/api-keys" -H "apikey: $AURABASE_SERVICE_ROLE_KEY" \
  -H 'content-type: application/json' -d '{"name": "sec-keys-from-project-key", "scope": "anon"}' \
  | node -e 'const e = JSON.parse(require("fs").readFileSync(0, "utf8")).error; console.log(e.code, "-", e.message)'
curl -s -X DELETE "$AURABASE_URL/v1/db/$PROJECT_ID/api-keys/00000000-0000-0000-0000-000000000000" -H "apikey: $AURABASE_SERVICE_ROLE_KEY" \
  | node -e 'const e = JSON.parse(require("fs").readFileSync(0, "utf8")).error; console.log(e.code, "-", e.message)'
FORBIDDEN - Forbidden: The owner or admin role is required to manage API keys
FORBIDDEN - Forbidden: The owner or admin role is required to manage API keys
FORBIDDEN - Forbidden: The owner or admin role is required to manage API keys

A leaked service_role key therefore cannot create other keys. Every creation and revocation is written to the audit trail of the platform, without the key.

Result#

aura db api-keys list --json | node -e '
  for (const k of JSON.parse(require("fs").readFileSync(0, "utf8"))) console.log(k.name, "|", k.scope)'
sec-keys-backend | service_role
Default service_role key | service_role
Default anon key | anon

The revoked rotation key no longer appears.

Common errors#

MessageCauseFix
401 API_KEY_INVALID, The API key is invalid, expired or revoked. …Revoked key, a key of another project, or a typing error.Check the key and the project URL; aura db api-keys list shows the active prefixes.
401 UNAUTHORIZED, An API key is required: add the apikey or X-API-Key headerNo key in the request.Send apikey: <key>.
401 SECRET_KEY_IN_BROWSERA service_role key from a browser.Use the anon key in the browser; move the call to a server.
401 UNAUTHORIZED, An API key in the query string is reserved for real-time streams (WS/SSE); …Key in ?apikey= on a REST call.Send it as a header.
403 FORBIDDEN, The owner or admin role is required to manage API keysA project key, or a console account that is not owner or admin.Use the console session of an owner or admin.
[404] API_KEY_NOT_FOUND, with a message in French in this versionUnknown or already revoked key.List the keys and use the id or the name shown.
AMBIGUOUS_API_KEYSeveral active keys match the prefix or name.Give the full id from list --json.
Error: Revoke API key …? This action is irreversible. then Not a terminal: cannot ask for confirmation.revoke in a script without --force.Add --force.

Clean up#

aura db api-keys revoke sec-keys-backend --force
  ✔ API key sec-keys-backend revoked.

See also#

@aurabase/aurabase-js@0.7.5 · aura@0.3.4↗ Report a problem