Authenticate API requests
API calls in your docs usually need credentials: an API key, a bearer token, or a user name and password. Doc Detective sends them as request headers on httpRequest and checkLink steps. Keep each credential in an environment variable and reference it as $NAME, so it stays out of your specs and Doc Detective masks it in output.
For the basics of httpRequest, see Test HTTP and APIs.
Before you begin
- A test account or API key for a non-production environment, with only the access your docs need. See Find a testable sign-in.
- The credentials in environment variables, such as
API_TOKEN=<your token>in a.envfile locally, and CI secrets in CI. See Protect sign-in secrets and session files.
Send a bearer token
Put the token in an Authorization header, and set statusCodes so the step fails when the API rejects it:
When the API rejects the token, the step fails with the status it returned, such as 401. The report shows the header as Bearer ***API_TOKEN***, so you can check that the token was sent without seeing it.
An API that takes a key in its own header, such as X-Api-Key, works the same way: "headers": { "X-Api-Key": "$API_KEY" }. For a key in the query string, use "parameters": { "api_key": "$API_KEY" } in request.
Send basic credentials
Basic authentication sends user:password, base64-encoded, in an Authorization header. Store the encoded value as one secret, such as API_BASIC_CREDENTIALS. To create it, run printf '%s' 'user:password' | base64 | tr -d '\n' yourself in a terminal, with the test account’s real values, and store the output. Don’t have an AI agent run it, and clear the command from your shell history afterward.
Reports mask the header, typically as Basic ***API_BASIC_CREDENTIALS***.
Check links that need credentials
checkLink takes the same headers, so you can check links to pages or endpoints behind authentication:
Reuse a token from a sign-in request
Some APIs issue a token from a sign-in or token endpoint. Request the token once, and save it in a variable with the step’s variables. Later steps use it as $NAME.
To share the token across specs, request it in a beforeAll spec. A variable set there lasts for the rest of the run.
Then send the token in any later test:
Use the field your API returns in place of access_token. Doc Detective masks the token in output, because its name contains TOKEN and later steps use it as $ACCESS_TOKEN. The variable lasts only for one run, so the run gets a fresh token every time. If a token can expire before a long run ends, request it again in the test that needs it.
If the token endpoint also asks for a one-time code, generate it with totp() in the variables of an earlier step, then send it as $NAME in request.body. totp() doesn’t work directly inside request:
For where to get TOTP_SECRET, see Sign in with an authenticator app code.
httpRequest doesn’t share cookies with the browser, and saveCookie and loadCookie move browser cookies only. To call an API as the signed-in browser user, send a token in a header. To test signed-in pages in the browser, see Sign in once and reuse the session.
Related
- Generate requests from OpenAPI, including how to override the
Authorizationheader. - Secrets in output, for what Doc Detective masks and what it can’t.