Python
Python SDK
Installation
You can call the Amadeus APIs using the Python SDK. The Python SDK has been uploaded to the official Python package repository, which makes life easier since you can install the SDK as a regular Python package.
Prerequisites
- Amadeus for Developers API key and API secret: to get one, create a free developer account and set up your first application in your Workspace.
- Python version >= 3.8
First step is to create the environment. Switch to your project repository and type:
A new folder .venv
will be created with everything necessary inside. Let's activate the isolated environment with the following command:
From now on, all packages installed using pip
will be located under .venv/lib
and not in your global /usr/lib
folder.
Finally, install the Amadeus SDK as follows:
You can also add it to your requirements.txt
file and install using:
The virtual environment can be disabled by typing:
Your first API call
This tutorial will guide you through the process of creating a simple Python application which calls the Airport and Search API using the Amadeus for Developers Python SDK.
Request
- Once you import the amadeus library, you initialize the client by adding your credentials in the
builder
method. The library can also be initialized without any parameters when the environment variablesAMADEUS_CLIENT_ID
andAMADEUS_CLIENT_SECRET
are present. - The authentication process is handled by the SDK and the access token is renewed every 30 minutes.
- The SDK uses namespaced methods to create a match between the APIs and the SDK. In this case, the API
GET /v1/reference-data/locations?keyword=LON&subType=AIRPORT
is implemented asamadeus.reference_data.locations.get(keyword='LON',subType=Location.AIRPORT)
.
Handling the response
Every API call returns a Response
object. If the API call contains a JSON response, it will parse the JSON into the .result
attribute. If this data also contains a data key, it will make that available as the .data
attribute. The raw body of the response is always available as the .body
attribute.
Arbitrary API calls
You can call any API not yet supported by the SDK by making arbitrary calls.
For the get
endpoints:
For the post
endpoints:
Video Tutorial
You can also check the video tutorial on how to get started with the Python SDK.
Managing API rate limits
Amadeus Self-Service APIs have rate limits in place to protect against abuse by third parties. You can find Rate limit example in Python using the Amadeus Python SDK here.
Python Async API calls
In a synchronous program, each step is completed before moving on to the next one. However, an asynchronous program may not wait for each step to be completed. Asynchronous functions can pause and allow other functions to run while waiting for a result. This enables concurrent execution and gives the feeling of working on multiple tasks at the same time.
In this guide we are going to show you how to make async API calls in Python to improve the performance of your Python applications.
For all these examples we are going to call the Flight-Checkin Links API.
Prerequisites
To follow along with the tutorial you will need the followings:
-
Python version >= 3.8
-
Amadeus for Developers API key and API secret: to get one, create a free developer account and set up your first application in your Workspace.
-
aiohttp
: you will use the aiohttp library to make asynchronous API calls. You can install it using the commandpip install aiohttp
. -
requests
: you will use the requests library for synchronous requests. You can install it using the commandpip install requests
. -
amadeus
: the Amadeus Pthon SDK. You can install it using the commandpip install amadeus
.
Async API calls with aiohttp
aiohttp
is a Python library for making asynchronous HTTP requests build top of asyncio
. The library provides a simple way of making HTTP requests and handling the responses in a non-blocking way.
In the example below you can call the the Amadeus Flight-Checkin link API using the aiohttp
library and the code runs in an async way.
The above code makes POST
request to the Authentication API using the requests
library. The returned access token is then used in the headers of following requests to make 20 asyncronous API calls.
Async API calls with thread executor
Since we offer the Python SDK we want to show you how you are able to make async API calls using the SDK. The SDK is built using the requests library
which only supports synchronous API calls. This means that when you call an API, your application will block and wait for the response. The solution is to use a thread executor to allow run blocking calls in separate threads, as the example below:
OpenAPI Generator
In this tutorial, we'll guide you through the process of making your first API calls using the OpenAPI Generator in Python. To begin, you'll need to retrieve the specification files from the GitHub repository. In this example, you will use the Authorization_v1_swagger_specification.yaml
and FlightOffersSearch_v2_swagger_specification.yaml
files.
Before getting started make sure you check out how to generate client libraries with the OpenAPI Generator.
Call the Authorization endpoint
You will now learn how to call the POST https://test.api.amadeus.com/v1/security/oauth2/token
endpoint in order to get the Amadeus access token.
Open your terminal and generate the Python client with the following command:
auth
which contains the generated library.
You can install the library using pip:
Then create a file auth.py
and add the following code to generate an Amadeus access token.
The code uses the library we have generated to get an OAuth2 access token. With the o_auth2_access_token_api.OAuth2AccessTokenApi()
we are able to call the oauth2_token()
method.
The body of the request is being created by passing the grant_type
, client_id
and client_secret
to the oauth2_token()
method. If you want to know more about how to get the access token check the authorization guide.
Call the Flight Offers Search API
Now let's call the Flight Offers Search API. Since thr OpenAPI Generator works with OAS3 you will have to convert the flight search specification to version 3 using the swagger editor (https://editor.swagger.io/){:target="_blank"}. To do the convertion, navigate to the top menu and select Edit
then Convert to OAS 3
.
The process is the same as above. You need to generate the library:
and then install it in your environment:
Then create a file flights.py
and add the following code:
The above code uses the generated library to to search for flight offers. It creates an instance of the shopping_api.ShoppingApi
class and setting the default headers to include the access token.
Then it is calling the get_flight_offers()
method to make the API request.