November 1st 2018

Work with OVH API using Postman

We are using /SMS API services in this example

Prerequisites, assuming that you have:

Introduction

OVH API

ovh-api

OVH API is a Web service allowing OVH customers to buy, manage, upgrade and configure OVH products without using the graphical customer interface (OVH manager).

Postman Pre-request scripts

Pre-request scripts are snippets of code associated with a collection request that are executed before the request is sent. This is perfect for use-cases like including the timestamp in the request headers or sending a random alphanumeric string in the URL parameters.

Doc: Postman Pre-request scripts

Let's go

Process

1) Import curl

Copy/paste content of request.curl into prompt window (File > Import > "Paste Raw text") and confirm

curl -X GET \
  https://api.ovh.com/1.0/sms \
-H 'Content-Type: application/json; charset=utf-8' \
-H 'X-Ovh-Application: xxxxxx' \
-H 'X-Ovh-Consumer: {{lf_ovh_api_app_consumer_key}}' \
-H 'X-Ovh-Signature: {{lf_ovh_api_app_consumer_signature}}' \
-H 'X-Ovh-Timestamp: {{lf_ovh_api_app_timestamp}}' \
-H 'cache-control: no-cache'

import curl request

2) Define variables

  • In Headers tab, fill X-Ovh-Application with your application key (e.g. AmJgcE99XcibYrV1).

    Postman headers tab

  • Define env. variable

    • lf_ovh_api_app_consumer_key with your consumer key
    • lf_ovh_api_app_secret with your app secret

Postman Manage Environments

(doc: Postman Variables)

3) Try to run

If you send the request as it is, the OVH API will return an error because the signature and the timestamp are not filled.

Response after press "Send" button:

{
  "errorCode": "QUERY_TIME_OUT",
  "httpCode": "400 Bad Request",
  "message": "Query out of time"
}

4) Pre-request Script

Copy/paste content of pre-request-script into "Pre-request Script" tab textarea.

pre-request script tab

pm.sendRequest({
    url: "https://api.ovh.com/1.0/auth/time",
    method: "GET",
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    },
    body: {}
 },
 function (err, res) {
     
    pm.expect(err).to.not.be.ok;
    pm.expect(res).to.have.property('code', 200);
    pm.expect(res).to.have.property('status', 'OK');
    
    var serverTimestamp = res.text();
    postman.setGlobalVariable("lf_ovh_api_app_server_timestamp", serverTimestamp);
    
    var time_delta = serverTimestamp - Math.round(new Date().getTime()/1000);
    var now = Math.round(new Date().getTime()/1000) + time_delta;
    var body = '';
    
    postman.setGlobalVariable("lf_ovh_api_app_timestamp", now);
    
    var toSign = environment.lf_ovh_api_app_secret + '+' + environment.lf_ovh_api_app_consumer_key + '+' + pm.request.method + '+' + pm.request.url.toString() + '+' + body + '+' + now;
    
    var signature = '$1$' + CryptoJS.SHA1(toSign);
    
    postman.setGlobalVariable("lf_ovh_api_app_consumer_signature", signature);
 }
);

5) Try to run

Normally you should then have the following response from OVH API πŸŽ‰πŸŽ‰

  [
    "sms-rnXXXXX-1"
  ]

😠the API returns you an error? check your credentials, check "Possibles issues" section below.

Possibles issues

Empty response []

No problem, the service works but you have to configure an SMS account in your OVH manager.

Invalid signature (400 Bad Request)

Check if your consumer_key and app_secret are valid.

This call has not been granted (403 Forbidden)

Make sure that you have generated the key with the permissions that are good for the service

Doc: Creating API Keys for your script for SMS services

Another issue ?

Do not hesitate to leave a comment with the error encountered.

Evolution

⚠️ the "Pre-request" script only supports the GET method for the moment. You are free to upgrade the script to support POST, PUT & DELETE methods.

Alternative

Use and test the OVH API directly from the browser with your OVH account: https://docs.ovh.com/fr/private-cloud/connexion-a-l-api-ovh/

ovh-api-login

ovh-api-service

Script sources

Check https://gist.github.com/romainnorberg/9847a6707a26a77039358d85cce3d7aa

Romain Norberg

Romain Norberg

Php developer, play with Symfony and PhalconPhp frameworks.
More on LinkedIn or Github