Thursday, 17 November 2016

How To Use SkyScanner API in ASP.Net?

In this era of technology, we think that we can get everything on internet. But we forgot that if we don't share then how will we/others get that.

This happens with me as well few weeks ago!

Well, I was given assignment of creating traveler page by using SkyScanner API. I thought that I will be able to do it easily by referring SkyScanner official API documentation but it was not so easy. I tried to find if anyone has shared any code snippet to consume SkyScanner API in ASP.Net but not much luck.

Here is the official SkyScanner API documentation.
https://support.business.skyscanner.net/hc/en-us/articles/210645649-Overview
https://support.business.skyscanner.net/hc/en-us/articles/211308489-Flights-Live-Pricing

So I researched and created traveler page by using SkyScanner API. I then thought let me share my research and code snippets with you in case if you are in the same boat.

Below is the landing page where user can search for flight based on different criteria. User can search for either “Round Trip” or “One Way” trip as per choice.



I created one landing page named default.aspx from where user can search for flights. On clicking search button, user will be redirected to search result page name flight-search.aspx

Airport API:

 Airport API is used to auto suggest airport when user type in “To” and “From” textbox



SkyScanner Flight API:

Skyscanner API is used to pull flight details as per given input. You can study API at official documentation https://support.business.skyscanner.net/hc/en-us/articles/211308489-Flights-Live-Pricingfor further details.






Search Result Page:

This will call Skyscanner API and will display result like this



Search Result Filters:

I have also implemented filters functionality which you can explore by looking into code and integrate it as per business requirement. Below screen shows all NON-STOP flights.



Below screen shows all fly-dubai flight as per filter. Clicking on “Book” button will redirect to actual airlines website to book the ticket.



How To Test SkyScanner API


Option-1: Use Postman

You can use any tool to test SkyScanner API, but my favorite one it “Postman” which is getting used by industry.

1)  Install Postman or any other tool as per your choice to test SkyScanner API



2) Send GET request and read “Location” field in HEADER from response.

var URL = "http://partners.api.skyscanner.net/apiservices/pricing/uk1/v1.0/" + sessionKey + "?apiKey=" + "prtl6749387986743898559646983194";




3) Now send POST request with all input values.

poststring = String.Format("apiKey={0}&country={1}&currency={2}&locale={3}&originplace={4}&destinationplace={5}&outbounddate={6}&adults={7}", "prtl6749387986743898559646983194", "AE", "AED", "en-US", fromAirPort, ToAirPort, oneWayDepartDate.ToString("yyyy-MM-dd"), passengers);



Option 2: Use SkyScanner Test Harness page:

SkyScanner has provided on test harness page where you can test your API key with all search paratmters to check flight search results.

This test harness page can be found at:




If you are looking for a complete SkyScanner API project or more details then get in touch with me by posting comment or send an email.

Hope this post will help you get started with SkyScanner API in .NET !

5 comments:

  1. Hi Thank you for saving my day, Skyscanner API documentation is not that great. I'm having an issue with the session key. Really appreciate if you can guide how to retrieve the session key from the API.

    Thanks in advance...!!!

    ReplyDelete
    Replies
    1. Thank you Vishi for your feedback.

      Below is a code snippet to get Session key:

      string poststring = String.Format("apiKey={0}&country={1}&currency={2}&locale={3}&originplace={4}&destinationplace={5}&outbounddate={6}&adults={7}", "[API-KEY]", "AE", "AED", "en-US", fromAirPort, ToAirPort, oneWayDepartDate.ToString("yyyy-MM-dd"), passengers);


      //Send FIRST Request to Get Session Key
      #region SendFirstRequest
      HttpWebRequest httpRequest1 = (HttpWebRequest)WebRequest.Create("http://partners.api.skyscanner.net/apiservices/pricing/v1.0");
      httpRequest1.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
      httpRequest1.IfModifiedSince = DateTime.Now;
      httpRequest1.Method = "POST";
      httpRequest1.ContentType = "application/x-www-form-urlencoded";
      byte[] bytedata = Encoding.UTF8.GetBytes(poststring);
      httpRequest1.ContentLength = bytedata.Length;
      Stream requestStream = httpRequest1.GetRequestStream();
      requestStream.Write(bytedata, 0, bytedata.Length);
      requestStream.Close();

      HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest1.GetResponse();
      Stream responseStream = httpWebResponse.GetResponseStream();
      pollURL = httpWebResponse.Headers["Location"].ToString();

      if (Session["sessionKey"] == null)
      {
      StringBuilder sb = new StringBuilder();
      using (StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8))
      {
      string line;
      while ((line = reader.ReadLine()) != null)
      {
      sb.Append(line);
      }
      }
      string[] sbl = httpWebResponse.Headers["Location"].ToString().Split('/');
      sessionKey = sbl[7];
      Session.Add("sessionKey", sessionKey);
      }
      else
      {
      sessionKey = Session["sessionKey"].ToString();
      }

      #endregion

      Let me know if you need any other assistance.

      Delete
    2. Dear Nilesh,

      Thank you so much for your quick response, really appreciate your thoughtfulness! :)

      Delete
  2. can u please send me sample project on kasmaniziad@outlook.com

    ReplyDelete
  3. The blog gave me idea to use To Use SkyScanner API in ASP.Net my sincere thanks for sharing this post Please Continue to share this post
    Dot Net Training in Chennai

    ReplyDelete