# Using OTP sent via email in E2E UI tests

Humanless works with any E2E framework because it is plain HTTP: create a disposable inbox with `POST /v1/identities`, have your UI test enter the returned `email`, submit, and call `GET /v1/identities/:id/wait`. That request stays open until the email arrives and returns the OTP in `code` (and any link in `url`). TypeScript and JavaScript tests can use `npm i -D humanless`, which wraps this in `waitForCode`. Use one inbox per test so parallel runs stay isolated, pass a `since` timestamp taken before the send so resends don't confuse the wait, and delete the inbox when the test ends. 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.

```js humanless-helpers.js
// Plain fetch, so it works in any Node-based runner (WebdriverIO, TestCafe, Nightwatch, Puppeteer)
const API = "https://api.usehumanless.com/v1/identities";
const headers = { Authorization: `Bearer ${process.env.HUMANLESS_API_KEY}` };

export async function createInbox() {
  const res = await fetch(API, { method: "POST", headers: { ...headers, "Content-Type": "application/json" },
    body: JSON.stringify({ channels: ["email"] }) });
  if (!res.ok) throw new Error(`create failed: ${res.status}`);
  return res.json();                            // { identity_id, email, ... }
}

export async function waitForOtp(identityId, since, deadlineMs = 120_000) {
  const end = Date.now() + deadlineMs;
  while (Date.now() < end) {
    const res = await fetch(`${API}/${identityId}/wait?channel=email&timeout=30&since=${since}`, { headers });
    if (res.ok) return (await res.json()).code;
    if (res.status !== 408) throw new Error(`wait failed: ${res.status}`);   // 408: nothing yet
  }
  throw new Error("no OTP email before the deadline");
}

export const deleteInbox = (identityId) => fetch(`${API}/${identityId}`, { method: "DELETE", headers });
```

## Other options

- **Mailpit** (a maintained alternative to MailHog): local SMTP capture with a REST API, ideal for CI with a sidecar container.
- **IMAP or the Gmail API** on a real test mailbox with plus-addressing.
- **Mailosaur, MailSlurp or Mailinator**: hosted inbox APIs with SDKs for several languages.

> **Use policy:** Only for apps you own or are authorized to test. See the [Terms](/terms).

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

Last updated 2026-12-04

Canonical: https://usehumanless.com/answers/using-otp-sent-via-email-in-e2e-ui-tests

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