Create subscription

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 C# code snippet below shows how to subscribe a phone number to a subscription service using our API.

The code uses our C# gateway class: AfricasTalkingGateway.cs [ Download]

To help decode JSON responses, you will need to add a reference to System.Web.Extensions and System.Microsoft.CSharp

using System;

class MainClass
{
	static public void Main (){
		
		// Specify your login credentials
		string username = "MyAfricasTalkingUsername";
		string apiKey   = "MyAfricasTalkingAPIKey";
		
		// Specify the number that you want to subscribe
		// Please ensure you include the country code (+254 for Kenya in this case)
		string phoneNumber   = "+254711XXXYYY";
		
		//Specify your Africa's Talking premium short code and keyword
		string shortCode = "myAfricasTalkingShortCode";
		string keyword   = "myAfricasTalkingKeyword"; 
		
		//Create an instance of our gateway class and pass your login credentials
		AfricasTalkingGateway gateway = new AfricasTalkingGateway(username, apiKey);
		
		// Any gateway error will be captured by our custom Exception class as shown below, 
		// so wrap the call in a try-catch block          
		try {
			
			dynamic result = gateway.createSubscription(phoneNumber, shortCode, keyword);
			Console.WriteLine("Status: " + result["status"]);
			Console.WriteLine("Description: " + result["description"]);
			
		} catch (AfricasTalkingGatewayException e) {

			Console.WriteLine ("Encountered an error: " + e.Message);		

		}
	}
}

To receive messages you will need to make a HTTP POST request to the following url:
POST URL: http://api.africastalking.com/version1/subscription/create
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
<?xml version="1.0"?>
  <AfricastalkingResponse>
    <status>Success</status>
    <description>Waiting for user input</description>
</AfricastalkingResponse>
JSON

{
  "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" https://api.africastalking.com/version1/subscription/create --insecure