Originally published on the apiwren API guides.
In this tutorial you will write a pytest test that signs up with a fresh disposable address, waits for the verification email and asserts that it carries a valid code or link. The same pattern works for password reset, magic-link login and invite emails. It uses the Spare Mailbox Temp Email API and about 50 lines of Python.
What you need: Python 3.8+, pip install requests pytest, and a free RapidAPI key.
1. Subscribe to the free plan
- Open the Spare Mailbox Temp Email API on RapidAPI and subscribe to the free Basic plan (20 requests a month).
- Copy your
X-RapidAPI-Keyand export it:
export RAPIDAPI_KEY=your_key
2. A small helper to call the API
The helper sends your key, retries rate limits (HTTP 429) and server errors (5xx) with a growing wait, and stops with the API's error message on anything else.
"""pytest end-to-end test of a signup email with a disposable inbox.
Uses the Spare Mailbox Temp Email API on RapidAPI:
https://rapidapi.com/apiwren-apiwren-default/api/spare-mailbox-temp-email-api
Run: SIGNUP_URL=https://your-app.example/api/signup pytest -s test_signup_email.py
"""
import os, re, sys, time
import pytest
import requests
HOST = "spare-mailbox-temp-email-api.p.rapidapi.com"
HEADERS = {"X-RapidAPI-Key": os.environ["RAPIDAPI_KEY"], "X-RapidAPI-Host": HOST}
def get(path, **params):
"""GET one endpoint. Retries rate limits (429) and server errors (5xx)."""
for attempt in range(4):
r = requests.get(f"https://{HOST}{path}", headers=HEADERS, params=params, timeout=90)
retry = r.status_code == 429 or r.status_code in range(500, 600)
if retry and attempt != 3:
time.sleep(float(r.headers.get("Retry-After") or 2 ** (attempt + 1)))
continue
if not r.ok:
sys.exit(f"HTTP {r.status_code}: {r.text[:300]}")
body = r.json()
if isinstance(body, dict) and body.get("success") is False:
sys.exit(f"API error: {body.get('error')}")
return body
3. A fresh inbox per test
A pytest fixture creates an address with /emails/generate, so tests never read each other's mail. A helper polls /emails/inbox every 15 seconds, for up to 2 minutes, until a message whose subject matches arrives.
@pytest.fixture
def inbox():
"""A fresh address for each test, so tests never read each other's mail."""
return os.environ.get("INBOX_ADDRESS") or get("/emails/generate")["email"]
def wait_for_message(address, subject_pattern, timeout=120, every=15):
for _ in range(timeout // every):
for msg in get("/emails/inbox", email=address):
if re.search(subject_pattern, msg["subject"] or "", re.I):
return msg
time.sleep(every)
pytest.fail(f"no message matching {subject_pattern!r} for {address} in {timeout}s")
4. Trigger your signup
Replace sign_up with your app's signup: an HTTP call to your API, or a Playwright form fill such as page.fill('#email', address) followed by a click on the submit button. The example posts to SIGNUP_URL when it is set.
def sign_up(address):
"""Replace with your app's signup: an HTTP call, or a Playwright form fill."""
url = os.environ.get("SIGNUP_URL")
if url:
requests.post(url, json={"email": address, "password": "Test-password-1"}, timeout=30).raise_for_status()
else:
print(f"\nno SIGNUP_URL set: send a verification email to {address} by hand")
5. Assert on the email
The inbox entry already has otp and magicLink, so the assertions are short.
def test_signup_sends_verification_code(inbox):
sign_up(inbox)
msg = wait_for_message(inbox, r"verif|code")
print(f"\n{inbox} got {msg['subject']!r}: otp={msg['otp']} link={msg['magicLink']}")
assert msg["otp"] or msg["magicLink"], "email has no code or link"
if msg["otp"]:
assert re.fullmatch(r"\d{4,8}", msg["otp"])
6. Run it
SIGNUP_URL=https://staging.example.com/api/signup pytest -s test_signup_email.py
Real output from a run on 2026-11-28:
============================= test session starts ==============================
platform darwin -- Python 3.15.2, pytest-9.2.0, pluggy-1.7.0
rootdir: /home/you/project
collected 1 item
test_signup_email.py
no SIGNUP_URL set: send a verification email to colin.ivanov58@lunaria.art by hand
colin.ivanov58@lunaria.art got 'Verify your email': otp=482913 link=None
.
============================== 1 passed in 0.42s ===============================
For this run there was no app to sign up to, so SIGNUP_URL was unset, INBOX_ADDRESS pointed at an address created earlier, and a verification email was sent to it by hand. Only the rootdir path is shortened in the output.
The complete script is in examples/test_signup_email.py.
Tips
- Run tests in parallel safely: each test has its own address.
- Set
INBOX_ADDRESSto reuse one address while you debug. - In CI, store
RAPIDAPI_KEYas a secret. - A test costs one request for the address and one per poll: 9 at most with these defaults. On the free plan of 20 requests a month, run it on demand rather than on every push, or move to a paid plan.
Next steps
- Spare Mailbox Temp Email API on RapidAPI: all endpoints, plans and the live playground.
- Spare Mailbox Temp Email API overview and more tutorials.
- Questions? Email terrelllouis056@gmail.com.
Top comments (0)