Basic usage example

Query plane tickets, from several airlines, using the Kiwi API (similar to Google Flights).

The API is documented at https://docs.kiwi.com/.

Usage

Get city or country IDs

library("rflights")
# get Argentina and toulouse IDs
arg_id <- find_location("Argentina", "country")
length(arg_id) # only one result, so it might be the one
## [1] 1
arg_id <- arg_id[[1]]
names(arg_id)
##  [1] "id"                "active"            "code"             
##  [4] "name"              "slug"              "alternative_names"
##  [7] "rank"              "global_rank_dst"   "neighbours"       
## [10] "organizations"     "currency"          "region"           
## [13] "continent"         "location"          "type"
arg_id$id
## [1] "AR"
arg_id$continent
## $id
## [1] "south-america"
## 
## $code
## [1] "SA"
## 
## $name
## [1] "South America"
## 
## $slug
## [1] "south-america"
arg_id <- arg_id$id

tl_id <- find_location("toulouse")
length(tl_id)
## [1] 5
lapply(tl_id, function (x) x$type)
## [[1]]
## [1] "city"
## 
## [[2]]
## [1] "airport"
## 
## [[3]]
## [1] "bus_station"
## 
## [[4]]
## [1] "tourist_region"
## 
## [[5]]
## [1] "bus_station"
# we are looking for the city
tl_id <- tl_id[[which(sapply(tl_id, function (x) x$type == "city"))]]
tl_id$country
## $id
## [1] "FR"
## 
## $name
## [1] "France"
## 
## $slug
## [1] "france"
## 
## $code
## [1] "FR"
tl_id <- tl_id$id
tl_id
## [1] "toulouse_fr"

Get flight prices

# get flights from Argentina to toulouse around 01 July to 09 July
# Maybe I can go to the user2019 đŸ€”??
flights <- get_flights(fly_from = "AR", fly_to = "toulouse_fr",
                       date_from = "01/09/2019", date_to = "09/09/2019")
length(flights)
## [1] 223
names(flights[[1]])
##  [1] "id"                            "countryFrom"                  
##  [3] "countryTo"                     "bags_price"                   
##  [5] "baglimit"                      "dTime"                        
##  [7] "aTime"                         "dTimeUTC"                     
##  [9] "p1"                            "p2"                           
## [11] "p3"                            "aTimeUTC"                     
## [13] "price"                         "flyFrom"                      
## [15] "mapIdfrom"                     "mapIdto"                      
## [17] "flyTo"                         "distance"                     
## [19] "cityFrom"                      "cityTo"                       
## [21] "route"                         "routes"                       
## [23] "airlines"                      "nightsInDest"                 
## [25] "pnr_count"                     "transfers"                    
## [27] "has_airport_change"            "virtual_interlining"          
## [29] "fly_duration"                  "duration"                     
## [31] "facilitated_booking_available" "type_flights"                 
## [33] "found_on"                      "conversion"                   
## [35] "booking_token"                 "quality"                      
## [37] "deep_link"
sapply(flights, function(x) x$price)
##   [1]  667  667  667  668  669  671  679  680  686  691  699  699  703  708
##  [15]  708  712  715  716  717  717  718  718  720  720  724  723  725  727
##  [29]  728  735  736  739  740  742  743  743  747  746  749  749  751  752
##  [43]  752  754  756  758  758  760  760  761  762  763  763  764  764  764
##  [57]  764  766  767  769  768  770  771  771  771  771  772  773  773  773
##  [71]  774  774  774  774  775  775  775  774  776  776  776  776  776  776
##  [85]  777  778  778  780  780  780  781  782  781  783  784  783  784  786
##  [99]  786  786  787  788  787  788  788  790  789  790  791  791  791  792
## [113]  793  792  793  793  794  794  794  794  795  796  796  797  796  797
## [127]  796  797  798  797  798  799  799  801  800  802  801  802  802  803
## [141]  803  803  803  804  804  804  804  804  805  805  806  806  808  807
## [155]  809  810  810  811  811  812  812  813  812  814  814  814  816  818
## [169]  818  819  820  819  821  821  821  822  822  822  825  826  826  827
## [183]  827  827  827  827  829  828  829  829  830  830  830  831  831  832
## [197]  832  832  832  834  833  834  834  834  834  836  837  838  837  837
## [211]  838  839  840  840  841  870  943 1089 1129 1133 1190 1318 1344

Examples

Create flight price alarms

I used it to alert through a Pushbullet message.

my_savings <- 25 # yup, just 25USD 😣
found_ticket <- FALSE
while (!found_ticket) {
  flights <- get_flights(fly_from = "AR", fly_to = "toulouse_fr",
                         date_from = "01/09/2019", date_to = "09/09/2019")
  flights <- flights[sapply(flights, function(x) x$price) <= my_savings]
  if (length(flights) > 0) {
    send_alert(paste0(
      "There is a plane ticket you can afford!\n",
      "Check it out at Kiwi.com"))
    # user-defined alert function (not in rflights)
  }
}

Fly to anywhere

Find plane tickets from my city to anywhere, from today to 2 next weeks.

# I am a freelancer, let's go anywhere!
flights <- get_flights(fly_from = "COR",
                       date_from = Sys.Date(), date_to = Sys.Date()+2*7)
length(flights)
## [1] 218
head(t(sapply(flights, function(x) c(x$price, x$cityTo))), n = 20)
##       [,1] [,2]                   
##  [1,] "25" "Buenos Aires"         
##  [2,] "25" "Buenos Aires"         
##  [3,] "25" "Buenos Aires"         
##  [4,] "25" "Buenos Aires"         
##  [5,] "28" "San Miguel de TucumĂĄn"
##  [6,] "28" "San Miguel de TucumĂĄn"
##  [7,] "28" "San Miguel de TucumĂĄn"
##  [8,] "28" "Buenos Aires"         
##  [9,] "28" "Buenos Aires"         
## [10,] "28" "Buenos Aires"         
## [11,] "30" "Buenos Aires"         
## [12,] "30" "Buenos Aires"         
## [13,] "30" "Buenos Aires"         
## [14,] "30" "Buenos Aires"         
## [15,] "30" "Salta"                
## [16,] "30" "Neuquén"              
## [17,] "30" "Neuquén"              
## [18,] "34" "Puerto IguazĂș"        
## [19,] "34" "Puerto IguazĂș"        
## [20,] "33" "Neuquén"