BrowserStack Integration
BrowserStack Integration
MailSlurp fits BrowserStack best when BrowserStack drives the browser UI and MailSlurp handles email state over HTTP. A common pattern is to create a fresh inbox for the test, submit that address in your app's sign-up form, wait for the OTP email, extract the code, and then type the code back into the BrowserStack session.
How MailSlurp links into the test
The test suite orchestrates both sides of the flow: it uses BrowserStack to drive the browser and MailSlurp APIs to create inboxes, wait for messages, and extract the OTP needed to finish the sign-up flow.
Authenticate API requests
Create an API key in the MailSlurp dashboard. MailSlurp accepts API keys in either of these forms:
- Recommended:
x-api-key: YOUR_API_KEYrequest header - Alternative:
?apiKey=YOUR_API_KEYquery parameter
Header-based authentication:
POST https://api.mailslurp.com/inboxes/withDefaultsx-api-key: YOUR_API_KEYAccept: application/jsonQuery-parameter authentication:
POST https://api.mailslurp.com/inboxes/withDefaults?apiKey=YOUR_API_KEYAccept: application/jsonUse the header form by default. It is easier to manage in helpers, CI secrets, and BrowserStack test configuration.
Typical BrowserStack OTP workflow
Imagine your app has a user sign-up form with email, password, and verification code fields.
- Start the BrowserStack test and open the sign-up page.
- Call
createInboxWithDefaultsto create a fresh MailSlurp inbox for this test run. - Put the returned
emailAddressinto the sign-up form and submit it. - Let your app send the OTP email.
- Call
waitForLatestEmailwith the new inbox ID and wait for the verification email. - Call
getEmailContentMatchwith the email ID to extract the OTP from the message body. - Type the OTP into the verification form and assert the user reaches the expected logged-in page.
If you are using BrowserStack's low-code tooling, keep these MailSlurp calls in a small helper script, test task, or API action that runs alongside the browser session.
1. Create an inbox for the sign-up flow
createInboxWithDefaults not found.API_KEY=your_api_key
INBOX_JSON=$(curl -sS -X POST "https://api.mailslurp.com/inboxes/withDefaults" \ -H "x-api-key: ${API_KEY}" \ -H "Accept: application/json")
INBOX_ID=$(printf '%s' "$INBOX_JSON" | jq -r '.id')EMAIL_ADDRESS=$(printf '%s' "$INBOX_JSON" | jq -r '.emailAddress')
printf 'Use this address in the BrowserStack sign-up form: %s\n' "$EMAIL_ADDRESS"POST https://api.mailslurp.com/inboxes/withDefaultsx-api-key: YOUR_API_KEYAccept: application/jsonThe response includes both the inbox id and the generated emailAddress. Use that address for the new user registration step in BrowserStack.
2. Wait for the verification email
waitForLatestEmail not found.After BrowserStack clicks your app's Sign up, Continue, or Send code button, wait for the latest unread email in the new inbox:
EMAIL_JSON=$(curl -sS "https://api.mailslurp.com/waitForLatestEmail?inboxId=${INBOX_ID}&timeout=120000&unreadOnly=true" \ -H "x-api-key: ${API_KEY}" \ -H "Accept: application/json")
EMAIL_ID=$(printf '%s' "$EMAIL_JSON" | jq -r '.id')EMAIL_SUBJECT=$(printf '%s' "$EMAIL_JSON" | jq -r '.subject')
printf 'Received email %s with subject: %s\n' "$EMAIL_ID" "$EMAIL_SUBJECT"GET https://api.mailslurp.com/waitForLatestEmail?inboxId=INBOX_ID&timeout=120000&unreadOnly=truex-api-key: YOUR_API_KEYAccept: application/jsonBecause the inbox was created just for this test, unreadOnly=true keeps the request aligned with the message generated by the current sign-up attempt.
3. Extract the OTP code from the email body
getEmailContentMatch not found.getEmailContentMatch uses Java-style regex patterns. Add a capture group around the OTP so the extracted code is returned in matches[1].
MATCH_JSON=$(curl -sS -X POST "https://api.mailslurp.com/emails/${EMAIL_ID}/contentMatch" \ -H "x-api-key: ${API_KEY}" \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ --data '{"pattern":"(?:verification code|OTP|code)[:\\s-]*(\\d{6})"}')
OTP_CODE=$(printf '%s' "$MATCH_JSON" | jq -r '.matches[1]')
printf 'OTP code: %s\n' "$OTP_CODE"POST https://api.mailslurp.com/emails/EMAIL_ID/contentMatchx-api-key: YOUR_API_KEYContent-Type: application/jsonAccept: application/json
{ "pattern": "(?:verification code|OTP|code)[:\\s-]*(\\d{6})"}Take OTP_CODE and enter it into the BrowserStack verification page to finish the sign-up flow.
End-to-end example sequence
This is the full MailSlurp side of a BrowserStack OTP test:
API_KEY=your_api_key
INBOX_JSON=$(curl -sS -X POST "https://api.mailslurp.com/inboxes/withDefaults" \ -H "x-api-key: ${API_KEY}" \ -H "Accept: application/json")INBOX_ID=$(printf '%s' "$INBOX_JSON" | jq -r '.id')EMAIL_ADDRESS=$(printf '%s' "$INBOX_JSON" | jq -r '.emailAddress')
# BrowserStack fills the sign-up form with $EMAIL_ADDRESS and submits it here.
EMAIL_JSON=$(curl -sS "https://api.mailslurp.com/waitForLatestEmail?inboxId=${INBOX_ID}&timeout=120000&unreadOnly=true" \ -H "x-api-key: ${API_KEY}" \ -H "Accept: application/json")EMAIL_ID=$(printf '%s' "$EMAIL_JSON" | jq -r '.id')
MATCH_JSON=$(curl -sS -X POST "https://api.mailslurp.com/emails/${EMAIL_ID}/contentMatch" \ -H "x-api-key: ${API_KEY}" \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ --data '{"pattern":"(?:verification code|OTP|code)[:\\s-]*(\\d{6})"}')OTP_CODE=$(printf '%s' "$MATCH_JSON" | jq -r '.matches[1]')
# BrowserStack types $OTP_CODE into the verification form and asserts success here.Secondary option: visual and non-coded tests
If you prefer visual and non-coded tests try our MailSlurp test interface. It gives you a browser-based inbox view where you can sign in with your API key, create disposable inboxes, inspect received emails, and read OTP codes without wiring MailSlurp HTTP calls into your test runner first.
This approach is useful when you are building BrowserStack flows in a recorder or low-code runner, when you want a visible inbox during test authoring, or when you need to debug a verification email template before you move to API-based assertions.
A typical visual flow looks like this:
- Open
test.mailslurp.comin a BrowserStack step or browser tab. - Authenticate with your MailSlurp API key.
- Create an inbox in the UI and copy the generated email address.
- Paste that address into your app's sign-up form in BrowserStack.
- Return to the MailSlurp tab to wait for and inspect the OTP email.
- Copy the code from the email and enter it into the verification form.
The tradeoff is that the visual route is slower and less deterministic than direct API calls. It is best for exploratory testing, low-code setups, and debugging. For CI pipelines, repeatable OTP checks, and parallel BrowserStack runs, the HTTP API workflow above should stay the primary path.