#!/usr/bin/env bash # # SendPier Send API — look up one message, or list the most recent ones. # # Requirements: bash and curl. # # Usage: # export SENDPIER_API_KEY='sp_live_...' # export SENDPIER_DOMAIN='example.com' # ./get-message.sh # the five most recent messages # ./get-message.sh 20 # the twenty most recent messages # ./get-message.sh msg_5f1c9a... # one message by its identifier # # Mind the difference between the two answers: the list returns the internal # status of a message, the single lookup returns the public one (queued, # delivered, bounced, complained, failed). Compare like with like, or -- better # -- let a webhook tell you and stop asking. set -euo pipefail API_BASE='https://api.sendpier.com' : "${SENDPIER_API_KEY:?set SENDPIER_API_KEY to your Send API key}" : "${SENDPIER_DOMAIN:?set SENDPIER_DOMAIN to the domain the key belongs to}" argument="${1:-5}" if [ "${argument#msg_}" != "$argument" ]; then # An identifier: ask for exactly that message. url="${API_BASE}/v1/messages/${argument}" else # A number: the list endpoint. limit is clamped to 1..20, and defaults to 5. url="${API_BASE}/v1/messages?limit=${argument}" fi response=$(curl -sS "$url" \ -H "Authorization: Bearer ${SENDPIER_API_KEY}" \ -H "X-Domain-Name: ${SENDPIER_DOMAIN}" \ -H 'Accept: application/json' \ -w '\n%{http_code}') status=$(printf '%s' "$response" | tail -n1) payload=$(printf '%s' "$response" | sed '$d') echo "$payload" if [ "$status" != '200' ]; then # 404 message_not_found also covers "this identifier belongs to another # domain" -- the key only ever sees its own domain. echo "request failed with HTTP ${status}" >&2 exit 1 fi