Delete Subscription

To unsubscribe a phone number you'll need to make a HTTP POST to the following endpoints:

Endpoints

Live: https://api.africastalking.com/version1/subscription/delete
Sandbox: https://api.africastalking.com/version1/subscription/delete

Request Parameters

In addition to the standard request headers, the body of the request should contain the following fields:

Parameter Description
username
String
Required
Your app username
shortCode
String
Required
This is a premium short code mapped to your account
keyword
String
Required
Value is a premium keyword under the above short code and mapped to your account
phoneNumber
String
Required
The phoneNumber to be unsubscribed

API Response

The body of the response will contain the following fields:

Parameter Description
status
String
Indicates whether the phone number was successfully unsubscribed or not. Possible values are:
  • Success
  • Failed
Description
String
Describes the status of this unsubscription.

Below is a sample delete subscription response for a successful request:

{
    "status": "Success",
    "description": "Succeeded"
}

Delete subscriptions sample code

The PHP code snippet below shows how to delete a number from a subscription service.

The code uses our PHP SDK

<?php
use AfricasTalking\SDK\AfricasTalking;

// Set your app credentials
$username = "MyAppUsername";
$apikey   = "MyAppAPIKey";

// Initialize the SDK
$AT       = new AfricasTalking($username, $apiKey);

// Get the sms service
$sms      = $AT->sms();

function deleteSubscription() {
    // Set your premium product shortCode and keyword
    $shortCode   = 'myPremiumShortCode';
    $keyword     = 'myPremiumKeyword';

    // Set the phone number you're unsubscribing
    $phoneNumber = "+254711XXXYYY";

    try {
        // Delete the subscription
        $result = $sms->deleteSubscription([
            'shortCode'   => $shortCode,
            'keyword'     => $keyword,
            'phoneNumber' => $phoneNumber
        ]);

        print_r($result);
    } catch(Exception $e) {
        echo "Error: ".$e.getMessage();
    }
}

deleteSubscription();

The Java code snippet below shows how to delete a number from a subscription service.

The code uses our Java SDK.

/* Import SDK classes */
import com.africastalking.Callback;
import com.africastalking.SmsService;
import com.africastalking.AfricasTalking;
import com.africastalking.sms.Subscription;
import com.africastalking.sms.SubscriptionResponse;

import java.util.List;
import java.io.IOException;

public class TestDeleteSubscription
{
    public static void main(String[] args)
    {
		/* Set your app credentials */
		String USERNAME = "sandbox";
		String API_KEY = "";

		/* Initialize SDK */
		AfricasTalking.initialize(USERNAME, API_KEY);

		/* Get the SMS */
        SmsService sms = AfricasTalking.getService(AfricasTalking.SERVICE_SMS);

        /* Set your premium product shortCode and keyword */
        String shortCode = "XXXXX";
        String keyword = "ABC";

        /* Set the phone number you're unsubscribing */
        String phoneNumber = "+254711XXXYYY";
        
        try {
			
			/* Delete the subscription */
			SubscriptionResponse response = sms.deleteSubscription(
                shortCode, keyword, phoneNumber);
            System.out.println(response.toString());

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
   	}
}

The Python code snippet below shows how to remove a number from a subscription service using our API.

The code uses our Python SDK.

# works with both python 2 and 3
from __future__ import print_function

import africastalking

class SMS:
    def __init__(self):
		# Set your app credentials
        self.username = "YOUR_USERNAME"
        self.api_key = "YOUR_API_KEY"
		# Initialize the SDK
        africastalking.initialize(self.username, self.api_key)
		# Get the sms service
        self.airtime = africastalking.Sms

    def delete_subscription(self):
        # Set the phone number you're unsubscribing
        phone_number = '+254718769881'
        # Set your premium product shortCode and keyword
        short_code = "MyAppShortCode"
        keyword   = "MyAppKeyword"
        try:
            responses = self.sms.delete_subscription(short_code, keyword, phone_number)
            print (responses)
        except Exception as e:
            print ("Encountered an error while deleting a subscription:%s" %str(e))

if __name__ == '__main__':
    AIRTIME().delete_subscription()

The Ruby code snippet below shows how to remove a number from a subscription service using our API.

The code uses our Ruby SDK.

require 'AfricasTalking'

# Set your app credentials
username = "MyAppUsername"
apikey   = "MyAppAPIKey"

# Initialize the SDK
AT = AfricasTalking::Initialize.new username, apikey

# Get the SMS service
sms = AT.sms
	
# Set your premium product shortCode and keyword
shortCode = "MyAppShortCode"
keyword   = "MyAppKeyword"

# Set the phone number you're unsubscribing
phoneNumber = "+254711XXXYYY"
options = {
    'shortCode' => shortCode,
    'keyword' => keyword,
    'phoneNumber' => phoneNumber,
}
begin
  # Delete the subscription
  result = sms.deleteSubcription options
  puts result.to_yaml
rescue AfricasTalking::AfricasTalkingException => ex
  puts 'Encountered an error: ' + ex.message
end

The Javascript code snippet below shows how to remove a number from a subscription service using our API.

The code uses our Node SDK.

// Set your app credentials
const credentials = {
    apiKey: 'MyAppAPIkey',
    username: 'MyAppUsername'
}

// Initialize the SDK
const AfricasTalking = require('africastalking')(credentials);

// Get the SMS service
const sms = AfricasTalking.SMS;

function unsubscribePhoneNumber() {

    const options = {
        // Set your premium product shortCode and keyword
        shortCode: 'XXYYZZ',
        keyword: 'premiumKeyword',
        // Set the phone number you're unsubscribing
        phoneNumber: '+254711XXXYYY',
    }

    // Delete the subscription
    sms.deleteSubscription(options)
        .then(response => {
            console.log(response);
        })
        .catch( error => {
            console.log(error);
        })
}

unsubscribePhoneNumber();