SIGN UP

HTTP API Client Libraries

HTTP API Client Libraries

Voximplant developers use our HTTP API widely in their projects since the API is versatile and constantly improving by adding new methods. All modern languages let a developer make HTTP requests and handle HTTP responses, but now there is no need to create your own wrappers for our API, just use the API client libraries which ensure you're using the API in the best supported manner. The API libraries are aimed to speed up development and let developers focus on the high-level aspects of their work instead of taking care of the low-level details of requests/responses.

Should you choose to use any of the libraries, check the links below:

What's the point?

Instead of just talking about API libraries, let's look at the example below.

Let's imagine that we have a Node.js application which sends requests to the Voximplant HTTP API. The application may send SMS messages using our HTTP API directly:

const fs = require("fs");
const jwt = require("jsonwebtoken");
const axios = require("axios").default;
const FormData = require("form-data");
const form = new FormData();
form.append('cmd', 'SendSmsMessage');
form.append('source', '447443332211');
form.append('destination', '447443332212');
form.append('smsBody', 'Test message');
fs.readFile(process.env.VOXIMPLANT_CREDENTIALS, 'utf8', (err, data) => {
    if (err)
        throw err;
    key = JSON.parse(data);
    const nowTS = (+new Date()) / 1000 | 0;
    const tokenData = { iss: key.account_id, iat: nowTS, exp: nowTS + 64 };
    const token = jwt.sign(tokenData, key.private_key, { algorithm: 'RS256', header: { kid: key.key_id } });
    axios.post(`https://${this.host || 'api.voximplant.com'}/platform_api`, form, { headers: Object.assign({}, form.getHeaders(), { 'Authorization': 'Bearer ' + token }) })
            .then(response => {
                return response.data;
            }).catch(err=>console.log(err))
});

As you see, there is a lot of "micromanagement" due to the need for performing low-level actions.

The following code does the same: it sends an SMS to the specified number. But the goal is reached via the Node.js API library:

const VoximplantApiClient = require("@voximplant/apiclient-nodejs").default;
const client = new VoximplantApiClient();
client.onReady = function(){
  // Send the SMS message with text "Test message" from the phone number 447443332211 to the phone number 447443332212.
  client.SMS.sendSmsMessage({source: '447443332211',
            destination: '447443332212',
            smsBody: 'Test message'})
        .then(ev=>console.log(ev))
        .catch(err=>console.error(err));
};

As people say, "less is more". This is an illustration of how handy API libraries could be – in our example, we achieved the same result with a more readable and shorter piece of code, all due to the Node.js API library.

 

Happy coding! Be productive and stay tuned.

Tags:developershttp apiapi clientnodejsgolangpythonphp
B6A24216-9891-45D1-9D1D-E7359CEB8282 Created with sketchtool.

Comments(0)

Add your comment

Please complete this field.

Recommended

Sign up for a free Voximplant developer account or talk to our experts
SIGN UP