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

//Import the required libraries
querystring = require('querystring');
https       = require('https');

var username = "myAfricasTalkingUsername";
var apiKey   = "myAfricasTalkingAPIkey";

//create the call function
function uploadMediaFile(locationURL_) {
	//Build post string from object
	
 var post_data = querystring.stringify({
       'username'  : username,
       'url'       : locationURL_
 });
 
 //create the request parameter object
 
 var request_parameters = {
	     host    : 'voice.africastalking.com',
	     port    : 443,
	     path    : '/mediaUpload',
	     
	     method  : 'POST',
	     
	     rejectUnauthorized : false,
	     requestCert        : true,
	     agent              : false,
	     
	     headers : { 
         'Content-Type'   : 'application/x-www-form-urlencoded',
         'Content-Length' : post_data.length,
         'apikey'         : apiKey,
         'Accept'         : 'application/json'
      }
	  }
                         
  request = https.request(request_parameters, function (response) {
  	response.setEncoding('utf8');
  	response.on('data', function (data_chunk) {
  		try {
  			var jsObject = JSON.parse(data_chunk);
  			if(jsObject.errorMessage != "None")
  			 throw jsObject.errorMessage;
  			 
  			 console.log('File uploaded. Time for song and dance');
  		}
  		catch (error) {
  			console.log("Error: " + error);
  			}
  		});
  	});
  
  request.write(post_data);
  request.end();
}

var url = "http://onlineMediaUrl.com/file.wav";

//call the function
uploadMediaFile(url);

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