Test Email OTP Verification with ReadyAPI
Create a private test inbox in ReadyAPI, wait for the current verification email, extract one code and complete your application's signup API.
Use with your tools
An email-verification API test crosses two systems. Your application accepts the signup request, then sends a code to an inbox. ReadyAPI needs that delivered code before it can confirm the user and check the account result.
Manually copying the code interrupts a functional test. Reusing a shared mailbox also risks reading an earlier request or another worker's message. MailSlurp provides private inboxes and message APIs that ReadyAPI can call as ordinary REST test steps.
The ReadyAPI example project shows the signup sequence. The requests below explain how to isolate each run, carry values between steps and stop when an expected result is missing. Adapt the application's signup, confirmation and login requests to your own API contract.
Configure MailSlurp requests separately from your application
Import the MailSlurp API definition into a ReadyAPI project. Use https://api.mailslurp.com as its base URL and add the x-api-key header to MailSlurp requests. Set its value from your private test environment; do not save a real key in a shared project export.
Your application's API has its own base URL and authentication. Do not forward the MailSlurp API key to its signup or confirmation endpoints. Use Content-Type: application/json for the JSON POST requests below.
Create TestCase properties named inboxId, emailAddress, requestedAt, emailId and otp. Clear them at the beginning of each run. Keep concurrent runs in separate test contexts rather than sharing mutable TestSuite properties.
Create the inbox and request the verification email
Add a REST request named Create inbox:
POST /inboxes?expiresIn=600000
x-api-key: YOUR_PRIVATE_ENVIRONMENT_VALUE
Assert a successful response before reading it. With a Property Transfer step, store response $.id as inboxId and $.emailAddress as emailAddress, selecting the TestCase as the target for both properties. Assert that both values are nonempty. The ten-minute expiry is a fallback for an interrupted run; choose a longer value if your full test requires it.
Immediately before the application's signup request, use a Groovy Script step to save the request boundary:
testRunner.testCase.setPropertyValue(
'requestedAt', java.time.Instant.now().toString()
)
Use ${#TestCase#emailAddress} in the application's signup payload, and assert that the application accepted the request. A signup response may mean "verification pending" rather than "account confirmed"; check the state your application promises.
Wait for the current message
Add a MailSlurp REST request named Wait for email. Set these query parameters in ReadyAPI's parameter editor so values are encoded correctly:
| Parameter | Value |
|---|---|
inboxId |
${#TestCase#inboxId} |
timeout |
30000 |
since |
${#TestCase#requestedAt} |
unreadOnly |
false |
Use GET /waitForLatestEmail and set the ReadyAPI request timeout above the API's 30-second wait budget. If the request has a response-time SLA assertion, give it a budget that also permits the full wait; a short response-time assertion would fail a valid delayed delivery. The API waits for mail without a fixed sleep in the test. The time boundary excludes earlier emails, while unreadOnly=false means opening a message in the dashboard does not hide it from this wait.
Assert the expected subject and recipient before storing response $.id as emailId. Use your application's real subject, not the example project's wording. If this inbox receives several message types or resends, use the matching wait API with the current request's subject or identifier. Do not treat whichever message arrived last as proof that the intended verification email arrived.
Extract one usable code
Add a MailSlurp request named Extract code:
POST /emails/${#TestCase#emailId}/codes
Content-Type: application/json
x-api-key: YOUR_PRIVATE_ENVIRONMENT_VALUE
For a six-digit template, use this body:
{
"method": "PATTERN",
"allowFallback": false,
"minLength": 6,
"maxLength": 6,
"maxCandidates": 5
}
Assert HTTP success first. Add a following Groovy Script step that checks the result before storing it. This preserves a leading zero and rejects a missing or ambiguous value:
import groovy.json.JsonSlurper
def result = new JsonSlurper().parseText(context.expand('${Extract code#Response}'))
assert result.found == true : 'No OTP found in the received email'
assert result.code instanceof String : 'OTP must remain a string'
assert result.code ==~ /[0-9]{6}/ : 'Expected six digits'
assert result.candidates instanceof List : 'Missing candidate details'
assert result.candidates.every { it instanceof Map && it.code instanceof String } : 'Invalid candidate'
def codes = result.candidates.collect { it.code }.toSet()
assert codes.size() == 1 && codes.contains(result.code) : 'Ambiguous OTP candidates'
testRunner.testCase.setPropertyValue('otp', result.code)
Adapt the length and template rules to your application. The code extraction API guide explains additional options. Do not silently take the first number from an email containing several candidate codes.
If an existing project uses the regex endpoint instead, its path is POST /emails/${#TestCase#emailId}/contentMatch, with the actual message ID substituted. A URL fragment such as #emailId is not a message ID path parameter. Check that the response contains the expected capture group before reading it.
Confirm the account and verify login
Send ${#TestCase#otp} and the expected account identity through your application's confirmation endpoint. Use the request format defined by your application, and keep sensitive values out of shared reports and URL logs.
Assert the application's verified state. Then make a fresh login or protected-account request and check the expected user identity. A 2xx response alone may only acknowledge the request; it does not establish that the intended account became accessible.
Keep invalid, expired, consumed and resent-code scenarios separate. They change account or challenge state, and should not depend on the happy path running first. Email verification by itself is not automatically two-factor authentication; when the application requires a password and an independent factor, test both required steps.
Clean up after success and failure
Configure TestCase teardown to delete the owned inbox with DELETE /inboxes/${#TestCase#inboxId} when its ID is present. Ensure this cleanup runs after assertion failures as well as success, and report a failed cleanup request rather than hiding it. Remove the application's test user separately through your supported test-data process.
Clear otp after use. Retain message and inbox references when needed for restricted debugging, but exclude usable codes, raw message bodies and API keys from shared logs. Run the same TestCase twice and concurrently with a separate context to check that no previous properties or inboxes leak into the next run.