Upload media file

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 Java code snippet below shows how you can upload a media file to Africas Talking servers using our API.

The code uses our Java gateway class: AfricasTalkingGateway.java [ Download]

To help decode JSON responses, you will need to add one of the available JAVA libraries as a dependency. For this tutorial we chose to use json-20090211.jar which you can download from this repository.

Note On SSL Certificate Validation Issues

Some of our users have experienced SSL Certificate validation issues when connecting to our website due to our use of https. The error you see may look something like this:

Unable to find valid certification path to requested target

Please do not panic! Our certificate is perfectly valid (as evidenced by these verification tests), and the error you are seeing is quite common amongst Java clients connecting to secure websites. Here is how to fix the issue:

  • Download and compile InstallCert.java (you can find the code here).
  • On the CMD prompt(Windows) or Terminal(*nix), run: java InstallCert api.africastalking.com
    This will download a copy of our Certificate and create a file named jssecacerts in the current directory.
  • You can now add our Certificate to your trusted store by copying the created jssecacerts file to the Java security directory under $JAVA_HOME/jre/lib/security
// Make sure the downloaded jar file is in the classpath

import org.json.*;

public class TestUploadMediaFile
{
	public static void main(String[] args_)
	{
		// Specify your login credentials
		String username = "MyAfricasTalking_Username";
		String apiKey   = "MyAfricasTalking_APIKey";

		// Specify the url of the file to be uploaded
		String url = "http://onlineMediaUrl.com/file.wav";;

		// Create a new instance of our awesome gateway class
		AfricasTalkingGateway gateway  = new AfricasTalkingGateway(username, apiKey);

		/*************************************************************************************
			NOTE: If connecting to the sandbox:

			1. Use "sandbox" as the username
			2. Use the apiKey generated from your sandbox application
				https://account.africastalking.com/apps/sandbox/settings/key
			3. Add the "sandbox" flag to the constructor

			AfricasTalkingGateway gateway = new AfricasTalkingGateway(username, apiKey, "sandbox");
		**************************************************************************************/

		// Any gateway errors will be captured by our custom Exception class below,
		// so wrap the call in a try-catch block
		try {
			gateway.uploadMediaFile(url);
			System.out.println("File upload initiated. Time for song and dance!");
		} catch (Exception e) {
			System.out.println("Encountered an error" + e.getMessage());
		}
	}
}

You can upload a media/audio file to our servers. This media file will be played when called upon by one of our voice functionalities. ie.

y
Functionality Attribute Description
Play url This contains the audio file you want played during a call.
Call queueing holdMusic This contains the audio file you want played when the user has been queued waiting to be dequeued.
Dial holdMusic This contains the audio file you want played when a number has been dialed before it's picked.
Our REST resource for making calls is located at: https://voice.africastalking.com/mediaUpload. In order to upload a media file, you will need to pass in the following parameters within a POST request:
Variable Name Location Description
apikey Header Your API Key, which you can generate from your dashboard
username Body Your Africa's Talking account username
url Body The url of the file to upload. Don't forget to start with http://

You can check the HTTP Response Code to determine whether the request was successful. Any response code other than 201 (Created) indicates that the call was not initiated

Example using CURL client
curl -H "Accept: application/xml" -H "Apikey:myAPIKey" -d "username=myUsername&url=http://myOnlineMediaFile.mp3" https://voice.africastalking.com/mediaUpload --insecure