# OAuth 2.0 client credentials For server-to-server integrations where you'd rather manage a short-lived access token than a long-lived API key, the platform issues JSON Web Tokens via the standard **client credentials** grant. Which flow you use is up to you: - **API key** (`x-api-key`) — simplest; the key is the credential. Good for CLIs, batch jobs, and simple integrations. - **OAuth client credentials** — the app exchanges a `client_id` + `client_secret` for a short-lived JWT, then uses the JWT as a bearer token on subsequent requests. Good for integrations that want token lifetimes measured in minutes, not months. Both flows resolve to the same `DeveloperAuthContext` on the server side. Every scope check, every rate-limit tier, and every audit-log entry behaves identically. ## Endpoint ``` POST /api/auth/oauth/token ``` ## Request The endpoint accepts the standard form encoding, which is what most OAuth client libraries send: ```http POST /api/auth/oauth/token HTTP/1.1 Host: developers.gallantreecapital.com Content-Type: application/x-www-form-urlencoded grant_type=client_credentials&client_id=your-app-client-id&client_secret=your-app-client-secret ``` JSON is also accepted, if that suits your client better: ```http Content-Type: application/json { "grant_type": "client_credentials", "client_id": "your-app-client-id", "client_secret": "your-app-client-secret" } ``` ### Client authentication Credentials can go in the body, as above, or in an HTTP Basic header — the header wins if you send both: ```http Authorization: Basic base64(client_id:client_secret) ``` - `grant_type` **must** be `client_credentials`. Anything else returns `400 unsupported_grant_type`. - `client_id` and `client_secret` are shown in the portal on the app's Credentials page. The secret is shown **once**; treat it like a password and store it in a secret manager. - Missing or wrong credentials return `401 invalid_client` — deliberately generic, as with API keys. A rejected Basic header also carries a `WWW-Authenticate` challenge. - A malformed body returns `400 invalid_request`. ### Requesting a narrower scope By default the token carries every scope the app holds. Send `scope` — space-delimited — to ask for less: ``` grant_type=client_credentials&client_id=…&client_secret=…&scope=loans:read ``` Asking for a scope the app does **not** hold returns `400 invalid_scope`; the request is refused rather than quietly trimmed. Narrowing is worth doing when one service only ever reads: a leaked token then carries only what that service needed. Scopes are re-checked against the app on every request, so a scope removed in the portal stops working immediately — you do not have to wait for the token to expire. ### Rate limit Token requests are limited **per client IP**, and every attempt counts whether or not the credentials are valid. Exceeding it returns `429` with a `Retry-After` header. A client needs one token per expiry period, not one per API call — cache the token in memory until it expires. See [Rate limits](rate-limits.md). ## Response ```json { "access_token": "eyJhbGciOiJIUzI1NiIs...", "token_type": "Bearer", "expires_in": 900, "scope": "loans:read organisations:read" } ``` - **`access_token`** — a JWT signed by the platform. Use it as a `Bearer` token on subsequent requests. - **`expires_in`** — seconds until the token expires. The default is **900 (15 minutes)** — request a new token before it expires; the platform does not refresh existing tokens. - **`scope`** — the space-separated list of scopes granted to *this token*: the app's full set, or the narrower set you requested. Every request the token makes is bounded by this list. Token responses are returned with `Cache-Control: no-store` — do not cache them in any shared or on-disk cache. ## Using the token ```http GET /api/v1/loans HTTP/1.1 Host: developers.gallantreecapital.com Authorization: Bearer eyJhbGciOiJIUzI1NiIs... ``` Send it in `Authorization: Bearer …` — do **not** put it in `x-api-key`. The middleware distinguishes the two. ## Choosing between key and JWT | Property | API key | OAuth JWT | |----------|---------|-----------| | Lifetime | 90 days (default) | 15 minutes | | Rotation | Manual — generate a new key, retire the old within a 24h grace window | Automatic — request a fresh token when the current one expires | | Storage | Secret store; grep-safe prefix `gt_` | Ephemeral — hold in memory only | | Best for | Long-lived integrations, CLIs, batch jobs | Frequent-request services that want short-lived credentials | | Compromise blast radius | Up to 90 days if you don't detect it | Up to 15 minutes if you don't detect it | For most integrations, API keys are fine. Reach for OAuth when the token blast radius matters more than the request overhead. ## Runnable example ```bash # 1. Get a token TOKEN=$(curl -sS https://developers.gallantreecapital.com/api/auth/oauth/token \ -H "Content-Type: application/json" \ -d "{ \"grant_type\": \"client_credentials\", \"client_id\": \"$GALLANTREE_CLIENT_ID\", \"client_secret\": \"$GALLANTREE_CLIENT_SECRET\" }" | jq -r .access_token) # 2. Use it curl -sS https://developers.gallantreecapital.com/api/v1/loans \ -H "Authorization: Bearer $TOKEN" ```