Sending SMS Messages with Ballerina Using Twilio

Dilan Perera
4 min readJan 16, 2024

--

Ballerina, known for its simplicity and power in building cloud-native integrations, combines with Twilio platform’s versatile communication APIs to help you send SMS, make voice calls, send WhatsApp messages, and many more.

Prerequisites: Set up the Twilio Account

If you do not have access to the Twilio API, please complete the following steps:

Step 1: Create a Twilio account

Creating a Twilio account can be done by visiting Twilio and clicking the “Start for Free” button.

Step 2: Obtain a Twilio phone number

All trial projects can provision a free trial phone number for testing. Here’s how to get started:

  1. Access the “Buy a Number” page in the Console.

2. Enter the criteria for the phone number you need and then click Search.

3. Click Buy to purchase a phone number for your current project or sub-account.

Step 3: Obtain a Twilio Account SID with Auth Token

Twilio uses two credentials to determine which account an API request is coming from: The Account SID, which acts as a username, and the Auth Token which acts as a password. You can find your account SID and auth token in your Twilio console.

Your account’s Auth Token is hidden by default. Click show to display the token, and hide to conceal it again.

Getting Started

First you need Ballerina to write the program.If you don’t have Ballerina installed on your computer, you can follow these instructions to install Ballerina on your computer.

Then you can create a new Ballerina package using the command below:

bal new twilio-sample

To provide the configurations to required , create a new file named Config.toml in the created package directory and add the account Sid and auth token obtained from Twilio like this:

The file structure within the package will look like below:

└── twilio-sample
├── Ballerina.toml
├── Config.toml
└── main.bal

Once you have everything set up, let’s dive into the Ballerina code to send SMS using Twilio by adding the following code to main.bal file:

import ballerina/io;
import ballerinax/twilio;

configurable string accountSid = ?;
configurable string authToken = ?;

// Twilio configurations
twilio:ConnectionConfig twilioConfig = {
auth: {
username: accountSid,
password: authToken
}
};

public function main() returns error? {
// Create the Twilio client
twilio:Client twilio = check new (twilioConfig);

// Create the message request
twilio:CreateMessageRequest messageRequest = {
To: "+94712345678", // Phone number to send the message
From: "+14843140730", // Twilio phone number
Body: "Hello from Ballerina"
};
// Send the message
twilio:Message response = check twilio->createMessage(messageRequest);

// Print the response
io:print(response);
}

Change the “To: “+94712345678” number to the desired number for sending the SMS (if you are using a trial account, make sure to verify that number through Twilio). Next, update the “From: “+14843140730” to your Twilio phone number, which you obtained in Prerequisites: Step 2.

If you are a person who is familiar with graphical views rather than code, you can use the sequence diagram generated by Ballerina to observe the interactions and understand the underlying code structure (You should have the Ballerina-VS Code plugin). To access the Sequence Diagram View, click the “Visualize” CodeLens that appears in VS Code.

Sequence diagram view will look like below:

Finally, execute the program using the “bal run” command, and you will receive an output like below, with a message sent to the “to” phone number you provided in the code.

Congratulations! By following these steps, you have successfully integrated Ballerina with Twilio to send SMS messages. You can now explore further customization and integration possibilities to enhance your communication capabilities by visiting this link, which contains all the Twilio capabilities that can be accessed through Ballerina.

Remember to keep your Twilio credentials secure and consider error handling and logging when implementing SMS messaging in production applications. Happy coding!

This article was written using Ballerina Swan Lake Update 8.0 (2201.8.0)

--

--