AssertInbox

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

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:

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

Troubleshooting

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.