JavaScript SDK

Overview

1. Integrate the SDK in your website

Production Version:

⚠️ We no longer support the use of our JavaScript SDK
The documentation here is for existing users only. We encourage you to use our REST API instead directly with fetch() or any other library of your choice.

Getting started

Add the following script to your site:

var ob = new OBManager('https://api.openbooking.ch', '<insert token here>');
var query = {
    "types": ["apartment"],
    "language" : "de",
    "currency" : "CHF",
    //"checkin" : "",
    //"checkout" : "",
    //"rooms" : [
    //    { "adults": 2, "children": 0, "childrens_age": [] }
    //]
};
var options = {
    tracktag: "test"
,   pageSize: 15
}

var items = ob.getItems(query, options)

Get items returns a Items-Instance, this instance allows you to easily get successive pages by calling nextPage on it. You can call reset this way the next call to nextPage will return the first page again.

items.nextPage().then(function(page) {
    console.log(page);
}).catch(function(err) {
    console.log(err);
});

You can also get a specific page directly based on its (zero based) index.

items.page(5).then(function(page) {
    console.log(page);
}).catch(function(err) {
    console.log(err);
});

This also sets the internal state to page 5, this means that the next call to nextPage will return page 6.

If you call nextPage multiple times or use page with an index you will eventually reach the last page. The last page may have less elements then the specified pageSize. Requesting a page past the last page will return an empty array.

[{
  city: "Sureggio (Lugano)",
  city_normalized: "Sureggio (Lugano)",
  externals: [],
  features: [],
  hookData: {
    "classification": {
      "statusCode":408,
      "executed":"2016-10-14T10:11:54.434Z"
    },
    "resort": {
      "statusCode":200,
      "data":{
        "resortKey":2632,
        "regionId":"012"
      }
      ,"executed":"2016-10-14T10:11:49.480Z"
    }
  },
  id: "577d16f1074db10b006968d7",
  images: [{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/sureggio_asb_01_.JPG-940x622-79330.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/sureggio_feriensiedlung_asb_06_.jpg-940x622-79330.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/sureggio_feriensiedlung_aan_01_.jpg-940x622-79330.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/sureggio_aso_01_.jpg-940x622-79330.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/sureggio_feriensiedlung_aso_22_.jpg-940x622-79330.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/sureggio_feriensiedlung_aso_600_.JPG-940x622-79330.jpg"
  },{
    "season":"summer",
    "url":"http://www.reka.ch/FewoImageList/sureggio_feriensiedlung_aso_05_.jpg-940x622-79330.jpg"
  },{
    "season":"winter",
    "url":"http://www.reka.ch/FewoImageList/sureggio_feriensiedlung_aso_01_.tif-940x622-79330.jpg"
  }],
  imagesServices:   [],
  name: {"de":"Feriensiedlung Sureggio, Sureggio "},
  name_add: {"de":"Typ K, 3-Zi.-Wohnung/5 Pers., 55m², Parterre"},
  position: {"lat":46.0535013359,"lng":8.9730388081},
  provider_name:    "reka",
  quality:  "3",
  ranks:    [],
  superior: false,
  type: "apartment",
  visible:  true
},
{
  city: "Moléson (ob Greyerz)",
  city_normalized: "Moléson (ob Greyerz)",
  externals: [],
  features: [],
  hookData: {
    "classification": {
      "statusCode":408,
      "executed":"2016-10-14T10:11:54.756Z"
    },
    "resort": {
      "statusCode":200,
      "data": {
        "resortKey":795,
        "regionId":"013"
      },
      "executed":"2016-10-14T10:11:49.802Z"
    }
  },
  id:   "577d16f1a20ea20c009fbbd3",
  images:   [{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/moleson_andromede_awi_200_.jpg-940x622-101335.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/moleson_andromede_iwz_01_.jpg-940x622-101335.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/moleson_andromede_iwz_02_.jpg-940x622-101335.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/moleson_andromede_iku_02_.jpg-940x622-101335.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/moleson_andromede_ibd_03_.jpg-940x622-101335.jpg"
  },{
    "season":"",
    "url":"http://www.reka.ch/FewoImageList/moleson_andromede_ibd_04_.jpg-940x622-101335.jpg"
  },{
    "season":"summer",
    "url":"http://www.reka.ch/FewoImageList/moleson_andromede_aso_03_.jpg-940x622-101335.jpg"
  },{
    "season":"winter",
    "url":"http://www.reka.ch/FewoImageList/moleson_andromede_awi_01_.tif-940x622-101335.jpg"
  }],
  imagesServices:   [],
  name: {"de":"Andromède"},
  name_add: {"de":"Typ B, 2-Zi.-Wohnung/4 Pers., 53m², 1. Stock"},
  position: {"lat":46.5627267438,"lng":7.0354476192},
  provider_name:    "reka",
  quality:  "3",
  ranks:    [],
  superior: false,
  type: "apartment",
  visible:  true
}, ...]

