Code Examples
A number of code examples in different programming languages were prepared for you to get up and running with the Aviationstack API as quickly as possible. You will find them below in PHP, Python, Node.js., JQuerys, Go and Ruby .
Code Example - PHP
$queryString = http_build_query([
'access_key' => '2073bd914dab866afeef7d0c88ae8d85'
]);
$ch = curl_init(sprintf('%s?%s', 'https://api.aviationstack.com/v1/flights', $queryString));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$api_result = json_decode($json, true);
foreach ($api_result['results'] as $flight) {
if (!$flight['live']['is_ground']) {
echo sprintf("%s flight %s from %s (%s) to %s (%s) is in the air.",
$flight['airline']['name'],
$flight['flight']['iata'],
$flight['departure']['airport'],
$flight['departure']['iata'],
$flight['arrival']['airport'],
$flight['arrival']['iata']
), PHP_EOL;
}
}Code Example - Python
import requests
params = {
'access_key': '2073bd914dab866afeef7d0c88ae8d85'
}
api_result = requests.get('https://api.aviationstack.com/v1/flights', params)
api_response = api_result.json()
for flight in api_response['results']:
if (flight['live']['is_ground'] is False):
print(u'%s flight %s from %s (%s) to %s (%s) is in the air.' % (
flight['airline']['name'],
flight['flight']['iata'],
flight['departure']['airport'],
flight['departure']['iata'],
flight['arrival']['airport'],
flight['arrival']['iata']))Code Example - Nodejs
const axios = require('axios');
const params = {
access_key: '2073bd914dab866afeef7d0c88ae8d85'
}
axios.get('https://api.aviationstack.com/v1/flights', {params})
.then(response => {
const apiResponse = response.data;
if (Array.isArray(apiResponse['results'])) {
apiResponse['results'].forEach(flight => {
if (!flight['live']['is_ground']) {
console.log(`${flight['airline']['name']} flight ${flight['flight']['iata']}`,
`from ${flight['departure']['airport']} (${flight['departure']['iata']})`,
`to ${flight['arrival']['airport']} (${flight['arrival']['iata']}) is in the air.`);
}
});
}
}).catch(error => {
console.log(error);
});Code Example - jQuery
$.ajax({
url: 'https://api.aviationstack.com/v1/flights',
data: {
access_key: '2073bd914dab866afeef7d0c88ae8d85'
},
dataType: 'json',
success: function(apiResponse) {
if (Array.isArray(apiResponse['results'])) {
apiResponse['results'].forEach(flight => {
if (!flight['live']['is_ground']) {
console.log(`${flight['airline']['name']} flight ${flight['flight']['iata']}`,
`from ${flight['departure']['airport']} (${flight['departure']['iata']})`,
`to ${flight['arrival']['airport']} (${flight['arrival']['iata']}) is in the air.`);
}
});
}
}
});Code Example - Go
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Airline struct {
Name string `json:"name"`
}
type FlightInfo struct {
IATACode string `json:"iata"`
}
type Destination struct {
Airport string `json:"airport"`
IATACode string `json:"iata"`
}
type LiveData struct {
IsGround bool `json:"is_ground"`
}
type Flight struct {
Airline Airline `json:"airline"`
FlightInfo FlightInfo `json:"flight"`
Departure Destination `json:"departure"`
Arrival Destination `json:"arrival"`
Live LiveData `json:"live"`
}
type Response struct {
Flights []Flight `json:"results"`
}
func main() {
httpClient := http.Client{}
req, err := http.NewRequest("GET", "https://api.aviationstack.com/v1/flights", nil)
if err != nil {
panic(err)
}
q := req.URL.Query()
q.Add("access_key", "2073bd914dab866afeef7d0c88ae8d85")
req.URL.RawQuery = q.Encode()
res, err := httpClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
var apiResponse Response
json.NewDecoder(res.Body).Decode(&apiResponse)
for _, flight := range apiResponse.Flights {
if (!flight.Live.IsGround) {
fmt.Println(fmt.Sprintf("%s flight %s from %s (%s) to %s (%s) is in the air.",
flight.Airline.Name,
flight.FlightInfo.IATACode,
flight.Departure.Airport,
flight.Departure.IATACode,
flight.Arrival.Airport,
flight.Arrival.IATACode))
}
}
}Code Example - Ruby
require 'net/http'
require 'json'
params = {
:access_key => "2073bd914dab866afeef7d0c88ae8d85"
}
uri = URI('https://api.aviationstack.com/v1/flights')
uri.query = URI.encode_www_form(params)
json = Net::HTTP.get(uri)
api_response = JSON.parse(json)
for flight in api_response['results']
unless flight['live']['is_ground']
puts sprintf("%s flight %s from %s (%s) to %s (%s) is in the air.",
flight['airline']['name'],
flight['flight']['iata'],
flight['departure']['airport'],
flight['departure']['iata'],
flight['arrival']['airport'],
flight['arrival']['iata']
)
end
end