Working with the API

Errors

One shape for every failure, and how to branch on it.

Every failure answers with the same envelope:

{
  "code": "invalid_address",
  "message": "none of the numbers is a valid phone number",
  "error": {
    "type": "invalid",
    "code": "invalid_address",
    "reason": "none of the numbers is a valid phone number",
    "field": "to",
    "retryable": false,
    "request_id": "46fb5b754b141e69c55e764ed182d160"
  }
}
FieldUse it for
codeBranching. This is the contract, and it changes only with notice.
typeHandling a whole class of failures at once.
reasonShowing a person. It is reworded freely: never match on it.
messageThe same text as reason, at the top level.
fieldWhich input to mark.
resourceWhich kind of thing was missing or not set up.
retryableWhether repeating the same request could work.
request_idQuoting to support.

field, resource and reason are left out when empty. Several failures share a code and differ in field: switch on code, then use field to decide which input to mark.

Codes you will meet first

StatuscodeMeaning
400missing_fielda required input was absent; field names it
400invalid_valueoutside the allowed set or breaking a limit. A body that is not JSON is this, with field: "Content-Type"
400invalid_jsonthe body did not parse
400unknown_fielda property this endpoint does not accept; field is the misspelling
400invalid_addressa phone number would not parse
401unauthorizedsee Keys and scopes
402insufficient_fundsthe wallet cannot cover the send; nothing was sent
403insufficient_scopethe key lacks the scope
403sender_id_not_approvedyour workspace may not send as this sender ID
403forbiddennot allowed, such as a campaign sent with a test key
404not_foundresource says what was missing
409idempotency_key_reusedsee Idempotency
409idempotency_in_progressthe first request with this key is still running; retry shortly
429rate_limitedsee Rate limits
502provider_rejectedthe network refused the send; it is recorded as failed and not charged
503provider_unavailablethe network did not answer; check the message's status before sending again

Unknown properties are refused, not ignored, so a misspelled field never silently does nothing:

curl https://v3.ghala.io/v1/messages \
  -H "Authorization: Bearer $GHALA_KEY" \
  -H "Content-Type: application/json" \
  -d '{"channel": "sms", "from": "ACME", "too": ["255700000001"], "type": "text", "text": "Hi"}'
{
  "code": "unknown_field",
  "message": "the request body has a property this endpoint does not accept",
  "error": { "type": "invalid", "code": "unknown_field", "field": "too", "retryable": false }
}

retryable

It is true only for failures that are a matter of timing, such as rate_limited (after Retry-After), idempotency_in_progress, provider_unavailable, unavailable and internal. Everything else is false: repeating a request with a bad number will never succeed.

Our own failures and a provider's answer with a fixed sentence for the code, never internal detail. The detail is logged against the request_id.

On this page