Playwright's request fixture (and request.newContext()) provides a full HTTP client for calling APIs directly — no browser needed. It shares Playwright's config (baseURL, auth, tracing) so you can write pure API tests or mix API calls into UI tests for setup and verification.
The APIRequestContext exposes get/post/put/patch/delete, returning responses you assert on with the same expect API (expect(response).toBeOK(), status(), json()). Because it reuses the browser context's cookies when you use page.request, an authenticated UI session's credentials carry into API calls automatically. Two big use cases: (1) fast API-only tests that validate endpoints without UI overhead; and (2) hybrid tests where you seed or clean up state via API (create a user, place an order) and then verify the UI, avoiding slow click-through setup. This is far more ergonomic than bolting REST Assured or requests onto a Selenium suite, because it's the same framework, config and reporting.
A UI test needs an existing order to view. Instead of clicking through checkout each time (slow, flaky), the test POSTs to /api/orders via request to create the order in one call, then loads the order page and asserts the UI shows it — cutting a 40-second setup to under a second.
import { test, expect } from '@playwright/test';
// Pure API test
test('GET /api/products returns a list', async ({ request }) => {
const res = await request.get('/api/products');
await expect(res).toBeOK();
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
});
// Hybrid: seed via API, verify via UI
test('created order shows in the UI', async ({ page, request }) => {
const res = await request.post('/api/orders', {
data: { productId: 42, qty: 2 },
});
await expect(res).toBeOK();
const { id } = await res.json();
await page.goto(`/orders/${id}`);
await expect(page.getByRole('heading', { name: `Order #${id}` })).toBeVisible();
});Scenario: End-to-end tests that build state through the UI (register → verify email → add address → checkout) take minutes each and flake on every intermediate step. How do you use API testing to make them fast and stable while still testing the real UI?