Exponential offers two ways for a program to sign in to the REST and GraphQL APIs. Pick the one that matches the job.
- A script of your own reads or publishes content: a personal API key.
- An app or partner site lets many users sign in and act for them: an OAuth application.
Personal API keys
A user with the policy apikey/create opens My account > API access, names a key, ticks the scopes it needs and picks a lifetime. In a new installation no role has that policy, so an administrator grants it first. The key has the form expk_<12 characters>_<40 characters> and is shown once. Only a salted HMAC-SHA-256 hash is stored, so nobody can show it again.
A script sends it as Authorization: Bearer expk_.... The request runs as the key's owner, limited to the key's scopes and never beyond the owner's own policies. Three checks run on every request: the key has the scope the route needs, the owner still holds that scope's policy, and for a write the owner may write that node.
Scopes are named in rest.ini [ApiKeySettings]: read (content/read), publish (content/create), edit (content/edit) and remove (content/remove). A write route of an extension is closed to keys until its scope is named.
A key ends when it is revoked, when it expires, or when its owner is disabled or removed. Revoking takes effect at the next request on every server. Administrators see every key at Setup > oAuth admin > API keys, and every make, revoke, first use and refusal is in the audit log.
OAuth 2 applications
- An administrator registers the application at Setup > oAuth admin. It gets a client id, a secret and its redirect address.
- The application sends the user to /oauth/authorize with client_id, redirect_uri, response_type=code and a random state, and with PKCE also code_challenge and code_challenge_method=S256. The user needs the policy oauth/authorize.
- The site redirects back with a code and the same state. The application exchanges the code at POST /api/auth/v1/oauth/token, with code_verifier when it sent a challenge.
- It sends the token as Authorization: Bearer <token>. A token lives for one hour by default (TokenTTL); a refresh token for 30 days (RefreshTokenTTL).
From 6.0.15 the authorization server supports PKCE and returns the state. A public client, stored without a secret (a mobile or browser app), must use PKCE. Clients made in Setup > oAuth admin always have a secret, so nothing changes for them. A token stops working as soon as its user is disabled or locked.
The password grant is available only for clients an administrator lists in rest.ini [OAuthSettings] PasswordGrantClients[], none by default.
Note: Upgrading to 6.0.15? Grant oauth/authorize to the roles that sign in through OAuth: php bin/php/addoauthauthorizepolicy.php shows what it would change, and --apply does it.