A developer journey

moon indicating dark mode
sun indicating light mode

Node JS app for amadeus API

January 07, 2020

Hello developers! Today we’re going to learn how to build a back end for a flight booking app with Node.js using Amadeus Quick-Connect, a flight booking bundle featuring Flight Offers Search, Flight Offers Price and Flight Create Orders, all available in our Self-Service catalog. We will also implemented a service using airport city search. LEt’s start coding !

Set up a simple Node.js server

FIrst thing first we need a simple Node js server to get it running

server.js

var express = require('express')
app = express(),
port = process.env.PORT || 3000;
app.listen(port);
// console.log(tree)
console.log('Amadeus RESTful API server started on: ' + port);

This code is an instance of an express server. Who listen on the port 3000.

Get an API token

THe amadeus self service API authentication is documented here. In other words you just need to get a token from the authentification endpoint and then you will pass it to the header of your futur request. Here is the code to perform it :

let headers= {
'Content-Type': 'application/x-www-form-urlencoded',
};
let body = {
"grant_type": "client_credentials",
"client_id": YOUR_CLIENT_ID,
"client_secret": YOUR_CLIENT_SECRET,
}

Then we use fetch to post our credentials and get the token

//airport city search
const uriAuth ="https://test.api.amadeus.com/v1/security/oauth2/token"
//init token
let token="";
fetch(uriAuth, { method: 'POST',
headers: headers,
body: 'grant_type=client_credentials&client_id=' + body.client_id + '&client_secret=' + body.client_secret
})

Creating a post request to get a list of flight deals

Here we will use our token freshly generated to pass it to the header of our request. We build the request on the back end for some security reason and best practices.

async function updateFlightSearch() {
// Default options are marked with *
const response = await fetch("https://test.api.amadeus.com/v2/shopping/flight-offers", {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json',authorization: 'Bearer '+token
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *client
body: JSON.stringify(request) // body data type must match "Content-Type" header
});
return await response.json(); // parses JSON response into native JavaScript objects
}

Calling Flight Offers Price to confirm the final price

To perfom a booking the process need to confirm the price before send it to create order endpoint. FOr that we select an item of the repsonse from flight offer search and we pass it to an other request.

async function flifghtPrice(inputFlightOffer) {
// Default options are marked with *
const response = await fetch("https://test.api.amadeus.com/v2/shopping/flight-offers/pricing", {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json',authorization: 'Bearer '+token
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *client
body: JSON.stringify(inputFlightOffer) // body data type must match "Content-Type" header
});
return await response.json(); // parses JSON response into native JavaScript objects
}

Then you will received your flight offer confirmation and you can pass it to the next step

Calling Flight Orders Create to complete the final booking

Now we have selected a flight offer, confirm the price, it’s time to book our ticket. We can do it in the next process. We comnstruct a request with the flight offer selected and some data about the passenger. Name lastname mail will be enought for the demonstration.this request is pass to a function :

async function CreateOrder(requestCreateOrder) {
// Default options are marked with *
const response = await fetch("https://test.api.amadeus.com/v1/booking/flight-orders", {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json',authorization: 'Bearer '+token
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *client
body: JSON.stringify(requestCreateOrder) // body data type must match "Content-Type" header
});
return await response.json(); // parses JSON response into native JavaScript objects
}

In the repsonse opbject you have a value call reference, here it is you got your flight confirmation number !

Then simply it npm run start and play with your api with postman !

Complete code

The complete code is available on github complete code