> ## Documentation Index
> Fetch the complete documentation index at: https://e2e.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# API

> Test HTTP APIs with the request fixture and value matchers, in the same files, run, and report as your UI tests, with no model calls.

API tests use the [`request`](https://e2e.com/docs/reference/request) fixture and value matchers
right alongside your UI tests, with no model calls.

```ts title="tests/users-api.e2e.ts"

const User = z.object({ id: z.number(), name: z.string() });

test('GET /api/users returns the seeded users', async ({ request }) => {
  const response = await request.get('/api/users');
  expect(response).toBeOK();
  expect(response.headers['content-type']).toContain('application/json');
  const users = expect(await response.json()).toMatchSchema(z.array(User));
  expect(users).toHaveLength(3);
  expect(users.map((user) => user.name)).toEqual(['Ada', 'Grace', expect.any(String)]);
});
```

Each call is a step in the report, such as `GET /api/users`, and fails the
test when it runs past `actionTimeout`.

[`toBeOK`](https://e2e.com/docs/reference/expect#tobeok) requires a 2xx status. When it fails, it
names the status and the response's origin and path.

`toMatchSchema` takes any [Standard Schema](https://e2e.com/docs/reference/expect#matching-a-schema),
such as Zod, Valibot, or ArkType, and returns the value, typed. Use the
schema your app parses the API with, and the test fails when the API stops
matching it.

## Where the API runs

A path such as `/api/users` resolves against the target's `app.url`, with
the port the run picked when the URL declares port `0`. So one file runs
against the `app.command` the config starts and against a deployed preview.
A leading `/` starts at the origin: when `app.url` has a path, such as
`/shop`, write ``request.get(`${app.baseUrl}/api/users`)``.

When the API writes state after it answers, use
[`expect.poll`](https://e2e.com/docs/reference/expect#polling-a-value) to read again until a
matcher passes.

## Signed-in requests

`request` does not send the browser's cookies. To call the API as the user
the test signed in, send the cookies the browser holds for that URL in a
header. `browser` needs a web target:

```ts title="tests/orders-api.e2e.ts"

test('lists my orders', async ({ app, browser, request }) => {
  await app.open('/sign-in');
  // ...sign in through the UI...
  const cookies = await browser.cookies('/api/orders');
  const headers = { cookie: cookies.map((c) => `${c.name}=${c.value}`).join('; ') };
  expect(await request.get('/api/orders', { headers })).toBeOK();
});
```

[`browser.cookies(url)`](https://e2e.com/docs/reference/web#cookies) returns only the cookies
the browser would send to that URL, so another site's, such as a sign-in
provider's, stay out of the header. Without a URL it returns every cookie
of the browser.

## Without a browser

On a web target, an API test still opens a browser context. If you want
pure API tests with no browser overhead, omit the engine on the target and
pass an absolute URL from an environment variable:

```ts
const response = await request.get(`${process.env.API_URL}/api/users`);
```

`e2e init` can scaffold an engine-free target for you.

## Next

<CardGroup cols={2}>
  <Card title="request" icon="plug" href="/reference/request">
    Every method, option, and response field.
  </Card>
  <Card title="Value matchers" icon="circle-check" href="/reference/expect#value-matchers">
    Every matcher, `toMatchSchema`, and `expect.poll`.
  </Card>
  <Card title="Authentication" icon="key" href="/authentication">
    Sign in once and reuse the session in every test.
  </Card>
</CardGroup>
