SMS on your Twilio

Base URL: https://chatsa.co/api/v1

Send and receive texts (SMS and MMS) on a business's own Twilio account: its own numbers, its own carrier registration (A2P 10DLC or toll-free verification) and its own Twilio bill. ChatSa receives and sends the texts, keeps opt-outs and consent, enforces quiet hours, and sends every text and status to your webhooks.

Being rolled out. SMS is not on yet: ask ChatSa when it will be. Until then every request answers 503 sms_disabled.

Paths are under https://chatsa.co/api/v1/sms. Authenticate with an API key that has the sms:read, sms:send or sms:manage scope, or with a partner account key (which may do everything below). Phone numbers are E.164 with the plus (+14155550199); a number without a country code is read as North American. Rate limits per key: 60 writes and 600 reads a minute (429 rate_limited, with Retry-After).

A key limited to some chatbots sees and uses only the numbers connected to one of them.

1. Save the Twilio credentials#

The business copies four values from the Twilio console (Account → API keys & tokens):

ValueUsed for
Account SIDEvery call
API Key SID and secret (a Standard key made for ChatSa)Every request ChatSa makes to Twilio. The business can revoke it on its own.
Auth TokenOnly to check that webhooks really come from Twilio (Twilio signs them with it)
POST /api/v1/sms/credentials
Authorization: Bearer csa_…

{
  "account_sid": "AC…",
  "api_key_sid": "SK…",
  "api_key_secret": "…",
  "auth_token": "…",
  "time_zone": "America/Los_Angeles"
}

Scope sms:manage, with a key that is not limited to chatbots (403 forbidden otherwise). ChatSa checks the key and the Auth Token with Twilio before saving. Secrets are stored encrypted and never returned. time_zone (optional, an IANA zone) is used for quiet hours only for customers outside North America. Saving again replaces every value, so send time_zone again to keep it.

{ "data": { "provider": "twilio", "account_sid": "AC…", "api_key_sid": "SK…", "friendly_name": "Acme Plumbing", "time_zone": "America/Los_Angeles", "status": "active", "status_reason": null, "last_checked_at": "…" } }
ErrorWhen
400 invalid_requestA field is missing or malformed (the SIDs are AC or SK and 32 hex characters, the Auth Token 32 hex characters), or time_zone is unknown
409 numbers_connectedA different Account SID while numbers of the old one are connected
422 invalid_credentials, invalid_auth_token, account_inactiveTwilio refused the key, the token, or the account is suspended or closed
502 twilio_unavailableTwilio could not be reached

GET /credentials (sms:read) reads them back (or null). status is active or needs_reconnect: ChatSa checks every account hourly and flags it when Twilio refuses the API Key (deleted or rotated), refuses the Auth Token (rotated, so webhooks would fail), or the Twilio account is no longer active. A send or call that Twilio refuses for the key flags it at once. While flagged, sends, calls, connects and call settings answer 409 needs_reconnect; save the new credentials to fix it.

