Fetch premium subscriptions

You need your AfricasTalking username and APIKey for any request to our API. If you don't have an APIKey, please click here to get one.

The Java code snippet below shows how to fetch subscribed numbers from a subscription service using our API.

// Import the required libraries
var https = require('https');

// Your login credentials
var username  = 'MyUsername';
var apikey    = 'MyApiKey';

var shortCode = "12345";
var keyword   = "myAfricasTalkingKeyword";

function fetchPremiumSubscriptions(lastReceivedId_) {
	//create request parameters
	var request_parameters = {
                host    : 'api.africastalking.com',
                port    : 443,
                path    : '/version1/subscription?username=' + username + '&shortCode=' + shortCode_ + '&keyword=' + keyword_ + '&lastReceivedId=' + lastReceivedId_,
                
                method  : 'GET',
                
                rejectUnauthorized : false,
                requestCert        : true,
                agent              : false,
                
                headers : {
                	      'apikey' : apikey,
                	      'Accept' : 'application/json'
                	     }
           }
											
		var request = https.request(request_parameters, function (response) {
      	response.setEncoding('utf8');
      	response.on('data', function (data_chunk) {
      		try {
      			if (response.statusCode != 200)
      				throw data_chunk;
      				
      			var jsObject = JSON.parse(data_chunk);
      			var subscription_data = jsObject.SubscriptionData.Subscriptions;
      			if (subscription.length > 0) {
        			for (subscription in subscription_data) {
        			   var logStr   = 'Number : ' + subscription.phoneNumber;
        			   logStr      += ';id : ' + subscription.id;
        			   lastReceivedId_ = subscription.id;
        			   console.log(logStr);
        		 }
        		 
        		 //fetch subscriptions recursively
        		 fetchPremiumSubscriptions(lastReceivedId_);
        		}
      		}
      		catch (error) {
      			console.log('Error: ' + error);
      		}
      	});
      });
	request.end();
	
}

var lastReceivedId = 0;

//Call get user function;
fetchPremiumSubscriptions(lastReceivedId_);

To receive messages you will need to make a HTTP GET request to the following url:
GET URL: http://api.africastalking.com/version1/subscription
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 URL This is your Africa'sTalking username Yes
shortCode URL This is a premium short code mapped to your account Yes
keyword URL Value is a premium keyword under the above short code and mapped to your account Yes
lastReceivedId URL ID of the subscription you believe to be your last. Set it to 0 to for the first time. Yes
Upon successful processing, the server shall respond in the following format:
XML
<?xml version="1.0"?>
  <AfricastalkingResponse>
    <Subscriptions>
      <Subscription>
        <id>100</id>
        <phoneNumber>+254711XXXYYY</phoneNumber>
        <date>Timestamp</date>
      </Subscription>
      .
      .
      .
      .
      <Subscription>
        <id>200</id>
        <phoneNumber>+254733YYYZZZ</phoneNumber>
        <date>Timestamp</date>
      </Subscription>
    </Subscriptions>
</AfricastalkingResponse>
JSON

{"Subscriptions" :
  [
    {
      "id" : 100,
      "phoneNumber" : "+254711XXXYYY",
      "date" : "Timestamp"
    }
    .
    .
    .
    .
    {
      "id" : 200,
      "phoneNumber" : "+254733YYYZZZ",
      "date" : "Timestamp"
    }
  ]
}
Example using CURL client
curl -H "Accept: application/xml" -H "Apikey:fb752d3417021812f0961y6c9464832dd1adb1e555c73f1e7c32bcc006488674" "https://api.africastalking.com/version1/subscription?username=myUsername&shortCode=12345&keyword=myPremiumKeyword" --insecure