Connect with ChatSa

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

"Connect with ChatSa" lets businesses that already use ChatSa connect one of their chatbots to your app with one button, the way "Sign in with…" works. Nobody copies an API key or webhook secret. It is OAuth 2.0 authorization code with PKCE (S256), and the token you receive is a ChatSa API key limited to the one chatbot the business chose.

1. Register your app#

In ChatSa, open Dashboard → Developers → Apps and press Register an app:

FieldNotes
Product name2 to 60 characters, shown to the business on the approval page. Use your product's name.
Arabic nameOptional, shown to Arabic readers
WebsiteYour product's site
Support e-mailWhere businesses reach you about the connection
Redirect URIsWhere ChatSa sends the business back, one per line, up to 10. Matched exactly as you typed them, character for character (a trailing slash counts). https only, except http://localhost and http://127.0.0.1 for development; no user name, password or #fragment.

You get a client id and a client secret. The secret is shown once: store it as a server secret. On the app's page you can later:

  • Rotate secret: a new secret, shown once; the old one stops working at once.
  • Change the names, website, support e-mail and redirect URIs under Details.
  • Delete the app: every chatbot connected to it is disconnected (their tokens stop working and the webhooks and connected-app settings they made are removed). An app that has created ChatSa accounts through the partner API can't be deleted while those accounts exist: delete them first.

Registering is free. A new app is on the standard tier: businesses with a ChatSa account connect to it. To create ChatSa accounts for your own customers, apply for the account-creation tier: see Partner tiers.

Your app may ask for these scopes:

ScopeAllows
whatsapp:readNumber status, display name, templates, threads, media links, voice settings
whatsapp:sendSend messages, mark read, take a conversation over
whatsapp:voiceVoice previews and trained voices
whatsapp:manageCreate and delete templates, change who answers, request a display name change, voice-reply settings

2. Send the business to ChatSa#

On your server, make a random state (up to 500 characters) bound to the signed-in user and a PKCE code_verifier (43–128 characters from A-Z a-z 0-9 - . _ ~). Keep both server-side. Then open, in a popup or a full page:

https://chatsa.co/connect/authorize
  ?response_type=code
  &client_id=<your client id>
  &redirect_uri=<one of your registered redirect URIs>
  &scope=whatsapp:read%20whatsapp:send
  &state=<state>
  &code_challenge=<BASE64URL(SHA256(code_verifier))>
  &code_challenge_method=S256
  &locale=en

scope is one or more of the scopes above, separated by spaces. locale (en or ar) is optional. A bad request (unknown client, unregistered redirect URI, a response_type other than code, an unknown scope or one your app may not ask for, no state or one over 500 characters, no S256 challenge) shows an error page on ChatSa and never redirects.

import crypto from 'node:crypto';

const verifier = crypto.randomBytes(32).toString('base64url');
const challenge = crypto.createHash('sha256').update(verifier).digest('base64url');
const state = crypto.randomBytes(16).toString('base64url');

3. On ChatSa#

The business signs in (or signs up), then sees three steps: signed in; choose a chatbot (or create one); the chatbot's WhatsApp number connected (or a link to connect it). Only owners and admins of a chatbot's workspace can connect it. They may approve before the number is connected: your app should show that step as unfinished and link back with links.whatsapp from GET /connect.

4. Back to you#

  • Approve: redirect_uri?code=…&state=…
  • Cancel: redirect_uri?error=access_denied&state=…

Check state against the one you stored before anything else.

5. Exchange the code#

Within 10 minutes, once, from your server:

POST /api/v1/connect/token
Content-Type: application/json

{
  "grant_type": "authorization_code",
  "code": "csc_…",
  "redirect_uri": "https://app.example.com/chatsa/callback",
  "client_id": "<your client id>",
  "client_secret": "<your client secret>",
  "code_verifier": "<verifier>"
}

Form-encoded bodies (application/x-www-form-urlencoded) work too.

{
  "data": {
    "access_token": "csa_…",
    "token_type": "bearer",
    "scope": "whatsapp:read whatsapp:send",
    "chatbot": { "id": "6f1c…", "name": "Acme Plumbing", "phone_number": "14155550123", "mode": "ai", "connected": true, "coexistence": false }
  }
}

Store the token encrypted. It is shown once and doesn't expire.

ErrorWhen
401 invalid_clientUnknown or deleted client id, or wrong secret
400 invalid_grantThe code is unknown, expired or used, or the redirect URI or PKCE verifier doesn't match. The code is burned: start again.
400 invalid_requestA field is missing
429 rate_limitedMore than 60 exchanges a minute for this client

6. Set up the connection#

With the access token as a bearer token. These calls accept only Connect tokens and the chatbot keys of the partner API: no token is 401 unauthorized, any other key 403 app_token_required, and a chatbot that was deleted 404 not_found. 30 calls a minute per token.

PUT /connect/webhook: your endpoint for this chatbot's events.

{ "url": "https://app.example.com/webhooks/chatsa/conn_123", "events": ["message.*", "contact.*", "connection.status", "template.status", "handoff.requested"] }

→ { "data": { "id", "events", "secret": "whsec_…" } }. One endpoint per connection, receiving only this chatbot's events (never the account's events with chatbot_id: null, such as payment.succeeded and campaign.replied). Every call replaces it and returns a new signing secret, once. An unknown event is 400 invalid_request, a private address 400 invalid_url. About 3 seconds later ChatSa sends a signed ping (data: { "connection": "ok" }): receiving it proves the webhook works. See Webhooks.

PUT /connect/context: make your app the chatbot's connected app, so ChatSa's AI can ask your system who a customer is and pass on questions only your data can answer.

{ "url": "https://app.example.com/chatsa/context/conn_123", "ask_when": "Questions about the customer's own jobs, quotes and invoices." }

url must be a public https address (400 invalid_url otherwise); ask_when is optional, up to 600 characters. → { "data": { "secret": "csk_…" } }, a new secret on every call. ChatSa then calls your URL with a signed POST (X-ChatSA-Call, X-ChatSA-Timestamp, and X-ChatSA-Signature-V1 over `${timestamp}.${rawBody}`, like webhooks) of two kinds:

  • customer ({ call, chatbot_id, channel, phone }, channel being whatsapp or voice for a phone call): answer within 4 seconds (3 on a phone call) with { "known": true, "name": "Jordan", "about": "…instructions about this customer, up to 1,500 characters…" }.
  • ask (adds conversation_id, question, persona, language): answer within 100 seconds with { "reply": "…", "images": [{ "mime_type": "image/png", "data_base64": "…", "caption": "…" }] } (at most 2 PNG or JPEG images of 5 MB each). The reply is sent to the customer as written.

GET /connect: the connection's state: app, scopes, chatbot, webhook (url, events) or null, context (url, enabled) or null, and links.chatbot / links.whatsapp to send the business back to ChatSa to finish a step.

DELETE /connect: revokes the token and removes the webhook and connected app it set up. { "data": { "revoked": true } }.

Messages, threads, templates, takeover and voice then use the same token with the WhatsApp messaging API.

Revoking#

The business disconnects your app under Developers → Connected apps in ChatSa, or you call DELETE /connect. Either way the token stops working at once, its webhook endpoint is deleted and the connected app it set up is removed. Treat 401 from any call as "disconnected" and offer to connect again.

Security notes#

  • ChatSa stores only hashes of client secrets, codes and tokens. A code lives 10 minutes, is used once and is bound to your app, the redirect URI, the chatbot, the scopes and the PKCE challenge.
  • The approval page checks everything again on the server when the business approves.
  • Webhook and connected-app URLs must be public addresses; connected-app URLs must be https.