#!/usr/bin/env bash # # SendPier Send API — send a single message. # # Requirements: bash and curl. Nothing else. # # Usage: # export SENDPIER_API_KEY='sp_live_...' # the key of one verified domain # export SENDPIER_DOMAIN='example.com' # that same domain # export SENDPIER_FROM='noreply@example.com' # must live on that domain # ./send-message.sh recipient@example.org # # A successful call answers 202 Accepted and returns one identifier per # recipient. 202 means "accepted for delivery", not "delivered" -- ask # get-message.sh later, or let a webhook tell you. 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}" : "${SENDPIER_FROM:?set SENDPIER_FROM to a sender address on that domain}" recipient="${1:-}" if [ -z "$recipient" ]; then echo "usage: $0 " >&2 exit 64 fi # The body is built with a here-document so the quoting stays readable. Anything # you interpolate here has to be valid JSON -- see send-with-template.sh for the # safer route when the values come from your own users. body=$(cat <This message was sent with curl.

" } JSON ) # -sS keeps curl quiet but still prints transport errors; -w writes the status # code on its own last line so we can read it without parsing the JSON. response=$(curl -sS -X POST "${API_BASE}/v1/messages" \ -H "Authorization: Bearer ${SENDPIER_API_KEY}" \ -H "X-Domain-Name: ${SENDPIER_DOMAIN}" \ -H 'Content-Type: application/json' \ -H 'Accept: application/json' \ -w '\n%{http_code}' \ -d "$body") status=$(printf '%s' "$response" | tail -n1) payload=$(printf '%s' "$response" | sed '$d') echo "$payload" if [ "$status" != '202' ]; then # Always log the status code *and* the body: the body carries the error code # that says what to change. Retry 429 and 503 with a growing wait; a 422 will # fail again in exactly the same way. echo "request failed with HTTP ${status}" >&2 exit 1 fi echo "accepted (HTTP 202)" >&2