Skip to content
View as Markdown
Tutorial

Make your first API call

Create an API key, find a customer and send them a WhatsApp message from your own code, in about 10 minutes.

10 minutesDevelopersUpdated

By the end of this tutorial you’ll have an API key, you’ll have found a customer with the API, and you’ll have sent them a WhatsApp message from your own code. The examples send Priya Sharma an update about her order.

  • A Soosh workspace with a WhatsApp number connected, where you’re the owner or an admin
  • A customer who has messaged your number in the last 24 hours (you can send them anything for 24 hours after their last message)
  • A terminal with curl, or Node.js 18 or later, or Python 3 with requests

An API key lets your code act in Soosh as you, but only for the actions you allow it.

  1. In Soosh, go to Settings > API keys (under Developers) and click Create API key.

  2. In Name, enter where the key will be used, like Order system.

  3. Under Key scopes, open Contacts and tick View next to Contacts.

  4. Open Conversations and tick View and Create and edit next to Conversations.

  5. Click Create.

    1. Pin 1: Contacts: View, to find customers.
    2. Pin 2: Conversations: View and Create and edit, to send messages and check on them.

The API key created window shows your key. It starts with soosh_. Copy it now: Soosh shows it only once.

In your terminal, keep the key in a variable so it isn’t typed into every command:

Terminal window
export SOOSH_API_KEY="soosh_paste_your_key_here"

Every request goes to https://app.soosh.io/api/ with your key in the X-API-Key header. Search your contacts for Priya:

Terminal window
curl "https://app.soosh.io/api/contacts?search=Priya" \
-H "X-API-Key: $SOOSH_API_KEY"

You get back the contacts that match, newest chat first:

{
"status": "success",
"data": {
"contacts": [
{
"id": "4ea0d485-435d-407f-837a-c7d67095f23a",
"phone_number": "919876543210",
"name": "Priya Sharma",
"tags": ["VIP"],
"last_message_preview": "I love the peacock blue one. Can you deliver it to Pune by Friday?",
"service_window_open": true,
"marketing_opt_out": false
}
],
"total": 1,
"page": 1,
"limit": 50
}
}

(Some fields are left out here.) Copy Priya’s id; the next request uses it. service_window_open is true, so you can send her any message right now.

Send Priya a text message, with her id in the path. In each example, replace 4ea0d485-… with the id you copied:

Terminal window
CONTACT_ID="4ea0d485-435d-407f-837a-c7d67095f23a"
curl "https://app.soosh.io/api/contacts/$CONTACT_ID/messages" \
-H "X-API-Key: $SOOSH_API_KEY" \
-H "Content-Type: application/json" \
-d '{"type": "text", "content": {"body": "Good news, Priya! Your peacock blue saree has shipped and arrives by Friday."}}'

Soosh answers straight away, before WhatsApp has the message, so its status is pending:

{
"status": "success",
"data": {
"id": "7c678987-a6fa-4a6e-b333-c57204c43eb7",
"contact_id": "4ea0d485-435d-407f-837a-c7d67095f23a",
"direction": "outgoing",
"message_type": "text",
"content": { "body": "Good news, Priya! Your peacock blue saree has shipped and arrives by Friday." },
"status": "pending"
}
}

Ask for the latest message in Priya’s chat:

Terminal window
curl "https://app.soosh.io/api/contacts/$CONTACT_ID/messages?limit=1" \
-H "X-API-Key: $SOOSH_API_KEY"

Its status is now sent, and becomes delivered and then read as Priya’s phone receives and opens it. If it says failed, error_message says why. The most common reason is that more than 24 hours have passed since the customer last wrote; then you need to send a template instead.

The message also appears in Priya’s chat in the Soosh inbox, sent by you, because the key acts as the person who created it.

  • An API key carries only the scopes you tick, and is shown once.
  • Every request sends the key in the X-API-Key header to https://app.soosh.io/api/.
  • GET /api/contacts?search= finds a customer; their id addresses them in other requests.
  • POST /api/contacts/{id}/messages sends a message, which starts as pending and moves to sent, delivered and read.
  • Reference: API keys, including errors such as Invalid API key
  • Reference: Messages API, for media, buttons and templates
  • Reference: Webhooks, to hear about replies instead of asking for them