1. Communication Apps
Salla Platform Docs
  • Overview
  • Get Started
  • Set Up Your Provider (Twillo)
  • App Channels Configuration
  • Build Your App Function
  • Test & Go Live
  • Examples
  • Event & Payload Reference
Merchant
Merchant
  • Merchant API
  • Embedded SDK
  • Salla OAuth 2.0
Storefront
Storefront
  • Twilight Engine
  • Twilight SDK
  • Web Components
  • Ecommerce Events
  • Component Bundle
  • Checkout APIs
  • Change Log
App Functions
Partner APIs
Partner APIs
  • App API
  • Shipments & Fulfillment APIs
  • Salla AWB
  • Recurring Payments API
  • Billing System Salla partners
  • Communication Apps
Dev Tools
Dev Tools
  • Partners Agent Kit
  • Salla Apps Playbook
  • Salla CLI
Merchant
Merchant
  • Merchant API
  • Embedded SDK
  • Salla OAuth 2.0
Storefront
Storefront
  • Twilight Engine
  • Twilight SDK
  • Web Components
  • Ecommerce Events
  • Component Bundle
  • Checkout APIs
  • Change Log
App Functions
Partner APIs
Partner APIs
  • App API
  • Shipments & Fulfillment APIs
  • Salla AWB
  • Recurring Payments API
  • Billing System Salla partners
  • Communication Apps
Dev Tools
Dev Tools
  • Partners Agent Kit
  • Salla Apps Playbook
  • Salla CLI
Salla - Opensource
Salla - Developers Community
  1. Communication Apps

Build Your App Function

In this article, you will configure your App Function with the Twilio credentials, understand the payload your function receives, and write the function that delivers WhatsApp messages through Twilio.
By the end of this article, you will have a working App Function that receives a communication event from Salla and sends a WhatsApp message to the customer.

Add a new App Function#

1
In the Partner Portal, open your app and scroll to App Functions.
2
2.
Click Add New Function.
image.png
The function builder consists of four main sections:
Function Name: A descriptive identifier
Action Selector: The specific event that triggers this function.
Code Editor: Where you write the custom handler logic.
Preview Panel: A testing environment using real demo store data.
image.png
3
Click Select Action and choose the channel you want to handle. The code editor will be auto-populated with boilerplate code which we will replace using the code in this article in the following sections.
image.png

Understand the payload#

Before writing your handler, it helps to know exactly what Salla sends to your function. Every communication event delivers the same shape inside context.payload.data.
FieldTypeDescription
notifiablestring[]One or more recipients — phone numbers for SMS/WhatsApp, email addresses for Email
typestringWhy this message is being sent (e.g. order.status.updated, auth.otp.verification)
contentstringThe ready-to-send message body — use this as-is or reformat it for your provider
entityobject | nullThe related store entity (order, shipment, product, etc.) — can be null, always check before accessing
metaobjectAdditional context such as customer_id or OTP code
Here is what a real communication.whatsapp.send payload looks like when an order status changes:
{
  "event": "communication.whatsapp.send",
  "data": {
    "notifiable": ["+966500000000"],
    "type": "order.status.updated",
    "content": "Your order #1234 status has been updated to: Shipped.",
    "entity": {
      "id": 1234,
      "type": "order"
    },
    "meta": {
      "customer_id": 987
    }
  }
}
Always guard entity and meta
Both fields can be null or missing depending on the event type. Accessing them without a null check is the most common cause of a 500 error. Use optional chaining (entity?.id) in your handler.

Write your handler#

In this section, there are two tabs:
Explain Code — walks through the handler section by section with inline annotations
Full Code — the complete handler ready to copy and paste
Follow whatever fits you
Code Explanation
Full Code
1
Set Your Twilio Credentials
These are the credentials from your Twilio Console. Replace the placeholder values with your actual Account SID, Auth Token, and phone number. In production, these should come using the App Settings feature instead of being hardcoded.
Hardcoded vs. App Settings
Hardcoding credentials is fine for local testing but should not go to production. Move these values to App Settings and read them via context.settings.twilio_account_sid etc. before publishing.
2
Validate the Recipient
Before sending anything, confirm the target customer phone number is actually in the notifiable array Salla sent. If it is not, log it and return a success early — this is not an error, just a message that was not meant for this customer.
3
Extract the Order ID
Pull the order ID from entity. This can be null depending on the event, so always guard it. If there is no order entity, log it and return early.
4
Build the Message
Compose the WhatsApp message text using the order ID and data.content. The data.content field is the ready-to-send body Salla prepared — use it as a fallback or as the primary message depending on your needs. Format the recipient number with the whatsapp: prefix that Twilio requires.
5
Call the Twilio API
Send the message using Twilio's Messages API. The request uses HTTP Basic Auth with your Account SID and Auth Token encoded in Base64. The full URL includes your Account SID.
6
Handle the Response
Parse Twilio's response. If the request succeeded, return the Twilio message SID as confirmation. If Twilio returned an error, surface it with the original status code so Salla can log it correctly.
7
Catch Unexpected Errors
Wrap the API call in a try/catch to handle network failures or unexpected exceptions. Return a 500 with the error message so it appears in the function logs.
8
Unhandled Event Fallback
If the event type does not match any of your handlers, return a success. This is intentional — an unrecognised event is not an error, it just means your app has nothing to do for it.

What you have now#

An App Function created and bound to communication.whatsapp.send
A handler that validates credentials, extracts the payload, calls Twilio, and returns a structured response
In the next article, you will test this function end-to-end by creating a real order on your demo store and confirming the WhatsApp message arrives on your phone.
Modified at 2026-05-12 12:34:17
Previous
App Channels Configuration
Next
Test & Go Live