← All use cases

OTP Verification

Auto-extract 2FA/MFA confirmation codes sent by email

The OTP extraction API pulls confirmation codes out of email bodies. Common formats need no regex, and you can pass a pattern for other formats. Test logins and two-factor flows that send codes by email, with no manual entry.

What you can do with MailBrew

  1. 01

    One call to get the code

    /addresses/{address}/emails/{id}/otp extracts the confirmation code from the email body and returns it.

  2. 02

    Formats recognized by default

    4 to 8 digit numbers and hyphenated alphanumeric codes such as ABCD-1234. Near words such as "code", space-separated digits (123 456) and alphanumeric codes (A1B2C3) are also picked up. For other formats, pass a pattern (regex). If the code arrives as a URL, use the link extraction API.

  3. 03

    2FA/MFA testing

    Test logins that use confirmation codes sent by email, without typing the code. Authenticator app (TOTP) codes are not covered.

  4. 04

    Check your app's requirements

    Also useful in tests that check code expiration, resend limits, brute-force protection, and other requirements of your app.

Sample code

Python (requests)

Python (requests)
import os
import requests

API = "https://mailbrew.dev/api/mailbrew"
HEADERS = {"X-API-Key": os.environ["MAILBREW_API_KEY"]}

# 1. Create a test address
addr = requests.post(f"{API}/addresses", json={"ttl": 300}, headers=HEADERS).json()["data"]["address"]

# 2. Trigger login in your app → sends OTP email
trigger_login(email=addr)

# 3. Wait for the email (timeout in seconds, max 60)
emails = requests.get(f"{API}/addresses/{addr}/emails/wait", params={"timeout": 30},
                      headers=HEADERS, timeout=40).json()["data"]
assert emails, "OTP email did not arrive"

# 4. Extract the code (no regex needed)
otp = requests.get(f"{API}/addresses/{addr}/emails/{emails[0]['id']}/otp",
                   headers=HEADERS).json()["data"]["code"]

# 5. Complete login with the extracted code
complete_login(code=otp)

Ready to try this use case?

Get started with the free plan today. No credit card required.
Free plan: up to 100 emails/month and 5 addresses

Create free account →