Fetch messages from API

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 VB.NET code snippet below shows how to airtime using our API.

The code uses VB gateway class: AfricasTalkingGateway.vb [ Download]

The code encodes and decodes data using Javascript serializer so you will need to add System.Web.Extensions to your referenced libraries

Imports System.IO
Imports System.Collections
Imports System.Collections.Generic

Public Class Application
	Public Shared Sub Main()
			' Specify your login credentials
			Dim username As String = "MyAppUsername"
			Dim apikey As String   = "myAPIKey"
			' Create a new instance of our awesome gateway class
			Dim gateway As New AfricasTalkingGateway(username, apikey)
			' Our gateway will return 100 messages at a time back to you, starting with
			' what you currently believe is the lastReceivedId. Specify 0 for the first
			' time you access the gateway, and the ID of the last message we sent you
			' on subsequent results
			Dim lastReceivedId As Integer = 0;
			' Any gateway errors will be captured by our custom Exception class below,
			' so wrap the call in a try-catch block
			 Dim messages As Object() = gateway.fetchMessages(lastReceivedId)
			 While messages.Length > 0
			 	Dim result As Object
			 	for each result in messages
			 		System.Console.WriteLine("from: " + result("from"))
			 		System.Console.WriteLine("to: " + result("to"))
			 		System.Console.WriteLine("message: " + result("text"))
			 		System.Console.WriteLine("date: " + result("date"))
			 		System.Console.WriteLine("linkId: " + result("linkId"))
			 		lastReceivedId = result("id")
			 	messages = gateway.fetchMessages(lastReceivedId)
			 End While
		catch ex As AfricasTalkingGatewayException
		End Try 
	End Sub
End Class

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 version="1.0"?>
        <text>A test message</text>
        <date>2012-07-15 15:40:06</date>
        <text>Another test message</text>
        <date>2012-07-15 15:47:06</date>

        "text":"A test message",
        "linkId": "messageLinkId",
        "date":"2012-07-15 15:40:06",
        "text":"Another test message",
        "linkId": "messageLinkId",
        "date":"2012-07-15 15:47:06",
Example using CURL client
curl -v -H "Accept: application/xml" -H "Apikey:fb752d3417021812f0961y6c9464832dd1adb1e555c73f1e7c32bcc006488674" https://api.africastalking.com/version1/messaging?username=myUsername&lastReceivedId=0 --insecure