pageCount returns the number of pages. This is useful if you want to show a pager below your results.

items.pageCount().then(function(count) {
    console.log(count);
}).catch(function(err) {
    console.log(err);
});

Reference

For the convenience to our API consumers, we offer a JavaScript SDK, which enables you to set-up your custom UI quickly and connect to our real-time Socket.IO-based Pusher channel to speed up Query runtime.

Overview

1. Integrate the SDK in your website

Production Version:

<script src="<https://api.openbooking.ch/js/ob.min.js>"></script>

Development Version:

<script src="<https://api.openbooking.ch/js/ob.dev.js>"></script>

2. Instantiate an instance of OBManager

OBManager is the main object to communicate with the OpenBooking API client-side.

<script>
  var ob = new OBManager("<https://api.openbooking.ch>", "[PUT YOUR TOKEN HERE]");
</script>

Method Reference

setAcceptLanguage

countAccommodations

Returns the number of available accommodation objects (core data) using the provided filter.

var filter = {
  type: "hotel"
}

obManager.countAccommodations(filter: Object, cb: function(err, val))

The filter is an Object containing filter parameters, for example { type: "apartment" } will return just the accommodations that match that criteria.

Filter Parameters: see “parameter” table below.

cb: Will be called with the results.

getAccommodations

Fetch accommodation core objects based on an optional filter.

ob.getAccommodations(filter:Object, skip:Integer, limit:Integer, cb:Function(err, val))

The filter is an Object containing filter parameters, for example { type: "apartment" } will return just the accommodations that match that criteria.

Filter Parameters: see “parameter” table below.

skip is to skip some items, if empty it will default to 0.

limit is to set the limit of the accommodations retrieved, defaults to 100.

cb will be called everytime we get accommodations, example of a cb function(err, accommodations)

var filter = {visible:true, features:"restaurant", ...};
ob.getAccommodations(filter, 10, 0, function(err, data) {
  console.log(err);
  console.log(data);
})

getAllAccommodations

If you need to get all the accommodations, instead of using the 2 above functions you can use this one. Will return you all the accommodations, 100 at the time, means tha cb will be called many times.

obManager.getAllAccommodations(filter:Object, cb:Function(err, val))

getAccommodation

Get a single accommodation by ID.

obManager.getAccommodation(id:String, cb:Function)

makeQuery

The main feature of Openbooking is making queries. When a query is issued we will contact the Booking Engines and return the data to you.

obManager.makeQuery(query:Object, [options:Object], cb:Function(err, data:Array, requestid:String))

With the options parameter, which is NOT mandatory, you can pass an object with the following fields:

  • finish a callback which is called when the Query is fully executed
  • finishProvider a finish callback for every single booking provider
  • filter a filter object, see below
  • tracktag a short string used to distinguish different queries from another. Max length 30 characters, no whitespace. Tracktags beginning with underscore are reserved for internal use.

When making a query, see example below, you will get, through the cb, the availabilities that you receive and the library stores it in a DataManager which are easily querable for you afterwards. Basicaly when a result comes you can add it to your page via jQuery or you can just load the data again from the DataManager included in the library, so you can filter and sort the data directly.

Example of a query:

{
  "checkin": "2015-07-12T00:00:00.000",
  "checkout": "2015-07-14T00:00:00.000",
  "rooms": [{"adults": "2","children": 0}],
  "language": "de",
  "currency": "CHF"
}

Filtering:

You can pass a filter parameter with an object with the following fields:

  • type: can be a type or all. Currently the following types are supported: hotelappartmentoffer
  • reference_id: pass an accommodationId here to get results only for a single accommodation
// makeQuery returns a requestid which is also passed to the callbacks
// Example:

var my_app_state = {};
var ob = new OBManager('https://api.openbooking.ch', token);

ob.finishedProviderCB = function(data) {
    if(data.requestid == my_app_state.last_requestid) {
        ...
    }
};
ob.finishedCB = function(data) {
    if(data.requestid == my_app_state.last_requestid) {
        ...
    }
};

var resultCB = function (err, data, requestid) {
    if(requestid == my_app_state.last_requestid) {
        ...
    }
};

