OAuth 2.0 in your API client: client credentials, PKCE and refresh tokens
OAuth 2.0 is the most common way modern APIs hand out tokens, and the most common reason a simple "let me just try this endpoint" takes half an hour. This guide shows how to get and refresh OAuth 2.0 access tokens in Requestarium, and how to deal with OAuth in automated runs.
Set it up once, on the collection
Open the collection's Authorization tab and choose OAuth 2.0. Every request set to Inherit auth will use the token. Then fill in:
| Field | What to put |
|---|---|
| Grant type | Client credentials, Authorization code (PKCE), Password or Refresh token |
| Token URL | The provider's token endpoint, e.g. https://auth.example.com/oauth/token |
| Client ID / Client secret | {{clientId}} and {{clientSecret}}, kept in the environment |
| Scope | Space-separated scopes, e.g. orders:read orders:write |
| Audience | Only if your provider needs it (Auth0 and some Okta setups) |
| Client authentication | Basic header (preferred) or form fields in the body |
Click Get new access token. The token is stored with the collection and sent as Authorization: Bearer … on every inheriting request. The access token, refresh token and expiry are encrypted at rest on the server.
Client credentials: service to service
The simplest grant. Your client authenticates as itself with its ID and secret, and gets a token back. No browser, no user. Use it for backend APIs, internal services and machine-to-machine integrations.
Authorization code with PKCE: acting as a user
When the API needs a real user's permission, use Authorization code (PKCE). Add the provider's authorization URL. When you click Get new access token, your browser opens the provider's login page; after you approve, the provider redirects to a local callback (http://127.0.0.1:8765/callback by default) and the app receives the token.
Password grant: legacy only
The resource owner password grant sends a username and password straight to the token endpoint. It's supported because some older and first-party APIs still use it, but OAuth 2.1 drops it. Prefer client credentials or PKCE for anything new.
Refreshing tokens
Access tokens expire, often after an hour. If the provider returned a refresh token, it's stored with the expiry, and the Refresh token grant gets a new access token without going through the login again.
Importing OAuth settings
If your API has an OpenAPI spec, you may not need to type any of this. Importing a spec turns its OAuth 2.0 security schemes (client credentials, authorization code and password flows) into collection auth with the token URL and scopes filled in, and {{clientId}} and {{clientSecret}} ready for values. Insomnia and Thunder Client exports carry their OAuth 2.0 settings across too. See From OpenAPI spec to a working collection.
OAuth in CI
The CLI sends the token stored with the collection, but that token was fetched by a person and it will expire. For CI, don't depend on it. Make the collection fetch its own token with a request at the top:
POST {{tokenUrl}}
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id={{clientId}}&client_secret={{clientSecret}}&scope=orders:readpm.environment.set('token', pm.response.json().access_token);Set the collection's auth to Bearer token with {{token}} for the CI environment (or on a dedicated folder), and pass the client secret from your CI's secrets:
requestarium run-collection "Orders API" --env Staging --env-var clientSecret=OAUTH_CLIENT_SECRETEach run gets a fresh token, nothing long-lived sits in CI, and the token request shows up in run reports, which makes auth failures easy to spot. More in How to chain API requests.
Troubleshooting
| Error | Usual cause |
|---|---|
invalid_client | Wrong client ID or secret, or the provider expects them in the body instead of a Basic header (switch Client authentication) |
invalid_scope | A scope the client isn't allowed to request |
redirect_uri_mismatch | The callback URL isn't registered with the provider exactly as configured |
| 401 on the API with a fresh token | Wrong audience, or the API expects a different scope |