# How to get OTP code in Playwright test

With Humanless, create a disposable inbox for the test, type its address into your app, then call `waitForCode`, which holds the request open until the email arrives and returns the OTP already extracted. Install it with `npm i -D humanless` and set `HUMANLESS_API_KEY` as a CI secret. Take `Date.now()` just before the click that sends the email and pass it as `since`, so a resend or a slow worker never picks up an old code. Use one inbox per test so parallel workers don't read each other's mail, and drop `page.waitForTimeout()`: the wait returns as soon as the message lands. Pay-as-you-go accounts can create 30 identities per hour (Starter 100, Team 300, Scale 1,000), so for larger suites reuse an identity across a spec or save the login state.

```ts otp.spec.ts
import { expect, test } from "@playwright/test";
import { Humanless, withIdentity } from "humanless";

const hl = new Humanless({ apiKey: process.env.HUMANLESS_API_KEY! });

test("log in with an emailed code", async ({ page }) => {
  await withIdentity(hl, { channels: ["email"] }, async ({ identity_id, email }) => {
    await page.goto("/login");
    await page.getByLabel("Email").fill(email!);
    const since = Date.now();
    await page.getByRole("button", { name: "Send code" }).click();
    const { code } = await hl.waitForCode({ identityId: identity_id, since, timeout: 25 });
    await page.getByLabel("Code").fill(code);
    await expect(page.getByTestId("dashboard")).toBeVisible();
  });
});
```

## Other options

- **Mailpit** (free, local or CI sidecar): point your app's SMTP at it and read the message from `GET /api/v1/search?query=to:"..."` inside `expect.poll`.
- **Gmail API** with plus-addressing (`qa+run123@yourdomain.com`): works against deployed environments, but needs an OAuth client and refresh token in CI.
- **Mailosaur or MailSlurp**: hosted inbox APIs with their own Playwright examples.

> **Use policy:** Only for apps you own or are authorized to test. Don't use disposable inboxes to sign up for or get past verification on third-party services. See the [Terms](/terms).

More: [How to test email OTP and verification codes in Playwright](/guides/playwright-email-otp) · [All answers](/answers/)

Last updated 2026-12-04

Canonical: https://usehumanless.com/answers/how-to-get-otp-code-in-playwright-test

---
More: https://usehumanless.com/llms.txt