var hash_or_query = ...;
var options = {tracktag: "mycustomtracktag"};
my_app_state.last_requestid = ob.makeQuery(hash_or_query, options, resultCB);

createQuery

Use this call to create a query and get a Query ID back without acutally executing the query.

getQuery

Get a Query by its hash.

obManager.getQuery(hash:String, cb:Function(err, value))

getResultsByOriginPerAccommodationId

When we get availabilities and we want to book a room, maybe, if we group our availabilities by accommodation, we want to show the accommodation and all its availabilities to do this you can use this method.

obManager.getResultsByOriginPerAccommodationId(origin:String, accommodationId:String, cb:Function)

origin is the hash of the query

accommodationId is the accommodationId which you want to get the availabilities for

cb is the callback that returns the availabilities. Provide a Function with the following signature:

function (err, data) {
  if (err) {
    console.log("Something went wrong, most likely the query origin did not exist");
    return
  }
  
  if (data && data.length > 0) {
    data.forEach(
      function(aResult){
        // do something with the results 
      }
    )}
  } else {
    // No results found
    // Query is either too old or results are expired/not cached
  }
}

getItems( parameters, options ) -> Items

getItems() returns an instance of the Items objects, which allows you to easily paginate and sort.

Example:

var items = ob.getItems({
  types: ["apartment"],
  checkin: "", 
  checkout: "", 
  rooms: [{
    adults: 2, 
    children: 0,
    childrens_age: []
  }]
}, {
  tracktag: "", 
  pageSize: 15
}).sortBy("price")

parameters can contain the same parameters as documented in the REST API:

Items.sortBy( sortString ) -> Items

sortString can be one of the following values.

Default sort order is ascending. For descending sort order add prefix -

Sort type Note
name
price
classification apartments only
quality
facts_bedrooms apartments only
facts_maxadults apartments only
random
position used together with position/radius filter. The order is always “nearest first”, descending is not supported
contentscore sort by content quality

Example:

.sortBy("-price")

Items.page( pageIndex, cb ) -> promise

Items.nextPage( cb ) -> promise

Items.prevPage( cb ) -> promise

Items.reset()

Items.currentPage( cb ) -> promise

Items.pageCount( cb ) -> promise

Items.count( cb ) -> promise

Items Callback

Callback signature:

function(err, page) {}

Page is an array of a “light” accommodation objects:

[{
  "id": "",
  "name": {},
  "name_add": {},
  "images": [],
  "imagesServices": [],
  "ranks": [],
  "provider_name": "",
  "features": [],
  "quality": "",
  "superior": true,
  "position": {},
  "visible":true,
  "type":"apartment",
  "city":"Nendaz",
  "city_normalized":"",
  "externals":[],
  "hookData":{},
  "price":{},
  ...
}, ...]

getAvailabilities(accommodationId, cb) -> promise

You can pass a callback cb or leave it undefined to get a promise as return value. Returns a calendar with Arrival days, available days and minimum stays in this format:

[{
  accommodation:	"57a9d0c58f28830d008c7302",
  arrival:	[161128,161129,161130,161201,161202,161203,161204,161205,161206,161207,161208,161209,161210,161211,161212,161213,161214,161215,161216,161217,161218,161219,161220,161221,161222,161223,161224,161231,170121,170211,170318,170325,170401,170415,170429],
  free:	[161128,161129,161130,161201,161202,161203,161204,161205,161206,161207,161208,161209,161210,161211,161212,161213,161214,161215,161216,161217,161218,161219,161220,161221,161222,161223,161224,161225,161226,161227,161228,161229,161230,161231,170101,170102,170103,170104,170105,170106,170121,170122,170123,170124,170125,170126,170127,170211,170212,170213,170214,170215,170216,170217,170318,170319,170320,170321,170322,170323,170324,170325,170326,170327,170328,170329,170330,170331,170401,170402,170403,170404,170405,170406,170407,170415,170416,170417,170418,170419,170420,170421,170429,170430],
  minstay:	[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,7,7,7,7,7,7,7,7],
  provider:	"57a9d0b173de570c00977fee"
}, ...]
  • The dates in arrival and free are integers in the format yymmdd
  • For each date in arrival there is a matching minstay element, denoting the number of minimum stay days for a particular arrival day
  • different providers can report different availability data

Example:

ob.getAvailabilities(id).then(function(availabilities) {
  if(availabilities.length > 0) {
    ...
  } else {
    console.log("for accommodation '"+id+"' there is currently no availability data");
  }
})