Reach Messaging API - Quickstart - PHP
Sending SMS with the Reach Messaging API from TalkyLabs is made possible with just a few lines of PHP code.
In what follows, we provide a step-by-step guide to facilitate your debut. In this quickstart, you will learn how to:
Sign up for the Reach platform
Create an applet
Retrieve your API credentials
Verify your phone number
Send a SMS
Sign-up / Sign-in
You can skip this section if you already have an account on the Reach platform.
You can sign up for an account on the Reach platform here. You will be asked to verify your email address to activate your account.
Create an applet
You can skip this section if you already have an applet and want to use it here.
An applet can be viewed as a subaccount or a development environment within the Reach platform. It allows you to regroup your operations in the Reach platform based on your customer segments, e.g., by country. You can have more than one applet associated with your account.
Creating an applet is just as simple as clicking a button and specifying the applet name as well as the operating country, that is the country where most of the customers targeted by the operations that will be performed under this applet reside.
Retrieve your API credentials
After accessing your applet, you can retrieve your API credentials by visiting Settings -> API from the left side menu.
As you will notice, you will gain access to your API User
as well as your Test and Live API Key
. Recall that the Test API key is for the sandbox mode where you can emulate sending messages while the Live API key is for sending messages for real.
Finally, note that it is possible to re-generate your API keys as you go.
Verify your phone number
You can skip this section if you already have a verified number.
With an applet, you can emulate sending messages in a sandbox mode by using whatever valid phone numbers you want. However, for sending real messages, you can only use phone numbers that you own.
To demonstrate the ownership of a phone number, you need to verify it. Within an applet, visit the section Phone numbers -> Verified numbers from the left side menu to add your number and start a verification process.
Once the number is verified, it will be valid for a period of time. For now, that period is three months and this can changed without prior notice. You will have to re-verify the number to continue use it after that period of time.
Install the client library
If you already have the PHP client library installed, you can skip this step and get straight to sending your first message.
You have to make sure you have PHP installed on your computer. This could be checked by running the following command:
php --version
If it is installed, you would see something like:
PHP 8.2.10 (cli) (built: Sep 2 2023 06:58:59) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.10, Copyright (c) Zend Technologies
with Zend OPcache v8.2.10, Copyright (c), by Zend Technologies
with Xdebug v3.2.1, Copyright (c) 2002-2023, by Derick Rethans
To install our PHP client library, it is highly recommend to use composer especially when doing web development. This is done by ruuning the following command in the terminal:
composer require talkylabs/reach-sdk
If you'd prefer a manual installation, simply download the source code, and unzip it in the same directory as your PHP code. Then, in your code, you can require the autoload file bundled with the PHP SDK.
Send a message
Now that the PHP client library is installed, we can send an SMS using a valid verified number registered in the applet. Create a file name send_message.php
and paste in it the code sample below.
<?php
require __DIR__ . '/vendor/autoload.php';
use Reach\Rest\Client;
$apiUser = getenv('REACH_TALKYLABS_API_USER');
$apiKey = getenv('REACH_TALKYLABS_API_KEY');
$client = new Client($apiUser, $apiKey);
$message = $client->messaging->messagingItems->send(
"+237671234567", // dest
"+237691234567", // src
"Hello World! This is a sms message."
);
print($message->messageId);
Replace the src
phone number with your verified number and the dest
phone number with the number you want to send the message to. The phone numbers must be in the E.164 format.
Also, use your API credentials to replace the placeholder values for $apiUser
and $apiKey
. Remember to use your Live API key.
For getting started, it is okay to hardcode your credentials in the file. However, it is highly recommend to use environment variables to store sensitive data instead.
Save the file and run the following command in your terminal:
php send_message.php
That's it! In a few moments, your message will be delivered to the destination phone number.