APIRequestContext can be created standalone via playwright.request.newContext() or accessed through the request fixture — with its own cookies and headers.
Standalone Request Context
Use request.newContext({ baseURL, storageState, extraHTTPHeaders }) when you need separate API sessions from the browser page — admin API vs user browser, for example.
Always dispose() standalone contexts when done to free sockets.
import { request as playwrightRequest } from '@playwright/test';
const apiContext = await playwrightRequest.newContext({
baseURL: 'https://api.example.com',
extraHTTPHeaders: { 'X-API-Key': process.env.API_KEY! },
});
const res = await apiContext.get('/v1/status');
await apiContext.dispose();
Fixture request is disposed automatically; manual contexts are not.