# Test an HTTP API

Source: https://testis.dev/docs/api-testing

> Let the AI tester call the API behind your app: real requests and responses, login, and your OpenAPI file as its map.

The AI tester tests an API the way a developer pokes at it: it sends real requests to the address you declare, reads each response, and checks it against your PRD: status codes, bodies, error answers, limits and who may do what.

## Set up

```bash
testis init --api http://localhost:4000
testis init --api http://localhost:4000 --web     # the API plus your web app
```

The API part of `.testis/config.yaml`:

```yaml
targets:
  - kind: api
    baseUrl: http://localhost:4000
    devCommand: node server.js          # started for you when the address is local and not up
    readyUrl: /health
    openapi: openapi.yaml               # optional: lets Testis learn your endpoints
    auth:                               # optional: log in before every run
      url: /auth/login
      body: { email: "{{EMAIL}}", password: "{{PASSWORD}}" }
      save: { token: $.token }          # tests can then send  Authorization: Bearer {{token}}
environments:
  local:
    variables: { EMAIL: ada@example.com }
    secretRefs: { PASSWORD: ADA_PASSWORD }   # the NAME of an environment variable, never the value
```

With `auth` set, Testis logs in at the start of every run, so tokens are always fresh. What `save` captures becomes a `{{variable}}` the tester sends (for example `Authorization: Bearer {{token}}`); the AI model never sees its value, and secret-looking fields in responses are shown to it as `[redacted]`.

## Test it

```bash
testis test --api                     # only the API
testis test --api "creating and editing tasks"
```

The tester first looks for your OpenAPI document (the `openapi` file above, or `/openapi.json` and similar addresses) and reads its routes; with none, it works from your PRD. Then it works through each area: it sends a request, reads the status, headers and body, sends the next one (creating what a check needs first), and records what works and what does not. Every bug quotes the response it got. Each request is a step in the report, so you can repeat it.

It tries the unhappy paths too: missing fields, wrong types, someone else's record, too many items.

## Test a deployed API

Set `baseUrl` to the deployed address, leave out `devCommand`, and use an environment for each stage (see [Variables and secrets](/docs/web-testing#variables-and-secrets)). Mark an environment `readOnly: true` to block anything that changes data, for example production. The tester never deletes or changes data it did not create.
