Send the SMS from your app (or send a test message straight to the inbox), then call GET https://api.otpmock.com/v1/inbox/{phone}/code?since={unix_ms} with your API key until it returns 200. It returns 404 until a code arrives, so poll every few hundred milliseconds with a timeout. The loops below do exactly that in Bash and PowerShell.
Step 1: set up the key
Create a free key in your dashboard and export it as an environment variable. Never paste it into a script that you commit.
export OTPMOCK_API_KEY=om_live_...Step 2: send a test message
Before wiring your app, check the inbox works by sending a message to it directly. This uses the provider-neutral API and needs no SDK:
curl https://api.otpmock.com/v1/messages/send \
-H "Authorization: Bearer $OTPMOCK_API_KEY" \
-d to=+15550142 -d "body=Your code is 4819"The message appears in your dashboard and in the inbox API. In a real test your application sends the SMS through its normal provider SDK, pointed at otpmock in the test environment; see the provider pages.
Step 3: read the code
# The "+" in the phone number must be URL-encoded as %2B
curl https://api.otpmock.com/v1/inbox/%2B15550142/code \
-H "Authorization: Bearer $OTPMOCK_API_KEY"
{ "code": "4819", "messageSid": "SM6926…", "receivedAt": 1791406301420 }If no message has arrived yet the API returns 404 with {"error":"no_code_yet"}. Add ?since=<unix_ms> to ignore anything older than a moment you choose, such as just before your app sent the SMS.
Step 4: wait for the code in Bash
This script polls until the code arrives. It needs curl and jq.
#!/usr/bin/env bash
set -euo pipefail
phone="${1:?usage: wait-for-code.sh +15550192834}"
enc="%2B${phone#+}" # URL-encode the leading +
since=$(( $(date +%s) * 1000 - 5000 )) # ignore older codes; 5 s margin for clock skew
# ... trigger the SMS here, for example: curl -X POST https://staging.example.com/api/send-code -d "phone=$phone"
for _ in $(seq 1 50); do # about 15 seconds
res=$(curl -s -w '\n%{http_code}' -H "Authorization: Bearer $OTPMOCK_API_KEY" \
"https://api.otpmock.com/v1/inbox/$enc/code?since=$since")
status="${res##*$'\n'}"
body="${res%$'\n'*}"
if [ "$status" = 200 ]; then
printf '%s\n' "$body" | jq -r .code
exit 0
fi
[ "$status" = 404 ] || { echo "otpmock returned HTTP $status" >&2; exit 1; }
sleep 0.3
done
echo "no code arrived for $phone" >&2
exit 1Use it in a smoke test: code=$(./wait-for-code.sh "$PHONE"), then submit $code to your verification endpoint with curl and check the response.
The same in PowerShell
param([Parameter(Mandatory)] [string] $Phone)
$headers = @{ Authorization = "Bearer $env:OTPMOCK_API_KEY" }
$since = [DateTimeOffset]::UtcNow.AddSeconds(-5).ToUnixTimeMilliseconds()
$url = "https://api.otpmock.com/v1/inbox/$([uri]::EscapeDataString($Phone))/code?since=$since"
# ... trigger the SMS here
$code = $null
for ($i = 0; $i -lt 50 -and -not $code; $i++) {
try {
$code = (Invoke-RestMethod -Uri $url -Headers $headers).code
} catch {
if ($_.Exception.Response.StatusCode.value__ -ne 404) { throw }
Start-Sleep -Milliseconds 300
}
}
if (-not $code) { throw "No code arrived for $Phone" }
$codeRunning it in CI
Put the key in your CI's secret store and expose it to the step as an environment variable. In GitHub Actions:
- name: Smoke test phone verification
env:
OTPMOCK_API_KEY: ${{ secrets.OTPMOCK_API_KEY }}
run: |
code=$(./wait-for-code.sh "+1555$(shuf -i 1000000-9999999 -n 1)")
echo "Received a ${#code}-digit code"Remember that the system under test must send its SMS through otpmock: set the otpmock option on the environment where your app runs, not only on the CI runner. To start a run with an empty inbox for a number, call curl -X DELETE https://api.otpmock.com/v1/inbox/%2B15550142 -H "Authorization: Bearer $OTPMOCK_API_KEY".
Common pitfalls
- An unencoded
+. In a URL path the plus sign must be%2B. Otherwise the number does not match. - Sleeping instead of polling. A fixed
sleep 10is slow when the code is already there and flaky when it is not. Poll with a timeout. - Reading an old code. Always pass
sincetaken just before the SMS is triggered, or use a fresh number per run. - Printing the key. CI logs are widely readable. Do not
echothe key or run scripts withset -xwhile it is in the command line. - Real phone numbers. Use made-up numbers such as
+1555…; messages are deleted after 10 minutes either way.
FAQ
Do I need to install anything?
Only curl (and jq for the Bash version). PowerShell's Invoke-RestMethod has no dependencies. otpmock itself is a hosted service.
Can I use this to test any SMS provider?
Yes, if your application can be pointed at otpmock. otpmock emulates fifteen provider APIs, and anything else can send through the provider-neutral endpoint shown in step 2.
Is a shell script a good substitute for a test framework?
For smoke tests and debugging, yes. For a full suite, use the helper for your framework, for example Playwright or Postman and Newman; the HTTP call is the same.
The free plan includes 300 messages a month. No card required.
Get a free API key