Skip to content

Authentication and signed requests

Make a WizMessage API key, give it only the permissions it needs, and sign each request with HMAC-SHA256. With a check value and a client in four languages.

Each call carries an API key, and a new key also wants each call signed. The signature shows that the caller holds the secret, so a key alone that leaked is not enough to write to your customers.

Make a key

Open API Management in WizMessage and choose Connect your software. Give the key the name of the system that will use it. The last step shows the key (pk_live_…) and the secret (sk_live_…) once; if you lose the secret, make a new one with Rotate Secret, and the old one stops working at that moment.

A key belongs to one WhatsApp number. Its page is also where you set an end date, change its limits, switch it off, or stop asking for signatures.

Headers of a request

HeaderValue
X-API-KeyThe full key. Authorization: Bearer with the same key works as well
X-TimestampThe time in Unix seconds. It may be 5 minutes off at most
X-API-SignatureThe signature, see below

Permissions

A call without the permission it needs is answered with 403 and PERMISSION_DENIED. A new key has all five; take away what your system does not use.

PermissionWhat it allows
send_textText, reply buttons, lists, contact cards and reactions
send_mediaDocuments, images, video and audio
send_templateApproved templates
view_templatesReading the templates of the account
view_messagesDownloading files that customers sent

Uploading a file and looking up a message of the same key need none of them.

Sign a request

  1. Take the body exactly as you send it. A GET has none, so its text is empty. An upload signs only its text fields, as JSON; without text fields that text is empty too.
  2. Put the timestamp and a dot in front of it: timestamp.body.
  3. The key of the signature is the lowercase hex SHA-256 of your secret, as text.
  4. The signature is the lowercase hex HMAC-SHA256 of that text.

Sign the bytes that go over the wire. If a library writes the JSON again after you signed it, with other spaces or another order, the signature no longer fits.

Check your code

With these values your code must give this result. The secret is made up.

PartValue
Secretsk_live_EXAMPLE
Key of the signatureedc352292a316877f82d4e7dddc0d64e5e9c026e5d293b5612062bb32517c03d
Timestamp1764064800
Body{"to":"+447700900123","message":"Hello"}
Signature73d39ae5d146e7c86f32234c179111106d4e622ed4fcd40c377bef36cc159c3c
Signature of a GET at the same time7d529df740b2f980a30fa519cd7e2ebc34a602d1274474c885355d80a2c9afa7

A client for your language

These files need nothing but the standard library of the language. The other pages use them. Every send method takes the JSON body that its call takes.

wizmessage.sh
#!/usr/bin/env bash
# bash curl/wizmessage.sh <command> <to>. Run it without a command to see the list.
# Needs curl and openssl. The key and the secret come from the environment.
set -euo pipefail

BASE="${WIZMESSAGE_BASE_URL:-https://wizmessage.com/api/v1/external}"
SIGNATURE=()

# The signature covers the timestamp and the exact text of the body. For a GET and for an upload
# without text fields, that text is empty.
sign() {
  SIGNATURE=()
  [ -n "${WIZMESSAGE_API_SECRET:-}" ] || return 0
  local timestamp key value
  timestamp=$(date +%s)
  key=$(printf '%s' "$WIZMESSAGE_API_SECRET" | openssl dgst -sha256 -r | cut -d' ' -f1)
  value=$(printf '%s' "$timestamp.$1" | openssl dgst -sha256 -hmac "$key" -r | cut -d' ' -f1)
  SIGNATURE=(-H "X-Timestamp: $timestamp" -H "X-API-Signature: $value")
}

# A call can wait its turn for 30 seconds before WhatsApp answers, hence the long timeout.
call() {
  curl -sS --max-time 70 -H "X-API-Key: ${WIZMESSAGE_API_KEY:?is not set}" "${SIGNATURE[@]}" "$@"
}

post() {
  sign "$2"
  printf '%s' "$2" | call -X POST "$BASE$1" -H 'Content-Type: application/json' --data-binary @-
  echo
}

get() {
  sign ''
  call "$BASE$1" "${@:2}"
}

TO="${2:-}"
EXTRA="${3:-}"
VARIABLES="${4:-}"

case "${1:-}" in
  text)
    post /messages/text '{"to":"'"$TO"'","message":"Your parcel is ready to collect.","reference":"order-1042"}'
    ;;
  media)
    post /messages/media '{"to":"'"$TO"'","type":"document","link":"'"$EXTRA"'","filename":"Invoice 1042.pdf","caption":"Your invoice"}'
    ;;
  template)
    [ -n "$VARIABLES" ] || VARIABLES='{}'
    post /messages/template/send '{"to":"'"$TO"'","template_name":"'"$EXTRA"'","language":"en","variables":'"$VARIABLES"'}'
    ;;
  list)
    post /messages/interactive/list '{"to":"'"$TO"'","body":"When shall we deliver?","button":"Choose a time","sections":[{"title":"Tomorrow","rows":[{"id":"slot-am","title":"9:00 to 12:00"},{"id":"slot-pm","title":"13:00 to 17:00","description":"Ring the bell"}]}]}'
    ;;
  buttons)
    post /messages/interactive/button '{"to":"'"$TO"'","body":"Shall we confirm your visit?","buttons":[{"id":"yes","title":"Yes"},{"id":"no","title":"No"}]}'
    ;;
  contacts)
    post /messages/contacts '{"to":"'"$TO"'","contacts":[{"name":{"formatted_name":"Riya Example"},"phones":[{"phone":"+447700900456","type":"WORK"}]}]}'
    ;;
  reaction)
    post /messages/reaction '{"to":"'"$TO"'","message_id":"'"$EXTRA"'","emoji":"👍"}'
    ;;
  status)
    get "/messages/$TO"
    echo
    ;;
  templates)
    get '/templates?limit=50'
    echo
    ;;
  upload)
    sign ''
    call -X POST "$BASE/media/upload" -F "file=@$TO;type=$EXTRA"
    echo
    ;;
  download)
    get "/media/$TO" -o "$EXTRA" -w '%{http_code} %{content_type} %{size_download}\n'
    ;;
  *)
    cat <<'TEXT'
Commands:
  text|list|buttons|contacts <to>
  media <to> <link>
  template <to> <name> '{"customer_name":"Riya"}'
  reaction <to> <whatsapp message id>
  status <message id>    templates    upload <file> <type>    download <media id> <file>
TEXT
    ;;
esac

Keep it safe

  • Only on your server. Whoever holds the key and the secret can write to your customers in your name.
  • Only over https. The signature covers the time and the body, not the address of the call. Somebody who records a signed call can repeat it for 5 minutes.
  • One key for each system. You can then switch one off without stopping the others.
  • Rotate the secret when somebody who knew it leaves.

When a call is refused

StatusCodeMeaning
401INVALID_API_KEYThe key is missing or unknown.
401INVALID_SIGNATUREThe signature does not fit the body, or is missing on a key that needs it.
401TIMESTAMP_EXPIREDThe timestamp is more than 5 minutes off.
401EXPIRED_API_KEYThe key has passed its end date.
401REVOKED_API_KEYThe key was revoked.
401SUSPENDED_API_KEYThe key is switched off.
403IP_NOT_ALLOWEDThe key has an address list and this address is not on it.
403PERMISSION_DENIEDThe key lacks this permission. AI agent keys are always refused here.
403SUBSCRIPTION_REQUIREDNo active Wiz Bot or Wiz Pro plan, and the 7 days of grace are over.
403ACCOUNT_NOT_ACTIVEThe WhatsApp number of the key is not active.