Skip to the content
Forge

Service accounts and API keys

Everything the console does goes through Forge's API, and you can use it too: from the forge command line, a script or a CI job. There are two kinds of credentials:

  • A personal API key acts as you, with your roles.
  • A service account token acts as a project's service account, with that account's role. It keeps working when people leave, so use it for CI, scripts and anything shared.

Personal API keys

Your sign-in, profile and API keys are managed by the sign-in service, not by Forge. Open the user menu, API keys, and create a key for Forge there. Personal keys start with ak_. When you create one, pick the scopes it needs. For the run commands of forge that is project:read, run:read, run:create and run:cancel.

Then log in from your terminal:

forge login --server https://forge.example.com

forge login reads the key from standard input (hidden on a terminal), so it stays out of your shell's history. It checks the key and keeps it for the next commands. forge whoami shows who you are logged in as. forge logout forgets the kept key, but the key stays valid until it expires or you revoke it at the sign-in service.

In scripts, set the environment instead:

export FORGE_SERVER=https://forge.example.com
export FORGE_TOKEN=<your-token>
forge run alpine:3.20 echo hello

FORGE_TOKEN takes precedence over the key forge login kept. See the command line for more.

Service accounts

A service account belongs to one project. It has a role like a member and its own tokens. Manage them on the project's Service accounts page. This needs service_account:manage on the project (maintainers and up).

Create one

  1. New service account, with a Name and a Description.
  2. Pick its Role. You can only pick roles you could give someone on this project yourself, so nobody can make an account that does more than they can.
  3. Create service account.

A project can have at most 50 service accounts. Changing an account's role applies to its tokens right away. Delete removes the account and revokes all its tokens.

Only people manage service accounts. A service account's token can't create accounts or tokens.

Tokens

Open Tokens on the account, then:

  1. Give the Token name: where it is used, for example "GitHub Actions deploy".
  2. Pick when it Expires after: 30, 90 or 365 days.
  3. Optionally Limit this token to some of the role's permissions. Without a limit, the token may do what the account's role allows, also after the role changes.
  4. Create token.

The token, starting with fs_, is shown only once. Copy it and store it as a secret where it is used. The list shows each token's name, permissions, expiry and when it was Last used. Revoke ends a token at once.

  • An account can have at most 20 active tokens.
  • Through the API, a token's expiry can be anywhere between 1 hour and 1 year from now.
  • Creating and revoking tokens is in the organization's audit log, with the first characters of the token, never the token itself.

A service account token works with forge login and FORGE_TOKEN like a personal key.

Schedules run as service accounts

A schedule starts its runs as one of the project's service accounts, shown as Runs as. The account's role needs to allow starting runs. You can only pick accounts whose role you could give yourself, so a schedule can't do more than the person who set it up. When the project has none yet, pick New service account… in the schedule's form to make one there. It gets no token: a schedule doesn't need one.

Using the API

The API is at /api/v1 on your Forge server. Send the token as a Bearer token:

curl -H "Authorization: Bearer $FORGE_TOKEN" https://forge.example.com/api/v1/me
curl -H "Authorization: Bearer $FORGE_TOKEN" \
  https://forge.example.com/api/v1/projects/<project-id>/runs
  • The full API is described in an OpenAPI 3 file at /api/v1/openapi.yaml, for reading or for generating a client.
  • Projects, organizations and runs are named by their IDs in the paths.
  • Every caller is rate limited. Over the limit the API answers 429 with Retry-After, in seconds.
  • You need to accept the terms of service in the console once before your personal key works for more than /me.
  • Creating a run takes an Idempotency-Key header, so a retried request doesn't start a second run.

Good to know

  • Treat tokens and keys as passwords. Keep them in your CI's secret store, not in a repository.
  • Give a service account the smallest role that does the job, and limit its token's permissions to exactly what it needs.
  • Prefer short expiries, and replace tokens before they expire: make the new one, switch, then revoke the old one.