Sign-In, Tokens and API Keys
Two Ways to Call the API
Section titled “Two Ways to Call the API”- A script, a pipeline or another system calls with a service account’s API key. A service account is a member of your organization that is not a person. It has roles of its own and needs no sign-in, password or authenticator code. This is the way for anything that runs unattended.
- A person signs in and calls with the short-lived access token that signing in answers. The web application works this way.
Both are sent the same way, as Authorization: Bearer …. An API key begins nbf_; an access token never does.
Service Accounts and API Keys
Section titled “Service Accounts and API Keys”An administrator of your organization makes service accounts and their keys on the Service Accounts screen: see Service Accounts. Before any key works, an administrator turns API keys on for the organization, under People, in its ways to sign in. Until then every key is refused.
A key:
- is
nbf_followed by 38 letters and digits: 32 random characters and a 6-character checksum of the rest. It has no dot, so it is never mistaken for an access token; - is shown once, when it is made. Only a keyed hash of it and its last four characters are kept, so nobody can show it to you again;
- lasts 90 days unless the administrator chooses otherwise, and never longer than 366 days, or than the shorter longest life the organization may set. There is no key without an end;
- may be limited to the addresses it is used from;
- acts as its service account, with its roles. It sees what a person with the same roles would see: a restricted model only when the service account is on its allowed list.
Call With an API Key
Section titled “Call With an API Key”# The key is kept in your pipeline's secret store and given to the script as a variable.curl -s -H "authorization: Bearer $FORGE_API_KEY" \ https://forge.example.com/api/models
# Who the key acts as: the service account, with "serviceAccount": true.curl -s -H "authorization: Bearer $FORGE_API_KEY" \ https://forge.example.com/api/meSend the key in the Authorization header only. A key in the address, as ?access_token=, is never read: addresses end up in logs.
When a Key Is Refused
Section titled “When a Key Is Refused”A refused key answers 401 with a problem document whose reason says why, and a detail to show a person:
reason |
What happened | What to do |
|---|---|---|
malformed |
It is not a key of this product: it is cut short, or a character changed. It is refused before anything is looked up | Copy the whole key again |
unknown |
No key like it was made here | Check the instance; ask an administrator for a key |
expired |
Its life is over | An administrator makes a new key |
revoked |
An administrator revoked it | An administrator makes a new key |
deactivated |
Its service account is deactivated | An administrator reactivates the account |
organizationClosed |
The organization is suspended or archived | Ask your organization’s administrators |
switchedOff |
The organization has API keys turned off | An administrator turns them on under People |
outsideAllowlist |
The call came from an address the key does not list | Call from a listed address, or ask an administrator to list it |
unavailable |
The instance’s operators have not set up API keys | Ask the instance’s operators |
Each refusal of a key that exists is written to the organization’s activity log, at most once an hour for one key and one reason, so a script left running with a dead key does not fill the log.
Each key may make 600 calls a minute unless the instance’s operators set another number. Past it the answer is 429 with Retry-After and the reason apiKey. An address that sends many unknown keys is refused for a minute, as an address that fails many sign-ins is: see Errors and limits.
Rotate a Key
Section titled “Rotate a Key”A service account has at most two live keys at once, so that one can replace the other:
- An administrator chooses Rotate the Key for the old key. The new key is shown once; the old one keeps working for the overlap chosen, up to 7 days.
- Put the new key in your secret store and run the script with it.
- The old key stops at the end of the overlap. To stop it sooner, the administrator revokes it.
What a Key Cannot Do
Section titled “What a Key Cannot Do”A service account is not a person, so these answer 403 with personOnly for a key:
- signing in and out, renewing a session, stepping up, and its own sign-in security (
/api/auth/…,/api/me/factors,/api/me/passkeys,/api/me/accounts); - drafts with the assistant (
/api/drafts), notifications (/api/me/notifications) and saved views (/api/views); - the live channel;
- support access and the administration of service accounts.
A service account is never an administrator, so the administrators’ routes refuse it too. The API reference shows, for each route, whether it takes an API key: the apiKey scheme.
A key is a password for a machine. Keep it in a secret store, never in a repository. GitHub’s secret scanning does not report this product’s keys to us, so a key pushed somewhere public is not revoked by itself: revoke it as soon as you know.
Sign In as a Person
Section titled “Sign In as a Person”POST /api/auth/sign-in with the person’s address and password:
{ "email": "someone@example.com", "password": "..." }The answer is one of two:
-
A session:
accessToken,expiresIn(seconds) anduser. The answer also sets the refresh token in a cookie,HttpOnlyand limited to/api/auth. -
A challenge, for a person with a second step:
secondFactor,challenge,expiresIn, andfactors, which liststotp,passkeyor both. Withtotp, send the challenge and a current code from the authenticator app toPOST /api/auth/second-factor:{ "challenge": "...", "code": "123456" }The answer is the session. The challenge lasts five minutes. The second step with a passkey needs a browser (below).
A wrong password or code answers 401. Repeated failures lock the account for a while, and sign-in is limited per address per minute; see Errors and limits.
An organization may turn passwords off. A right password then answers 409, with the reason in detail.
Use the Token
Section titled “Use the Token”Send Authorization: Bearer <accessToken> with every call. An access token lasts a few minutes (10 unless the instance’s operator changed it).
Renew the Session
Section titled “Renew the Session”POST /api/auth/refresh with the refresh cookie and the header X-Requested-With: forge answers a new access token and replaces the cookie. Without the header the answer is 403: the header is what tells the API the request is not a form posted from another site.
Each refresh token is used once. Using one twice ends the whole session, because it can mean the token was copied.
Sign Out
Section titled “Sign Out”POST /api/auth/sign-out ends the session the cookie names and revokes the access token sent with it.
Sign-Ins That Need a Browser
Section titled “Sign-Ins That Need a Browser”These are for the web application, not for scripts. Each ends in the same session as a password sign-in.
-
Passkeys. Each step has two calls.
.../optionsanswers aceremonyid and theoptionsfor the browser’s WebAuthn call. The second call sends back theceremonyand the browser’scredential. A ceremony lasts five minutes and serves once. A refused passkey answers401at sign-in and at the second step, and409at step-up, each with the reason indetail.Step Calls Sign in POST /api/auth/passkey/options, thenPOST /api/auth/passkeyThe second step POST /api/auth/passkey/second-factor/optionsandPOST /api/auth/passkey/second-factor, each with thechallengeStep-up POST /api/auth/step-up/passkey/options, thenPOST /api/auth/step-up/passkey, signed in -
Microsoft, Google and Apple accounts.
POST /api/auth/accounts/{provider}/startanswers the provider’s address. The provider sends the browser back to/api/auth/accounts/callback.GET /api/auth/accountssays which providers the instance offers. -
Single sign-on.
POST /api/auth/discoverwith an address says which way the sign-in page sends it. OpenID Connect starts at/api/auth/sso/{slug}/start, and SAML at/api/auth/saml/{slug}/start. -
Invitations.
POST /api/invitations/describeandPOST /api/invitations/accepttake the token from the invitation link.
A person who signs in only through single sign-on, or only with a Microsoft, Google or Apple account, has no password, so a script cannot sign in as them. Use a service account instead.
Every route, with what it takes and answers, is in the API reference.