> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.doc-detective.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.doc-detective.com/_mcp/server.

# Authenticate API requests

> Send bearer tokens and basic credentials with httpRequest and checkLink, and reuse a token from a sign-in request in later steps.

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`](/docs/actions/httprequest) and [`checkLink`](/docs/actions/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](/docs/test-docs/http-and-api).

## 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](/docs/test-docs/sign-in).
* The credentials in environment variables, such as `API_TOKEN=<your token>` in a `.env` file locally, and CI secrets in CI. See [Protect sign-in secrets and session files](/docs/test-docs/sign-in-secrets).

## 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`**

```json title="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`**

```json title="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***`.

## Check links that need credentials

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

```json
{
  "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`**

```json title="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`**

```json title=".doc-detective.json"
{
  "input": "docs",
  "beforeAll": "tests/get-token.spec.json",
  "loadVariables": ".env"
}
```

Then send the token in any later test:

**`projects.spec.json`**

```json title="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`:

```json
{
  "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](/docs/test-docs/authenticator-app-sign-in).

`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](/docs/test-docs/reuse-a-session).

## Related

* [Generate requests from OpenAPI](/docs/test-docs/generate-tests-from-openapi), including how to override the `Authorization` header.
* [Secrets in output](/docs/ci/secrets), for what Doc Detective masks and what it can't.