November 17, 2026

Disposable email addresses for automated tests, created by API — and why not to use public inboxes

TestingAPIE2EDisposable addresses

Why each test run needs a new address

Sign-up, password reset, and invitation tests stop working the second time you reuse the same email address. The app answers "this address is already registered", or the test picks up the email from the previous run instead of the current one.

So create a new address for every test run and read only the emails sent to it. Tests running in parallel then never share an inbox.

The problem with public inboxes

The quick option is a disposable email service on the internet: make up a name and use it. Some of these services, however, let anyone who knows the address open its inbox.

The emails a test receives contain one-time passwords, password reset links, and invitation links — information that lets someone into an account if they use it. Even on staging, you do not want a third party to be able to get through the same login flow as production.

Pick an inbox for tests that meets two conditions:

  • Only people in your company and your test code can read it
  • Your test code can wait for an email to arrive and pull a code out of the body

Creating an address with MailBrew

With MailBrew, you create an address with an API key.

curl -X POST https://mailbrew.dev/api/mailbrew/addresses \
  -H "X-API-Key: $MAILBREW_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"ttl": 600}'
{
  "data": {
    "id": 123,
    "address": "[email protected]",
    "webhook_url": null,
    "expires_at": "2026-11-17T10:10:00+00:00",
    "is_permanent": false,
    "created_at": "2026-11-17T10:00:00+00:00"
  }
}
  • The address looks like [email protected]. If your company has a custom domain set up, the address is created on that domain.
  • ttl is the lifetime in seconds. It defaults to 1 hour and can be up to 1 year. 0 makes a permanent address with no expiry.
  • local_part lets you choose the part before @. You cannot reuse the name of an address that has not expired yet.

Through the API, emails for an address created with an API key are read with that same API key. In the dashboard, members of your company can read them. Other companies cannot see them.

Waiting for the email and extracting the code

In Node.js, use @mailbrew/sdk from npm.

const { MailBrew } = require('@mailbrew/sdk');

const mb = new MailBrew(process.env.MAILBREW_API_KEY);

// 1. Create an address just for this test (expires in 10 minutes)
const { address } = await mb.createAddress({ ttl: 600 });

// 2. Sign up in your app with that address (the action under test)
await signUp(address);

// 3. Wait for the email, filtered by subject, and extract the one-time password
const email = await mb.waitForEmail(address, {
  timeout: 30000,
  where: { subject_contains: 'Verification code' },
  extract: 'otp',
});

// otp is null when no code was found
console.log(email.extracted.otp?.code); // e.g. "482913"

// 4. Delete the address when you are done
await mb.deleteAddress(address);

waitForEmail calls an API that waits on the server until an email arrives (long polling). A single call waits up to 60 seconds. If nothing arrives, it throws, and the test fails there.

There is also captureOtp, which combines creating the address and receiving the code.

const otp = await mb.captureOtp({ ttl: 300 });
await signUp(otp.address);
const { code } = await otp.waitAndExtract();

Lifetimes and limits

Item Rule
Lifetime (ttl) 1 hour if omitted. Up to 1 year. 0 for permanent
Expired addresses (created by API) The wait API returns 410. You can create an address with the same name again
Addresses you can hold at once Limited per plan (pricing). Expired addresses and addresses created automatically on receipt (below) are not counted
Received emails Kept for your plan's retention period

You can also skip the create call

Instead of calling the API to create an address, you can send straight to [email protected] (your company needs at least one active API key). The address is created automatically when the first email arrives, and you can call the wait API before the email arrives. Your test code only has to build the address string, which makes this the easy option for parallel tests. See "Tag-Based Addresses: Test Email Addresses Without an API Call".

Summary

  • Prepare a new email address for every test run
  • Tests receive one-time passwords and links, so do not use public inboxes that anyone can open
  • With MailBrew, your test code can create an address by API, wait for the email, extract the code, and delete the address afterwards

For Playwright and Cypress, see "Write OTP Authentication E2E Tests in 5 Lines with Playwright" and "Testing email one-time passwords end to end with Cypress".

Try it now →

← Back to blog