Wait for an email in a shell script
A shell script is the lowest common denominator of test tooling: it runs in every CI system, in a Docker image with nothing but curl, and next to a service written in any language. This guide waits for a sign-up email from a bash script, extracts the one-time code with jq, and asserts that the application accepts it. It also shows the same check with the command-line mode of the AssertInbox client file, which removes the retry loop.
What you need
- Bash 4 or newer,
curlandjq. The command-line variant also needs Node 18 or newer. - A staging or local application whose sign-up endpoint sends a real email containing a six-digit code. The script assumes
POST /signupwith anemailfield andPOST /verifywithemailandcode; change the two calls to match your app, and theverifysubject filter to a word from your own subject. - An AssertInbox key from /start. One click creates a key and a private namespace, with no email address or card. Export
ASSERTINBOX_KEY(theapi_key) andASSERTINBOX_NAMESPACE(thenamespace) in your shell or your CI secrets. Use inboxes only for services you own or control; see the terms.
How the wait works
Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait holds the request open for up to 25 seconds and returns the newest matching message as JSON, with otp and magic_link already extracted. When nothing arrives in time it answers 408. That is the normal "not yet" answer, so the script asks again instead of failing; see the limits page for every status.
The script
Save this as signup-email-check.sh and run it with APP_URL=https://staging.example.com bash signup-email-check.sh. The function test_signup_sends_working_code is the test; the helpers above it are reusable.
#!/usr/bin/env bash
# signup-email-check.sh: sign up on a staging app, read the code from the email, verify it.
set -euo pipefail
API="${ASSERTINBOX_URL:-https://assertinbox.com}"
DOMAIN="${ASSERTINBOX_DOMAIN:-assertinbox.com}"
APP="${APP_URL:-http://localhost:3000}"
: "${ASSERTINBOX_KEY:?set ASSERTINBOX_KEY}"
: "${ASSERTINBOX_NAMESPACE:?set ASSERTINBOX_NAMESPACE}"
fail() { echo "FAIL: $*" >&2; exit 1; }
# A tag no other run uses: the clock, this process id and a random number.
new_tag() { echo "sh-$(date +%s)-$$-$RANDOM"; }
# wait_for_email TAG [SUBJECT]: prints the newest matching message as JSON.
# /v1/wait holds the request open for up to 25 seconds and answers 408 when
# nothing arrived, so a 408 is not an error: ask again, up to three times.
wait_for_email() {
local tag="$1" subject="${2:-}" body status attempt
body=$(mktemp)
for attempt in 1 2 3; do
status=$(curl -s --max-time 40 -o "$body" -w '%{http_code}' -G "$API/v1/wait" \
-H "Authorization: Bearer $ASSERTINBOX_KEY" \
--data-urlencode "tag=$tag" --data-urlencode "timeout=25" \
--data-urlencode "subject=$subject") || status=000
case "$status" in
200) cat "$body"; rm -f "$body"; return 0 ;;
408) ;;
*) rm -f "$body"; fail "AssertInbox answered HTTP $status" ;;
esac
done
rm -f "$body"
fail "no email arrived for tag $tag"
}
delete_emails() {
curl -s --max-time 20 -X DELETE -G "$API/v1/messages" \
-H "Authorization: Bearer $ASSERTINBOX_KEY" \
--data-urlencode "tag=$1" >/dev/null || true
}
test_signup_sends_working_code() {
local email message code
tag=$(new_tag)
email="$ASSERTINBOX_NAMESPACE.$tag@$DOMAIN"
trap 'delete_emails "$tag"' EXIT
curl -fsS -X POST "$APP/signup" --data-urlencode "email=$email" >/dev/null ||
fail "the sign-up request failed"
message=$(wait_for_email "$tag" verify)
code=$(jq -r '.otp // empty' <<<"$message")
[[ "$code" =~ ^[0-9]{6}$ ]] || fail "expected a six-digit code, got '$code'"
curl -fsS -X POST "$APP/verify" \
--data-urlencode "email=$email" --data-urlencode "code=$code" >/dev/null ||
fail "the app did not accept the code"
echo "ok: the sign-up email carried a working code"
}
test_signup_sends_working_code
Four details carry the weight:
- A unique tag per run.
new_tagcombines the clock, the process id and$RANDOM, so two jobs that run at the same time never read each other's mail.$RANDOMis a bash feature; in plainshuse$(od -An -N3 -tx1 /dev/urandom | tr -d ' '). - The status code decides.
curl -swithout-fkeeps the 408 visible, and-w '%{http_code}'prints it. Anything other than 200 or 408 stops with the status in the message, which is how a wrong key (401) or the daily limit (429) shows up immediately. - Cleanup in a trap. The
EXITtrap deletes the test's messages whether the script finishes orfailstops it (tagis deliberately notlocal, so the trap can still read it). Messages are deleted after 24 hours anyway. - Assert the shape, not the value. The code changes on every run, so the test checks that it is six digits and that the app accepts it.
The same check with the command-line mode
The client file assertinbox.mjs also runs as a command. Download it next to the script (see the client files page) with curl -O https://assertinbox.com/sdk/assertinbox.mjs. It reads the same two environment variables, retries a 408 twice by itself, prints only the value you ask for and never prints the key. Its exit code is 0 on success, 1 on a timeout, a missing code or an API error, and 2 on a usage error, so set -e does the failing for you.
#!/usr/bin/env bash
# The same check with the command-line mode of assertinbox.mjs (Node 18 or newer).
set -euo pipefail
APP="${APP_URL:-http://localhost:3000}"
CLI="node assertinbox.mjs"
tag="sh-$(date +%s)-$$-$RANDOM"
email=$($CLI address "$tag")
trap '$CLI delete "$tag" >/dev/null || true' EXIT
curl -fsS -X POST "$APP/signup" --data-urlencode "email=$email" >/dev/null
code=$($CLI otp "$tag" --subject verify) # exit 1 on a timeout or when the email has no code
[[ "$code" =~ ^[0-9]{6}$ ]]
curl -fsS -X POST "$APP/verify" \
--data-urlencode "email=$email" --data-urlencode "code=$code" >/dev/null
echo "ok: $email verified with $code"
Use link instead of otp for a magic-link flow, and wait to get the whole message as JSON for jq.
Pitfalls in shell tests
set -edoes not see everything. A failure insidea=$(cmd) || ...or in the left side of&&is handled by you. That is why the script tests the code with[[ ... ]] || failand checks the sign-up call explicitly.- Quote everything. Use
--data-urlencodefor values, so a subject with spaces or an address with a plus sign survives. - Do not put the key on the command line of a shared runner. It is in the header of
curl, which other users of the same machine may see in the process list. In shared CI, prefer the command-line mode, which reads it from the environment, or a curl config file passed with-K. - Parallel jobs. Unique tags make them safe; the daily request allowance is per key, not per tag.
Troubleshooting
- "no email arrived". The app did not send the mail, or sent it to another address. Print
$email, compare it with the app's log, and look at the message in the inbox viewer. jqprints nothing for the code. Print$message. Ifotpis null, the email has no code the detector recognised; thetextfield holds the body so you can parse it yourself.- HTTP 401 or 429. The key is missing or wrong, or the key used its 2,000 requests for the day.
- Works locally, hangs in CI. The CI runner may not be allowed to reach the application's mail provider, or the job's own timeout is shorter than three 25-second waits.
Next steps
For unique tags, the trigger-then-wait order, retries and cleanup in one place, read Reliable email tests. More stacks are on the guides page.