API request context

Create test data, verify endpoints, and share authentication with the browser through an APIRequestContext.

  • Setup Create state before opening the page
  • Shared state Browser and API requests use the same cookies
  • Response evidence Keep response data and network traces together
Setup

Create state before opening the page

Send GET, POST, PUT, PATCH, DELETE, or HEAD requests from PHP. JSON and form data travel with the request, and response assertions verify the endpoint contract.

Network and API testing →
$response = $context->request()->post('/api/orders', [
    'data' => ['sku' => 'PW-1'],
]);

Expect::response($response)->toBeOK();
Shared state

Browser and API requests use the same cookies

A request context obtained from BrowserContext shares its cookie jar. API setup can authenticate the browser, and browser authentication can authorize direct requests.

Handling authentication →
Response evidence

Keep response data and network traces together

Inspect status, headers, text, and JSON directly in PHP. HAR tracing records the request flow when a backend check needs deeper evidence.

Choosing assertions →
Next step

Move from the tour to working code.

Choose the explanation, recipe, or runnable example that matches what you need next.

Common questions

Before you wire it into a suite.

What is APIRequestContext for?
Use it for setup, cleanup, endpoint checks, and backend verification without opening another browser page.
Does it share browser authentication?
Yes. A request context obtained from BrowserContext uses the same cookie jar, and response cookies are available to browser pages in that context.
What can I inspect on a response?
Read its status, status text, URL, headers, text, or JSON body, then use API response assertions for the expected contract.