Node + OVH

Node.js + OVH API

The easiest way to use the OVH REST API in your Node.js applications.

Getting started

1

1. Install node-ovh using NPM

The Node.js module "ovh" is unofficial and consequently not maintained by OVH.

Please report any issues on Github.

$ npm install ovh
2

2. Create an OVH application

You need to create a new application on the OVH website in order to have an application key (AK) and an application secret (AS).

3

3. Authorize your application to use an OVH account

To allow your application to use an OVH account, you need a consumer key (CK).

Here is a sample code you can use to allow your application to access to a complete account.

Optionally, you can specify the API host (eu.api.ovh.com or ca.api.ovh.com). The default one is the european one.

var ovh = require('ovh')({
  appKey: 'YOUR_APP_KEY',
  appSecret: 'YOUR_APP_SECRET',
  host: 'eu.api.ovh.com'
});

ovh.request('POST', '/auth/credential', {
  'accessRules': [
    { 'method': 'GET', 'path': '/*'},
    { 'method': 'POST', 'path': '/*'},
    { 'method': 'PUT', 'path': '/*'},
    { 'method': 'DELETE', 'path': '/*'}
  ]
}, function (error, credential) {
  console.log(error || credential);
});
$ node credentials.js
{ validationUrl: 'https://api.ovh.com/auth/?credentialToken=XXXXXXX',
  consumerKey: 'CK',
  state: 'pendingValidation' }

This consumer key can be scoped with a specific authorization. For example if your application will only send SMS:

ovh.request('POST', '/auth/credential', {
  'accessRules': [
    { 'method': 'POST', 'path': '/sms/*/jobs'},
  ]
}, function (error, credential) {
  console.log(error || credential);
});

Once the consumer key will be authorized on the OVH website, you'll be able to play with the API calls allowed by the key.

4

4. Let's play!

You should now be able to play with the API. Look at the examples below.

Examples

Node-ovh can be used in two ways, with or without Harmony Proxies. Each example below is written in the two ways.

All the examples have a node-ovh module instantiated as below:

var ovh = require('ovh')({
  appKey: APP_KEY,
  appSecret: APP_SECRET,
  consumerKey: APP_CONSUMER_KEY
});

1. Fetch the user information

Without harmony proxies:

ovh.request('GET', '/me', function (err, me) {
  console.log(err || me);
});
$ node me.js
{ firstname: 'Foo',
  country: 'FR',
  language: 'fr_FR',
  organisation: '',
  area: '',
  name: 'Bar',
  phone: '+33.XXXXXXXXX',
  email: 'user@domain.tld',
  city: 'Foo',
  zip: '42000',
  fax: '',
  nichandle: 'aaXXXXXX-ovh',
  address: '42 rue bar',
  legalform: 'individual' }

With harmony proxies:

ovh.me.$get(function (err, me) {
  console.log(err || me);
});
$ node --harmony-proxies me.js
{ firstname: 'Foo',
  country: 'FR',
  language: 'fr_FR',
  organisation: '',
  area: '',
  name: 'Bar',
  phone: '+33.XXXXXXXXX',
  email: 'user@domain.tld',
  city: 'Foo',
  zip: '42000',
  fax: '',
  nichandle: 'aaXXXXXX-ovh',
  address: '42 rue bar',
  legalform: 'individual' }

2. Fetch the bill list and their summaries then print the PDF urls

Without harmony proxies:

var async = require('async');

ovh.request('GET', '/me/bill', function (err, bills) {
  if (err) { throw err; }
  async.map(
    bills,
    function (bill, callback) {
      ovh.request('GET', '/me/bill/' + bill, callback);
    },
    function (err, bills) {
      if (err) { throw err; }
      bills.forEach(function (bill) {
        console.log(bill.pdfUrl);
      });
    }
  );
});
$ node bills.js
https://www.ovh.com/cgi-bin/order/bill.pdf?reference=FRXXXXXXX&passwd=xxxx
https://www.ovh.com/cgi-bin/order/bill.pdf?reference=FRXXXXXXX&passwd=xxxx
https://www.ovh.com/cgi-bin/order/bill.pdf?reference=FRXXXXXXX&passwd=xxxx
[...]

With harmony proxies:

var async = require('async');

ovh.me.bill.$get(function (err, bills) {
  if (err) { throw err; }
  async.map(
    bills,
    function (bill, callback) {
      this[bill].$get(callback);
    }.bind(this),
    function (err, bills) {
      if (err) { throw err; }
      bills.forEach(function (bill) {
        console.log(bill.pdfUrl);
      });
    }
  );
});
$ node --harmony-proxies bills.js
https://www.ovh.com/cgi-bin/order/bill.pdf?reference=FRXXXXXXX&passwd=xxxx
https://www.ovh.com/cgi-bin/order/bill.pdf?reference=FRXXXXXXX&passwd=xxxx
https://www.ovh.com/cgi-bin/order/bill.pdf?reference=FRXXXXXXX&passwd=xxxx
[...]

3. Send a SMS

Without harmony proxies:

ovh.request('POST', '/sms/{serviceName}/jobs', {
  serviceName: 'sms-aaXXXXX-1',
  message: 'Hello World!',
  sender: 'Foo',
  receivers: ['0033600000000']
 }, function (err, result) {
  console.log(err || result);
});
$ node sms.js
{ totalCreditsRemoved: 1, ids: [ 42 ] }

With harmony proxies:

ovh.sms['sms-aaXXXXX-1'].jobs.$post({
  message: 'Hello World!',
  sender: 'Foo',
  receivers: ['0033600000000']
}, function (err, result) {
  console.log(err || result);
});
$ node --harmony-proxies sms.js
{ totalCreditsRemoved: 1, ids: [ 42 ] }

Developing with node-ovh

API schemas checks

During development, you can ensure that the calls you use are still in beta or in production. You just need to declare the list of the APIs you are using:

var ovh = require('../node-ovh')({
  appKey: APP_KEY,
  appSecret: APP_SECRET,
  consumerKey: APP_CONSUMER_KEY,
  apis: ['me', 'sms']
});

ovh.me.foo.$get(function (err, result) {});
ovh.sms['sms-aaXXXXX-1'].histories.$get(function (err, result) {});
$ node --harmony-proxies dev.js
[OVH] Your call /me/foo was not found in the API schemas.
[OVH] Your API call /sms/sms-aaXXXXX-1/histories is tagged DEPRECATED since 2013-07-31T10:00:00+01:00 and will deleted on 2013-09-01T10:00:00+01:00. You can replace it with /sms/{serviceName}/outgoing/{id}

Debug mode

You can active the debug mode to log all the HTTP requests and responses:

var ovh = require('../node-ovh')({
  appKey: APP_KEY,
  appSecret: APP_SECRET,
  consumerKey: APP_CONSUMER_KEY,
  debug: true
});

ovh.domains.$get(function (err, domains) {});
$ node --harmony-proxies domains.js
[OVH] API call: GET /1.0/domains 
[OVH] API response to GET /1.0/domains : ["foo.com", "bar.fr"]

Examples of projects using node-ovh

node-ovh-pca

This module provides a fuse mount for OVH Public Cloud Archive.

NPM Github

node-ovh-ip

A really simple UI to manage the OVH network firewall.

NPM Github

License

node-ovh is freely distributable under the terms of the MIT license.

Copyright (c) 2012 - 2013 Vincent Giersch <mail@vincent.sh>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.