Create subscription

You need your AfricasTalking username and APIKey for any request to our API. If you don't have an APIKey, you can generate one from your applications settings page.

The PHP code snippet below shows how to add a number to a subscription service using our API.

The code uses our PHP gateway class: AfricasTalkingGateway.php [ Download]

Also, please ensure that you have the php_curl module enabled. To enable it uncomment the line: ;extension=php_curl.dll in php.ini and restart apache


require_once "AfricasTalkingGateway.php";

//Specify your credentials
$username = "MyAppUsername";
$apiKey   = "MyAppAPIKey";

// Specify the number that you want to subscribe
// Please ensure you include the country code (+254 for Kenya in this case)
$phoneNumber   = "+254711XXXYYY";

//Specify your Africa's Talking short code and keyword
$shortCode = "ABCDE";
$keyword   = "myKeyword";


Get a checkoutToken by making a POST call to the endpoint bellow

You only need a phoneNumber as a POST parameter.
This should be the phoneNumber you're creating the subscription for.

The above request should ideally be made from a client - e.g browser - and finally
sent back to your server to initiate the createSubscription request.

Successfull response...
	"descriptions": "Success",
	"token": "CkTkn_5fccf6fa-172f-4566-9d42-fd441d236b44"

Failure response...
	"descriptions": "Too many requests in the current window",
	"token": "None"


$checkoutToken = $postResponse->token;

//Create an instance of our awesome gateway class and pass your credentials
$gateway = new AfricasTalkingGateway($username, $apiKey);

  NOTE: If connecting to the sandbox:

  1. Use "sandbox" as the username
  2. Use the apiKey generated from your sandbox application
  3. Add the "sandbox" flag to the constructor

  $gateway  = new AfricasTalkingGateway($username, $apiKey, "sandbox");

// Thats it, hit send and we'll take care of the rest. Any errors will
// be captured in the Exception class as shown below

try {
	$result = $gateway->createSubscription(

	//Only status Success signifies the subscription was successfully
	echo $result->status;
	echo $result->description;
} catch(AfricasTalkingGatewayException $e){
	echo $e->getMessage();
To receive messages you will need to make a HTTP POST request to the following url:
The server shall expect:
Parameter Location Description Required
API key Header API key generated from your account settings Yes
Accept Header This is the format you would like your data formatted. It may be application/xml or application/json. The default is application/xml. The gateway classes provided uses appliaction/json. No
username Body This is your Africa'sTalking username Yes
shortCode Body This is a premium short code mapped to your account Yes
keyword Body Value is a premium keyword under the above short code and mapped to your account Yes
phoneNumber Body The phoneNumber to be subscribed Yes
Upon successful processing, the server shall respond in the following format:
<?xml version="1.0"?>
    <description>Waiting for user input</description>

  "status": "Success",
  "description": "Waiting for user input"
Example using CURL client
curl -H "Accept: application/xml" -H "Apikey:fb752d3417021812f0961y6c9464832dd1adb1e555c73f1e7c32bcc006488674" -d "username=myUsername&shortCode=12345&keyword=myPremiumKeyword&phoneNumber=+254711XXXYYY" --insecure