Getting Flights info from the Kayak API in ASP.Net December 1
Kayak is one of the top travel meta-search engines on the web, and they have a free API which allows you to get flight and hotel information.
To use the Kayak API, first get a developer key
Then you can read the full specifications to get the info. Or, just use the code below
This gets the flights given an Origin, Destination, Departure date and the number of travellers (note: Origin & Destination must be in the form of a 3 letter Airport code, eg: MEL = Melbourne, SYD = Sydney, LAX = Los Angeles).
You can also Download the full source code
Private Function Flights(ByVal dtDepartDate As Date, _
ByVal sOriginCode As String, _
ByVal sDestinationCode As String, _
ByVal iTravellers As Integer) As DataTable
Dim sResult As String
Dim oCookies As New CookieContainer
Dim sURL As String
Dim oDoc As New System.Xml.XmlDocument
Dim sSID As String
Dim sSearchID As String
Dim bMorePending As Boolean = True
Dim oTripList As System.Xml.XmlNodeList
Dim oTrip As System.Xml.XmlNode
Dim oFlights As DataTable
Dim oFlight As DataRow
Dim iFlightsFound As Integer
Dim sDeveloperKey As String = "***Your Key Here****"
'Full specs at: http://www.kayak.com/labs/api/search/spec.html
'Get the SessionID
sURL = "http://www.kayak.com/k/ident/apisession?version=1&token=" & sDeveloperKey
sResult = sGetData(sURL, oCookies)
oDoc.LoadXml(sResult)
sSID = oDoc("ident")("sid").InnerText
'Start the Flight Search & get the SearchID
sURL = "http://www.kayak.com/s/apisearch?" & _
"basicmode=true&oneway=y" & _
"&origin=" & sOriginCode & _
"&destination=" & sDestinationCode & _
"&depart_date=" & Format(dtDepartDate, "MM/dd/yyyy") & _
"&depart_time=a&" & _
"return_date=" & _
"&return_time=a" & _
"&travelers=" & iTravellers & _
"&cabin=b&action=doflights&apimode=1&_sid_=" & sSID
sResult = sGetData(sURL, oCookies)
oDoc.LoadXml(sResult)
sSearchID = oDoc("search")("searchid").InnerText
oFlights = dtFlightsTableInit()
Do While bMorePending = True
'Get the first lot of results
sURL = "http://www.kayak.com/s/basic/flight?" & _
"searchid=" & sSearchID & _
"&c=10&apimode=1&_sid_=" & sSID
sResult = sGetData(sURL, oCookies)
oDoc.LoadXml(sResult)
bMorePending = IIf(oDoc("searchresult")("morepending").InnerText = "true", True, False)
If bMorePending = True Then
'Wait for 5 seconds, then poll again
System.Threading.Thread.Sleep(5 * 1000)
End If
Loop
iFlightsFound = Val(oDoc("searchresult")("count").InnerText)
'Build the final request, asking for the total number of flights found by Kayak.com
sURL = "http://www.kayak.com/s/basic/flight?" & _
"searchid=" & sSearchID & _
"&c=" & iFlightsFound & _
"&apimode=1&_sid_=" & sSID
sResult = sGetData(sURL, oCookies)
oDoc.LoadXml(sResult)
oTripList = oDoc.SelectNodes("//trip")
For Each oTrip In oTripList
oFlight = oFlights.NewRow()
oFlight("DepartDate") = oTrip("legs")("leg")("depart").InnerText
oFlight("ArriveDate") = oTrip("legs")("leg")("arrive").InnerText
oFlight("Airline") = oTrip("legs")("leg")("airline_display").InnerText
oFlight("Price") = oTrip("price").InnerText
oFlight("BookURL") = oTrip("price").Attributes("url").Value
oFlights.Rows.Add(oFlight)
Next
Flights = oFlights
End Function
Public Function dtFlightsTableInit() As DataTable
Dim dtFlights As New DataTable
dtFlights.Columns.Add(New DataColumn("DepartDate", GetType(String)))
dtFlights.Columns.Add(New DataColumn("ArriveDate", GetType(String)))
dtFlights.Columns.Add(New DataColumn("Airline", GetType(String)))
dtFlights.Columns.Add(New DataColumn("Price", GetType(String)))
dtFlights.Columns.Add(New DataColumn("BookURL", GetType(String)))
dtFlightsTableInit = dtFlights
End Function
Public Function sGetData(ByVal sURL As String, _
Optional ByRef oCookies As CookieContainer = Nothing) As String
Dim Writer As StreamWriter = Nothing
Dim WebRequestObject As HttpWebRequest
Dim sr As StreamReader
Dim WebResponseObject As HttpWebResponse
Dim sbResultsBuilder As New StringBuilder
Dim sBuffer(8192) As Char
Dim iRetChars As Integer
WebRequestObject = CType(WebRequest.Create(sURL), HttpWebRequest)
WebRequestObject.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
WebRequestObject.Method = "GET"
WebRequestObject.Timeout = 55000
WebRequestObject.ReadWriteTimeout = 55000
WebRequestObject.AllowAutoRedirect = True
If Not (oCookies Is Nothing) Then
WebRequestObject.CookieContainer = oCookies
End If
WebResponseObject = CType(WebRequestObject.GetResponse(), HttpWebResponse)
sr = New StreamReader(WebResponseObject.GetResponseStream)
Do
iRetChars = sr.Read(sBuffer, 0, sBuffer.Length)
If iRetChars > 0 Then
sbResultsBuilder.Append(sBuffer, 0, iRetChars)
End If
Loop While iRetChars > 0
sGetData = sbResultsBuilder.ToString
End Function
The Kayak API is pretty good, but does have some limitations, so I think is works best when you use it for stats rather than bookings. Anyone else know of some good travel APIs?
This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!
Related posts:
Bookmarks for Pingomatic and Pingoat » »
If you liked this, then subscribe to my RSS feed

Jacob Dec 2
TravelFusion is another travel API which gets you flight information.
http://travelfusion.com/xmlspec/v2/
Dale Dec 2
Just a note, I took a look at the Kayak API a couple years back. Then I was going to work with it again, but had an issue. When I emailed support, I got back this response:
———- Forwarded message ———-
From: Jim Giza, Kayak Director Technology
Date: Tue, Sep 23, 2008 at 4:12 PM
Subject: Your Kayak feedback from Today
We discourage use of the Kayak XML Search API and no longer have it listed on the site.
Please see http://www.Kayak.com/help/affiliate.html for details of our Affiliate program. Affiliates can monetize their sites with Kayak through the use of banners, links, RSS feeds and search forms.
Kind regards,
Jim Giza
billo Dec 2
You can always ping me with questions about the API. It still works, and probably always will; but it’s more for light/non-commercial use, since our experience with commercial licenses hasn’t worked out well for us or the users.
Thanks for posting the ASP.net example. You probably should remove your developer key, though, since if anybody starts using your sample, you’ll hit your quota pretty quick.
You can probably guess my email address.
Best Regards,
billo, chief architect, kayak.com
Gath Dec 2
Hi,
Dale - I was aware that they were not actively promoting the api. However, I still think it provides useful information.
Billo - Good to hear from someone at Kayak
Thanks for pointing out that I left the key in, I must be tired…
Wayne John Dec 24
Nice GetData proc, I think I need to steal that and compare to mine. You’re doing a little more in there than I have been.