[Blog](https://mailforbots.com/blog)

# How to test sign-up and magic-link emails in Cypress

Published October 12, 2026 · Updated October 12, 2026

To test email verification in Cypress, create a throwaway inbox with `cy.request`, type its address into your sign-up form, then call the long-poll wait endpoint from a custom command. The response already contains the extracted one-time codes and links, so the test needs no `cy.wait(5000)` and no HTML parsing. The one Cypress-specific detail is timing: the wait call can hold for up to 60 seconds by default, so the request needs its own timeout.

## What does a Cypress email test need from the API?

Three calls under `/api/v1`, all with `Authorization: Bearer bm_...`. See the [REST API docs](https://mailforbots.com/docs/rest) for every parameter.

| Step | Call | Returns |
| --- | --- | --- |
| Create inbox | `POST /inboxes` with `{ "username": "cy-1718000000", "ttl_minutes": 60 }` | The inbox `id` and `address` |
| Wait for mail | `GET /inboxes/:inbox/wait?subject_contains=verify&timeout_seconds=60` | The full message with `extracted.codes` and `extracted.links`, or `408` |
| Clean up | `DELETE /inboxes/:inbox` | Removes the inbox and its messages |

Add a unique suffix such as a timestamp to the username, because a deleted username cannot be reused. The wait call returns the first matching unread inbound message and marks it read, so you can submit the form first and wait afterwards without a race.

## How do I keep the API key out of the spec files?

Put the key in `Cypress.env`. Any environment variable prefixed with `CYPRESS_` is exposed that way, so `export CYPRESS_BOTMAIL_KEY=bm_...` locally, or store it as a CI secret under that name. Never write it into `cypress.config.js`, which is committed. The API base URL is not secret and can live in the config. Key scopes are covered in [API keys](https://mailforbots.com/docs/keys).

cypress.config.js

```
// cypress.config.js
const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    baseUrl: "http://localhost:3000",
    responseTimeout: 70000, // default for cy.request; the wait command also sets it per call
  },
  env: {
    BOTMAIL_API_URL: "https://YOUR-API-HOST/api/v1", // your API base, ending in /api/v1
    // BOTMAIL_KEY comes from the environment: export CYPRESS_BOTMAIL_KEY=bm_...
  },
});
```

## How do I write the custom commands?

Custom commands keep the HTTP details out of your tests. The important line is `timeout: 70000` on the wait request: `cy.request` uses `responseTimeout` (30 seconds by default), which is shorter than a 60-second wait, so without it Cypress fails the request before the API can answer. Setting `failOnStatusCode: false` lets the command turn a `408` into a clear message instead of a generic status-code error.

cypress/support/commands.js

```
// cypress/support/commands.js
const api = (path) => `${Cypress.env("BOTMAIL_API_URL")}${path}`; // base ending in /api/v1
const auth = () => ({ Authorization: `Bearer ${Cypress.env("BOTMAIL_KEY")}` });

Cypress.Commands.add("createInbox", () => {
  return cy
    .request({
      method: "POST",
      url: api("/inboxes"),
      headers: auth(),
      body: { username: `cy-${Date.now()}`, ttl_minutes: 60 },
    })
    .its("body"); // { id, address, ... }
});

Cypress.Commands.add("waitForEmail", (inboxId, query = {}) => {
  const qs = new URLSearchParams({ timeout_seconds: "60", ...query });
  return cy
    .request({
      url: api(`/inboxes/${inboxId}/wait?${qs}`),
      headers: auth(),
      timeout: 70000, // the API may hold the request for up to 60s
      failOnStatusCode: false, // so we can turn a 408 into a readable failure
    })
    .then((res) => {
      if (res.status === 408) throw new Error("No matching email arrived in time");
      expect(res.status, "wait status").to.eq(200);
      return res.body;
    });
});

Cypress.Commands.add("deleteInbox", (inboxId) => {
  cy.request({ method: "DELETE", url: api(`/inboxes/${inboxId}`), headers: auth(), failOnStatusCode: false });
});
```

Import the file from `cypress/support/e2e.js`. Because `cy.request` runs in Node-side networking rather than in the browser, there are no CORS concerns, and no `cy.task` is needed for these calls. Reach for `cy.task` only if you want to share an inbox across spec files, since `before` hooks run per spec.

## What does the sign-up and magic-link test look like?

Create one inbox per spec, then give each test its own plus-address tag so tests do not read each other's mail. `agent+tag@domain` is delivered to the same inbox and the full address is stored on the message, which `to_contains` matches. The selectors below use `cypress-testing-library` (`findByLabelText`); swap in `cy.get` if you do not use it.

cypress/e2e/signup.cy.js

```
// cypress/e2e/signup.cy.js
describe("signup emails", () => {
  let inbox;

  before(() => {
    cy.createInbox().then((created) => { inbox = created; });
  });

  after(() => {
    if (inbox) cy.deleteInbox(inbox.id);
  });

  it("verifies an account with the emailed code", () => {
    const [user, domain] = inbox.address.split("@");
    const tag = `+${Cypress._.uniqueId("signup-")}-${Date.now()}`;

    cy.visit("/signup");
    cy.findByLabelText("Email").type(`${user}${tag}@${domain}`);
    cy.contains("button", "Continue").click();

    cy.waitForEmail(inbox.id, { to_contains: tag, subject_contains: "verification" }).then((msg) => {
      cy.findByLabelText("Code").type(msg.extracted.codes[0]);
    });
    cy.contains("button", "Verify").click();
    cy.contains("Welcome").should("be.visible");
  });

  it("signs in with a magic link", () => {
    const [user, domain] = inbox.address.split("@");
    const tag = `+${Date.now()}-magic`;

    cy.visit("/login");
    cy.findByLabelText("Email").type(`${user}${tag}@${domain}`);
    cy.contains("button", "Email me a link").click();

    cy.waitForEmail(inbox.id, { to_contains: tag, subject_contains: "sign in" }).then((msg) => {
      cy.visit(msg.extracted.links[0].url);
    });
    cy.url().should("include", "/dashboard");
  });
});
```

Two things to check in your own app. First, the `.then` callbacks matter: Cypress commands are queued, so values from the wait call are only available inside the callback. Second, if the magic link points to a different origin than your `baseUrl`, wrap the post-visit assertions in `cy.origin`. If the email contains several links, choose the right entry in `extracted.links`; each has a `kind` such as `verify` or `login`. For more on tags, see [email testing for developers](https://mailforbots.com/email-testing-for-developers).

## Checklist before you run it in CI

- The wait request has a `timeout` above `timeout_seconds`; the example uses 70 seconds against 60.
- Every username and every plus-address tag is unique per run.
- The key is in `CYPRESS_BOTMAIL_KEY`, not in the repository.
- `after` deletes the inbox, and `ttl_minutes` outlasts your spec.
- The key has the scopes it needs: reading mail, plus managing inboxes if the spec creates them.
- Free allows 3 inboxes and Pro 100, so parallel specs on Free should share one inbox with tags. See [pricing](https://mailforbots.com/pricing).

## What if the email never arrives?

The endpoint answers `408` when nothing matching arrives before `timeout_seconds`. Fail the test with that message; do not add retries or sleeps. If your app is slow to send, raise `timeout_seconds` (up to 300) and raise the request `timeout` with it. Also check the filter: a `subject_contains` that does not match your real subject looks identical to a missing email. The same pipeline applies to password resets, which are walked through in [test password reset emails in CI](https://mailforbots.com/blog/test-password-reset-emails-in-ci). Playwright users can use the [Playwright version](https://mailforbots.com/blog/playwright-email-verification-otp-tests) with a worker-scoped fixture, and the [quickstart](https://mailforbots.com/docs/quickstart) covers creating your first key and inbox.

If you are weighing this against a hosted email-testing service, see the [Mailosaur alternative comparison](https://mailforbots.com/compare/mailosaur-alternative). Otherwise, [start free](https://mailforbots.com/#/signup) and run your first Cypress email test today.

## Frequently asked questions

**Why does cy.request fail before the email arrives?**

cy.request waits for a response only up to responseTimeout, which is 30 seconds by default. The wait endpoint can hold for up to 60 seconds, so pass a larger timeout (for example 70000) on that request.

**Do I need cy.task to call the email API?**

No. cy.request can call the REST API directly, and it is not subject to browser CORS rules. Use cy.task only if you need Node-side logic or state shared across specs.

**How do I get the magic link or code out of the email?**

The wait response includes extracted.codes for one-time codes and extracted.links for links, each with a url and a kind. Use msg.extracted.codes[0] or the matching link in your test.

[Start free](https://mailforbots.com/#/signup) [Read the quickstart](https://mailforbots.com/docs/quickstart)

## Related reading

- [Disposable inboxes for testing sign-up and password-reset emails](https://mailforbots.com/email-testing-for-developers)
- [How to QA email flows in 2026 with AI](https://mailforbots.com/blog/how-to-qa-email-flows-with-ai-2026)
- [How to test password-reset emails in CI](https://mailforbots.com/blog/test-password-reset-emails-in-ci)
