Management API Client Libraries

2019-06-10 14:07:17
93438
0
Blog picture

Voximplant developers use our Management API widely in their projects since the API is versatile and constantly improving to keep pace with Voximplant platform improvements. All modern languages let a developer make HTTP requests and handle HTTP responses. One could build a custom wrapper for Voximplant using these methods and our HTTP Management API, but there is no need to do this thanks to our Management API Client libraries. The API libraries are aimed to ensure best implementation practices for interfacing with Voximplant, 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 that sends requests to the Voximplant Management 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.

Sign Up for a free Voximplant developer account or talk to our experts

Add your comment

Name*
Email*
Message

Your comment has been added and will be published after moderation.

Recommended posts