Fetch messages from API

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 Node.js code snippet below shows how to send airtime using our API.

// We need this to build our post string
var querystring = require('querystring');
var https       = require('https');

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

function fetchMessages(lastReceivedId_) {
    // Build the post string from an object
    var options = {
		host: 'api.africastalking.com',
		port: '443',
		path: '/version1/messaging?username=' + username + '&lastReceivedId=' + lastReceivedId_,
		method: 'GET',
		
		rejectUnauthorized : false,
		requestCert        : true,
		agent              : false,
  
		headers: {
		    'Accept': 'application/json',
		    'apikey': apikey
		}
    };
    
    var request = https.request(options, function(res) {
	    res.setEncoding('utf8');
	    res.on('data', function (chunk) {
		    var jsObject = JSON.parse(chunk);
		    var messages = jsObject.SMSMessageData.Messages;
		    if ( messages.length > 0 ) {
			for (var i = 0; i < messages.length; ++i ) {
			    var logStr  = 'from=' + messages[i].from;
			    logStr     += ';to='   + messages[i].to;
			    logStr     += ';message=' + messages[i].text;
			    logStr     += ';linkId=' + messages[i].linkId;
			    logStr     += ';date=' + messages[i].date;
			    logStr     += ';id=' + messages[i].id;
			    lastReceivedId_ = messages[i].id;
			    console.log(logStr);
			}
			
			// Recursively fetch messages
			fetchMessages(lastReceivedId_);
			
		    } 
		});
	});
    
    request.end();
    console.log('LastReceivedId: ' + lastReceivedId_);
}

var lastReceivedId = 0;
fetchMessages(lastReceivedId);
   
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 username Yes
lastReceivedId URL This is the id of the message that you last processed. If this is your first call, pass in 0 Yes
XML
<?xml version="1.0"?>
  <AfricastalkingResponse>
    <SMSMessageData>
      <Messages>
        <from>+254711XXXYYY</from>
        <to>5252</to>
        <text>A test message</text>
        <linkId>messageLinkId</linkId>
        <date>2012-07-15 15:40:06</date>
        <id>1</id>
      </Message>
      .
      .
      .
      .
      <Message>
        <from>+254733YYYZZZ</from>
        <to>5252</to>
        <text>Another test message</text>
        <linkId>messageLinkId</linkId>
        <date>2012-07-15 15:47:06</date>
        <id>10</id>
      </Message>
    </Messages>
  </SMSMessageData>
</AfricastalkingResponse>
JSON

{"SMSMessageData":
  {"Messages":
    [
      {
        "from":"+254711XXXYYY",
        "to":"5252",
        "text":"A test message",
        "linkId": "messageLinkId",
        "date":"2012-07-15 15:40:06",
        "id":"1"
      },
      .
      .
      .
      .
      {
        "from":"+254733YYYZZZ",
        "to":"5252",
        "text":"Another test message",
        "linkId": "messageLinkId",
        "date":"2012-07-15 15:47:06",
        "id":"10"
      }
    ]
  }
}
Example using CURL client
curl -v -H "Accept: application/xml" -H "Apikey:fb752d3417021812f0961y6c9464832dd1adb1e555c73f1e7c32bcc006488674" https://api.africastalking.com/version1/messaging?username=myUsername&lastReceivedId=0 --insecure