Search code examples
javascriptamadeus

How to get full list of cities and airports with their corresponding codes?


I am building a travel booking site using Amadeus self service web-service. I have come to a point where I want to the users to be able to search perform a relative search for a city or airport using the actual name instead of IATA codes since. The problem is that the Amadeus API doesn't really solve this problem for me. This is like this because the request process requires me ton include a country code as part of the request parameters. is there anyway I can achieve this without the countryCode field?

After Including the Access Token in the header. I get this. https://i.sstatic.net/9OqZn.png

```async function airportAndCitySearch(keyword) {
  console.log("Searching " + keyword);
  var form = new FormData();
  var settings = {
    "url": "https://test.api.amadeus.com/v1/reference-data/locations?subType=CITY,AIRPORT&keyword=" + keyword + "&countryCode=DE",
    "method": "GET",
    "timeout": 0,
    "processData": false,
    "mimeType": "multipart/form-data",
    "contentType": false,
    "data": form
  };```

I manage to get the Access Token with this request.

  console.log("Requesting Token...");
  var settings = {
    "url": "https://test.api.amadeus.com/v1/security/oauth2/token",
    "method": "POST",
    "timeout": 0,
    "data": {
      "client_id": CLIENT_ID,
      "client_secret": CLIENT_SECRET,
      "grant_type": "client_credentials"
    }
  };

  $.ajax(settings).done(function (response) {
    console.log("Assigning Token variables...");
    ACCESS_TOKEN = response.access_token;
    console.log("Access Token : " + ACCESS_TOKEN);
  });
}







Solution

  • Amadeus has an endpoint that doesn't require country code.

    Endpoint:

    GET https://test.api.amadeus.com/v1/reference-data/locations?subType=AIRPORT,CITY&keyword=r&page[limit]=5
    

    See here the definition: https://developers.amadeus.com/self-service/category/air/api-doc/airport-and-city-search/api-reference

    Example:

    https://test.api.amadeus.com/v1/reference-data/locations?subType=CITY,AIRPORT&keyword=paris&page[limit]=5

    Answer: https://pastebin.com/efrmUiYs