DELETE /credentials (sms:manage, a key not limited to chatbots) disconnects every number (putting back each one's previous webhook), forgets the credentials, and answers how many numbers it disconnected and how many webhooks it restored. Message history is kept.

2. Connect a number#

GET /numbers (sms:read) lists the Twilio account's numbers with capabilities (sms, mms, voice), where their texts and calls go now (sms_url, sms_application_sid, voice_url, voice_application_sid), sms_webhook (none, other or chatsa), connected, chatbot_id and connected_at. Before credentials are saved it answers 409 not_connected.

POST /api/v1/sms/numbers/PN0123…/connect

{ "replace": false, "chatbot_id": "6f1c…" }

Scope sms:manage.

  • ChatSa points only the number's incoming-text webhook at itself. Its voice set-up is never touched here (Calls does that).
  • Never silent: when the number's texts already go to another webhook or a TwiML app, ChatSa answers 409 webhook_in_use with current: { sms_url, sms_application_sid } and changes nothing. Send "replace": true to take it over. The old set-up is kept and put back when you disconnect.
  • chatbot_id (optional) ties the number to one of the workspace's chatbots, which decides where its events go (§5). A key limited to one chatbot uses that chatbot; a key limited to several must name one of them.
  • 201 connected now, 200 already connected (connecting again changes nothing, chatbot_id included).
ErrorWhen
404 number_not_foundNo such number on the Twilio account
409 number_in_useConnected to another ChatSa workspace
409 not_connected, needs_reconnectCredentials missing or refused
422 sms_not_supportedThe number can't send or receive texts
422 twilio_refusedTwilio refused to update the number (twilio_code)
502 twilio_unavailableTwilio could not be reached

If the number sits in a Twilio Messaging Service, set the service's Incoming Messages to "Defer to sender's webhook", or inbound texts won't reach ChatSa. Don't apply a Twilio webhook signing key (Webhooks configuration) to ChatSa's URLs: ChatSa checks Twilio's standard signature, made with the Auth Token, and refuses any other.

DELETE /numbers/{sid} (sms:manage) stops texts and calls on the number and answers sid, connected: false and webhook_restored. The number's previous set-up for texts and calls is put back while it still points at ChatSa; a number someone has pointed elsewhere since is left alone. Message history is kept, and connecting the same number again later keeps every customer's opt-out and consent from before: a customer who texted STOP stays opted out.

POST /numbers/{sid}/test (sms:manage) with { "to": "+1…" } sends a test text to your own phone (5 an hour per workspace). Asking for it records that phone's consent (source test) unless it has already opted in or out; opt-out and quiet hours still apply.

3. Send a text#

POST /api/v1/sms/messages
Authorization: Bearer csa_…

{
  "from_number": "+14155550123",
  "to": "+14155550199",
  "body": "Your technician arrives at 3 pm.",
  "media_urls": ["https://files.example.com/map.png"],
  "idempotency_key": "visit-8812-reminder"
}

Scope sms:send.

  • from_number: one of the workspace's connected numbers (404 number_not_found otherwise).
  • body (up to 1,600 characters) and/or media_urls (up to 10 public https links; the number must have MMS, 422 mms_not_supported otherwise).
  • idempotency_key is required (1–100 of A-Z a-z 0-9 . _ : -; the Idempotency-Key header works too). It is unique per workspace, for good: the same key answers the first result again (200); the same key with a different to, from_number or body answers 409 idempotency_key_reused.

Before sending, ChatSa checks, in order: opt-out (409 opted_out), consent (422 no_consent) and quiet hours (422 quiet_hours, with allowed_at and the customer's time_zones). A refusal by these checks does not use up the key. The key is used once Twilio has accepted the text, refused it (422 twilio_refused, or 409 opted_out with 21610) or timed out (202): send a corrected text with a new key.

AnswerMeaning
201Twilio accepted it (status: "queued" or "accepted")
200A replay of an earlier send with this key
202 with status: "unknown"Twilio timed out: the text may have gone. It is never sent again with this key.
409 opted_out with error_code: 21610Twilio refused it because the customer had replied STOP to this number; the opt-out is now recorded
409 not_connected, needs_reconnectCredentials missing or refused. Nothing was sent.
422 twilio_refusedTwilio refused it (id, status: "failed", error_code, for example 21211 invalid number)
429 twilio_rate_limitedTwilio throttled the account. Nothing was sent: retry with the same key.

The Message object (in answers and history):

{
  "id": "e7a1…",
  "direction": "outbound",
  "from": "+14155550123",
  "to": "+14155550199",
  "body": "Your technician arrives at 3 pm.",
  "media": [{ "content_type": null, "url": "https://files.example.com/map.png" }],
  "status": "queued",
  "error_code": null,
  "twilio_sid": "SM…",
  "chatbot_id": null,
  "created_at": "…",
  "status_at": "…"
}

An inbound text has direction: "inbound" and status: "received". Outbound statuses are Twilio's (queued, sent, delivered, undelivered, failed and the others), plus unknown after a timeout.

GET /messages?contact=+1… (sms:read) lists one customer's texts, both ways, newest first (from_number, before, limit 1–100, default 50). GET /messages/{id} reads one text, including one you didn't send yourself (a missed-call text, for example). An inbound MMS's media url is a path on ChatSa (/api/v1/sms/messages/{id}/media/{index}) that you fetch with a key that has sms:read (up to 5 MB each).

US sending needs the business's own A2P 10DLC campaign or toll-free verification in Twilio; ChatSa doesn't register on its behalf.

4. The rules ChatSa enforces#

Opt-out keywords. A single-word text (any case) of STOP, STOPALL, UNSUBSCRIBE, CANCEL, END, QUIT, REVOKE or OPTOUT opts the customer out of that number (STOPALL: every connected number of the workspace). START, UNSTOP or YES opts them back in (on a US toll-free number only START and UNSTOP). On a number in a Messaging Service with Twilio's Advanced Opt-Out, Twilio's own verdict decides, so its custom keywords count too. Twilio replies to the keyword itself, so neither ChatSa nor you should. Twilio's error 21610 (the customer had replied STOP before) records the opt-out too.

Consent. A customer's text or call to a number is consent to answer them on it, unless they opted out. Otherwise record consent first:

POST /api/v1/sms/consent

{ "phone": "+14155550199", "status": "opted_in", "from_number": "+14155550123", "source": "web_form", "note": "Estimate form, checkbox ticked 2026-11-30" }

Scope sms:send. status is opted_in or opted_out. Without from_number it applies to every connected number the key may use. source (up to 40 characters) is saved as api:web_form; note takes up to 500. The answer lists the customer's state on each number. A customer who opted out themselves (a keyword or error 21610) stays opted out until they text START: recording opted_in then answers 409 opted_out_by_recipient. An opt-out you recorded through the API can be undone through the API.

GET /consent?phone=+1… (sms:read) reads each number's state: number, phone, consent (none, opted_in or opted_out), source, note, consent_at, opted_out_at, keyword and last_inbound_at.

Quiet hours. No text before 8 am or from 9 pm at the customer's location. North American numbers use their area code's time zones (an area code that spans zones must be inside the window in all of them; an unknown area code, toll-free included, uses the four continental US zones). Others use the credentials' time_zone, else the sending number's area code, else UTC. allowed_at is the first quarter-hour the text may go. A reply within 30 minutes of the customer's own text or call may go at any hour.

5. Events#

Subscribe to sms.* or each event (Webhooks):

Eventdata
sms.receivedid, from, to, body, media: [{ content_type, url }], twilio_sid, received_at
sms.statusid, status, error_code, twilio_sid, from, to, at (only when the status moves forward, and at once when Twilio refuses a send)
sms.opted_outphone, number, source, keyword, at (STOPALL sends one per number)
sms.opted_inphone, number, source, keyword, at

phone is the customer; number is the business's Twilio number. source is keyword, twilio_21610, test, api or api:<your source>; keyword is the word that changed the state, or null.

Where events go (Three kinds of endpoint): the business's own dashboard endpoints get every number's events they subscribe to. For your app, the events of a number connected with a chatbot go to your Connect endpoint for that chatbot when it subscribes to them; otherwise, and for numbers without a chatbot, they go to your account endpoint. Never both.

ChatSa's own AI doesn't answer texts yet, and texts don't show in ChatSa's inbox: SMS is for your app.