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

The code uses our Ruby and Rails gateway class: AfricasTalkingGateway.rb [ Download]

# Be sure to import the helper gateway class
require './AfricasTalkingGateway'

# Specify your login credentials
username = 'MyUsername'
apikey   = 'MyApikey'

# Create a new instance of our awesome gateway class
gateway = AfricasTalkingGateway.new(username, apikey)

# Any gateway errors will be captured by our custom Exception class below,
# so wrap the call in a try-catch block
  # Our gateway will return 100 subscribers 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
  last_received_id = 0

  while true
    subscribers = gateway.fetchPremiumSubscriptions(last_received_id)
    subscribers.each {|x|
      puts "id = #{x.id}; phone number = #{x.phoneNumber}"
      last_received_id = x.id
    break if subscribers.length == 0

rescue AfricasTalkingGatewayException => ex

  puts 'Encountered an error: ' + ex.message


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 version="1.0"?>

{"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