Guides / cURL, Bash and PowerShell

How to test SMS OTP verification from the command line (cURL, Bash, PowerShell)

You do not need a test framework to check an SMS flow. A mock inbox is just an HTTP API, so a shell script can trigger the SMS, wait for the code and use it. That is handy for smoke tests after a deploy, for CI steps and for debugging.

Updated November 12, 2026
Short answer

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.

terminal
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:

terminal
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

terminal
# 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.

wait-for-code.sh
#!/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 1

Use 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

wait-for-code.ps1
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" }
$code

Running 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:

.github/workflows/smoke.yml
- 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

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.

Try it from your terminal

The free plan includes 300 messages a month. No card required.

Get a free API key

Related guides