Skip to navigation

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

Send a bearer token

Put the token in an Authorization header, and set statusCodes so the step fails when the API rejects it:

api.spec.json
{
"tests": [
{
"steps": [
{
"httpRequest": {
"url": "https://api.example.com/v1/projects",
"method": "get",
"request": {
"headers": { "Authorization": "Bearer $API_TOKEN" }
},
"statusCodes": [200]
}
}
]
}
]
}

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.

api.spec.json
{
"tests": [
{
"steps": [
{
"httpRequest": {
"url": "https://api.example.com/v1/projects",
"method": "get",
"request": {
"headers": { "Authorization": "Basic $API_BASIC_CREDENTIALS" }
},
"statusCodes": [200]
}
}
]
}
]
}

Reports mask the header, typically as Basic ***API_BASIC_CREDENTIALS***.

checkLink takes the same headers, so you can check links to pages or endpoints behind authentication:

{
"checkLink": {
"url": "https://api.example.com/v1/status",
"headers": { "Authorization": "Bearer $API_TOKEN" },
"statusCodes": [200]
}
}

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.

tests/get-token.spec.json
{
"tests": [
{
"testId": "get-token",
"steps": [
{
"description": "Sign in and keep the access token.",
"httpRequest": {
"url": "https://api.example.com/v1/auth/token",
"method": "post",
"request": {
"headers": { "Content-Type": "application/json" },
"body": { "username": "$API_USER", "password": "$API_PASSWORD" }
},
"statusCodes": [200]
},
"variables": { "ACCESS_TOKEN": "$$response.body.access_token" }
}
]
}
]
}
.doc-detective.json
{
"input": "docs",
"beforeAll": "tests/get-token.spec.json",
"loadVariables": ".env"
}

Then send the token in any later test:

projects.spec.json
{
"tests": [
{
"steps": [
{
"httpRequest": {
"url": "https://api.example.com/v1/projects",
"method": "get",
"request": {
"headers": { "Authorization": "Bearer $ACCESS_TOKEN" }
},
"statusCodes": [200]
}
}
]
}
]
}

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:

{
"steps": [
{
"description": "Generate the one-time code.",
"runShell": "echo code",
"variables": { "API_OTP": "totp(\"$TOTP_SECRET\")" }
},
{
"httpRequest": {
"url": "https://api.example.com/v1/auth/token",
"method": "post",
"request": {
"headers": { "Content-Type": "application/json" },
"body": { "username": "$API_USER", "password": "$API_PASSWORD", "otp": "$API_OTP" }
},
"statusCodes": [200]
},
"variables": { "ACCESS_TOKEN": "$$response.body.access_token" }
}
]
}

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.