Integrating SMS services to your Express app with Africa’s Talking APIs.

Here’s a tutorial on how to use Africa’s Talking for SMSs.

Onejohi

--

First thing’s first, what is Africa’s Talking. AT, is your last telco integration in Africa. You can use a lot of their services like sending Bulk SMS, use USSD codes, make voice calls, buy airtime and make transactions/payments. In this particular article, we’ll focus on getting started and SMS.

To get started with AT APIs, head over to their website below and create an account. It’s free, plus you get to test your APIs for free using a default sandbox application.

Integrating Africa’s Talking SMS service means you can reach around 300 million users across the African continent.

When you’re done creating an account, open up your free sandbox application to get familiar with the interface. You should have something similar to the image below.

a screenshot of your sandbox dashboard.

Once you have this set-up, we can now create our express application and integrate AT APIs.

> md my_app
> cd my_app
/my_app> npm install express body-parser africastalking --save

After creating a new folder for our application, initialize a new node application (Which I’ve skipped since I assume you’re familiar with NodeJS basics) then install three important packages that we’ll use. express creates and configures our server, body-parser will parse the request body for information and africastalking will configure AT into our app.

Getting our Demo ready.

To make a good demonstration, let’s create a somewhat realistic example. Let’s assume you have a list of 20 users stashed away in a database and each user has a verified, valid phone number. To make it extremely simple, we’ll not focus on how this data is retrieved. These 20 users are your clients, and you would like to notify them of a new offer via SMS all at once (often referred to as spamming 😆). For 20 phone numbers, it would be easy to copy and paste the same SMS 20 times, but what if your client’s number grows to 20,000? You’d need a solution, one that Africa’s Talking APIs cover.

To use the APIs, you need an API key. In your dashboard, head over to the Settings option and select API Key from the dropdown. Use your password to generate a new key, copy this as you will use it in your code. Here’s how your express application should look like, the comments should be very helpful.

It’s beautifully this easy.

A few things to note is the array of phone numbers should be in string format and the phone numbers should have a country code. The service currently supports phone numbers from Kenya, Uganda, Malawi, Nigeria, Rwanda, and Tanzania.

To include a shortcode or sender ID in your SMS, simply add a from property with the shortcode/ID in your options.

const options = {
to: ['+254727321766','+254786606075'],
message: "I've to a new offer for you G, hit me up",
from: 'XXYYZZ'
}

If you’re sending to a really large number of users, you might want to enqueue the messages. Simply add an enque property to options and set it to true.

Remember: You have to launch your simulator with your simulated phone numbers to successfully send your messages.

The return data should look something like this when I made a request using PostMan client.

response object from my API

A message will appear in our APIs and this is how it’ll open as.

Note that the shortcode here is AFRICASTKNG, you can set your own custom shortcode, however.

Going Premium.

I haven’t applied for a premium service yet, but the pricing on Africa’s Talking premium SMS is super affordable. Since I haven’t developed anything in premium, I’ll just document it as is in the docs. We’ll make a few changes to our application, from line 27– 38, replace the code with these lines of code.

To send premium rated on-demand messages, simply add a linkId property to options. Set the link id as the message sent to a subscriber to your on-demand service.

Getting to Production.

When you feel ready for getting into production, use teams to create and manage your production ready app. Your code should not change at all, only the API key. To make this change easy, change it into an environment variable where you can access it via process.env.AT_APIKEY instead.

You get a bonus KES 10.00 to start with, and it’s very easy to set up payments from the Billing option. You can set up billing using Mpesa (Very popular in Kenya), Airtel Money, PayPal (If you live outside EA) or directly using Wire Transfer or your credit card.

Conclusion.

Africa’s Talking has given us developers a neat platform where we can integrate SMS, calls, payments and much more. With AT APIs, their npm package greatly simplifies interaction with their APIs and the functionality is simple to understand, get started and keep using. The package is detailed in their npm repository homepage here.

More articles on more of Africa’s Talking integration are coming to you, just remember to hit the follow button 😜. Share to your social circles if you feel this article will be of help to someone else. Thanks! 😄

--

--

Onejohi

Creative 🚀 | Gamer 🎮 | Web & App developer 💻📱 | Graphics Designer 📏📝 | Entrepreneur 💶 | Cool AF 😎🤓 https://onejohi.com | WhatsApp +254727321766