How to Automate OTP Verification Testing With an Email API
Guide
By the zephbox teamPublished Updated 5 min read
How we write these: product details are checked against the current zephbox API and site behavior on the date shown. Spotted a mistake? Tell us and we correct it (editorial policy).
To automate OTP verification testing, give every test run its own fresh inbox, submit the signup with that address, wait for the email through an API instead of a human, extract the code, and finish the flow while asserting the account is really active. The five steps below use the real zephbox endpoints, and the second half covers the part most guides skip: what to test around the happy path, and how to stay inside your quota.
The five-step pattern with real calls
All calls send an X-API-Key header and use the base URL https://zephbox.com/api/developers. Creating inboxes through the API requires a Full Access account (see pricing); a free account can hold an API key but the inbox endpoints answer 403.
- Create an inbox.
POST /aliases/returns a JSON object that includes the address inaliasand itsexpires_at. One test run, one address. - Submit the signup. Drive your app (browser automation or an HTTP call) with that address, exactly as a user would.
- Wait for the mail.
GET /aliases/{alias}/wait-for-otp/?timeout=20holds the request until a new message arrives, up to 20 seconds. It only counts mail that arrives after the call starts, so an old message in the inbox cannot satisfy it. A 408 means nothing came in time. - Read the result. The response carries
codeandlink(either may benull), plussubject,senderandreceived_at. - Finish and assert. Submit the code, then check that the account is active, for example by logging in or loading a page that needs a verified user.
BASE=https://zephbox.com/api/developers
ALIAS=$(curl -s -X POST "$BASE/aliases/" -H "X-API-Key: $ZEPHBOX_API_KEY" | jq -r .alias)
# ... submit your signup form with $ALIAS here ...
curl -s "$BASE/aliases/$ALIAS/wait-for-otp/?timeout=20" -H "X-API-Key: $ZEPHBOX_API_KEY"Start the wait call before or immediately after you submit the form. Because the endpoint ignores mail that arrived earlier, calling it too late (after the email has already landed) will wait for a message that never comes. If your signup is slow to trigger and you cannot guarantee the order, use GET /aliases/{alias}/otp/ instead, which reads mail that has already arrived.
Why one inbox per run beats a shared mailbox
A shared test mailbox fails in two predictable ways. Parallel runs read each other's messages, and the mailbox slowly fills with mail nobody deletes, so the "newest message" is no longer the one you expect. A fresh inbox per run removes both problems by construction. Delete it at the end with DELETE /aliases/{alias}/ if you want tidy accounts; otherwise it expires on its own timer.
Beyond the happy path: what to actually test
Email verification has more failure behaviour than most teams cover. A useful checklist, each of which needs a real inbox to test properly:
- Wrong code is rejected and does not lock the account after a single attempt (or does, if that is your policy).
- Code reuse: submit the same valid code twice; the second must fail.
- Expiry: wait past your documented validity window and confirm the code is refused. Keep this as a slow, scheduled test, not part of every commit.
- Resend: request a second code and check which one your app honors. Many apps invalidate the first; some accept both. Pick the intended rule and assert it. Use the
received_atfield to tell the two messages apart. - Rate limits: confirm the app limits resend requests instead of mailing indefinitely.
- Email content: the sender, subject and link domain are what you intend, and links point to the right environment (staging mail that links to production is a classic bug).
- Link flow: when the email contains a magic link rather than a code, assert on
linkand follow it.
Timeouts and flaky runs
Most "flaky OTP test" reports are one of three things: a wait that is shorter than your own email provider's worst-case delay, a test that reads the previous run's message, or a code that expired because the suite paused between requesting and entering it. Set the wait to the longest delay you are willing to tolerate, fail with the subject and sender of whatever did arrive, and keep the gap between "receive code" and "enter code" short. The wait endpoint caps at 20 seconds per call (the default is 15), so for slower senders loop a couple of calls rather than increasing a single sleep.
Budget your quota in CI
An inbox per run is only free if you have the quota for it. The plans in pricing list the daily allowance per tier (50 per day on Full Access at the time of writing), so a suite that creates an inbox per test, run on every pull request, can use that up quickly. Two ways to stay inside it: create one inbox per test file or per signup scenario rather than per assertion, and reuse an inbox across steps of one flow (sign up, then password reset) since each message is separate and the received_at ordering tells them apart.
Choosing a provider for this job
Three things matter, in this order: a documented JSON API (not only a browser page), predictable mailbox lifetimes so a run cannot expire halfway (zephbox mailboxes last 24 hours by default), and delivery that is fast enough that your timeouts can be short. Check delivery against your own sender, because it depends on the sending service as much as on the receiver. For other approaches see what separates a real developer API, and for the code layer the Python SDK version of this pattern. Full endpoint details are in the API docs.
Frequently asked questions
Do I need a browser to use this pattern?
No. The inbox and OTP calls are plain HTTP. Your signup step can be a browser test, an HTTP request to your own API, or a mobile test; the email side does not care.
What if the wait call times out?
A 408 response means no new mail arrived within the timeout. Fall back to listing messages to see whether something arrived earlier or from an unexpected sender, then fail the test with that information.
Can I use a free account for this?
A free account can create an API key, but creating inboxes and reading OTPs through the API needs a Full Access plan. The web interface remains free for manual testing.
Automate OTP Verification in Cypress & Selenium
Use zephbox REST API keys to fetch OTP codes in headless automated browser runs.