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 Node.js code snippet below shows how to add a number to a subscription service using our API.

//import the required libraries

var querystring = require('querystring');
var https       = require('https');

//Set your AfricasTalking API login credentials
var username = "MyAppUsername";
var apiKey   = "MyAppAPIkey";

//create createSubscription function
function createSubscription(phoneNumber_, shortCode_, keyword_) {
	//Build post string from object
	var post_data = querystring.stringify({
	      'username'    : username,
	      'phoneNumber' : phoneNumber_,
	      'shortCode'   : shortCode_,
	      'keyword'     : keyword_	
	//Create request parameters
	var request_parameters = {
       host : '',
       port : 443,
       path : '/version1/subscription/create',
       method : 'POST',
       rejectUnauthorized : false,
       requestCert        : true,
       agent              : false,
       headers : {
            'Content-Type'    : 'application/x-www-form-urlencoded',
            'Content-Length'  : post_data.length,
            'apikey'          : apiKey,
            'Accept'          : 'application/json'
 var post_request = https.request(request_parameters, function (response) {
 	  response.on('data', function (data_chunk) {
 	  	try {
 	  	   if (response.statusCode != 201)
 	  	    throw data_chunk;
 	  	   var jsObject = JSON.parse(response);
 	  	   var logStr   = "Status : "     + jsObject.status;
 	  	   logStr       = "; Description" + jsObject.description;
 	  	 catch(error) {
 	  	  console.log("Error:" + error);

var phoneNumber = "+254711XXXYYY";
var shortCode   = "12345";
var keyword     = "myKeyword";

//call the function
createSubscription(phoneNumber, shortCode, keyword);

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