Skyscanner Flights API and Google script

6.1k views Asked by At

I am trying to learn how to use the Skyscanner Flights API with Google Script. It seems that the information available online is not adapted to newbies like me.

From what I got, the procedure to gain access to the flights' prices is : - to send a HTTP POST request with information about which flights we want information about - then send a HTTP GET request which will give us the pricing information

I would like to do that with Google Script.

Here is my code so far :

    function sky1() {

    /*
    Link to Skyscanner.com help : http://business.skyscanner.net/portal/en-                GB/Documentation/FlightsLivePricingList
    Link to Skyscanner api demo (api key given there): http://business.skyscanner.net/portal/en-        GB/Documentation/FlightsLivePricingQuickStart
    */


    var apikey = "prtl6749387986743898559646983194";// is given on skyscanner website for testing
    var url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apikey=" + apikey;
    // Post http request to skyscanner
    var  post_resp=sendHttpPost(url,apikey);
    }


    function sendHttpPost(url) {
     // post_params
      var post_params = {
          "Country": "CH",
          "Currency": "CHF",
          "Locale": "en-GB",
          "Adults": 1,
          "Children": 0,
          "Infants": 0,
          "OriginPlace": "12015",
          "DestinationPlace": "5772",
          "OutboundDate": "2015-08-09",
          "InboundDate": "2015-08-23",
          "LocationSchema": "Default",
          "CabinClass": "Economy",
          "GroupPricing": true
      };

      var options =
          {
            "method"  : "POST",
            "contentType" : "application/json", // didn't get what this means
            "payload" : JSON.stringify(post_params),  // didn't get what this means
            "muteHttpExceptions" : true, // avoids error message
          };

       var post_resp=UrlFetchApp.fetch(url,options);
       Logger.log(post_resp.getResponseCode());

       return post_resp;
    }

Any help would be very appreciated. This gives me a 415 response rode instead of a 201 indicating that a session has been created.

PS: I am not a programmer, I would be very grateful if we keep thing simple.

1

There are 1 answers

1
Skyscanner On

Skyscanner API team here. You may be interested to see a reference Javascript implementation at https://github.com/Skyscanner/skyscanner-api-js. I also recommend using Fiddler (a network tracing tool) to compare the request/response from the test harness at http://business.skyscanner.net/portal/en-GB/Documentation/FlightsLivePricingQuickStart with that of your code.