Amadeus Python SDK

repository·master·Indexed 42 days ago

https://github.com/amadeus4dev/amadeus-python

A programmatic interface for Amadeus travel industry APIs, enabling developers to access flight searches, hotel offers, location data, and travel analytics. The SDK features the `amadeus.Client` for API interactions, comprehensive error handling via `amadeus.ResponseError`, and specialized modules for shopping, booking, and reference data. It supports Python 3.8+ and provides built-in utilities for paginated responses and environment-based configuration.

Tokens
3.8K
Snippets
11
Records
16
Agent score
4%

What's inside Amadeus

  1. Handle API Responses and Data

    master

    Every API call returns a Response object. Use the following attributes to access the data:

    • .body: The raw response as a string.
    • .result: The response body parsed as JSON (if applicable).
    • .data: The specific data payload (e.g., a list of locations) extracted from the JSON if a data key is present.
    from amadeus import Client, Location
    
    amadeus = Client(client_id='KEY', client_secret='SECRET')
    response = amadeus.reference_data.locations.get(keyword='LON', subType=Location.ANY)
    
    print(response.body)   # Raw string
    print(response.result) # Parsed JSON
    print(response.data)   # Extracted data payload
  2. Search for hotels and book stays

    master

    The SDK supports multiple versions of hotel booking and various search methods:

    • Search: Find hotels by ID (hotel_offers_search.get), by city code (reference_data.locations.hotels.by_city.get), or by geocode (reference_data.locations.hotels.by_geocode.get).
    • Autocomplete: Use reference_data.locations.hotel.get(keyword=...) to find hotels by name.
    • Booking v2: Use amadeus.booking.hotel_orders.post() with guests, travel_agent, room_associations, and payment objects.
    • Booking v1: Use amadeus.booking.hotel_bookings.post(offerId, guests, payments) for simpler booking flows.
    # Get list of available offers by hotel ids
    amadeus.shopping.hotel_offers_search.get(hotelIds='RTPAR001', adults='2')
    
    # Hotel Booking v2
    amadeus.booking.hotel_orders.post(
        guests=guests, 
        travel_agent=travel_agent,
        room_associations=room_associations,
        payment=payment
    )
  3. Use travel prediction and analytics APIs

    master

    The SDK provides access to machine learning-driven predictions and historical analytics:

    • Flight Delay Prediction: Predict delays using amadeus.travel.predictions.flight_delay.get() by providing flight details like carrierCode, flightNumber, and departureTime.
    • Trip Purpose Prediction: Predict why a user is traveling using amadeus.travel.predictions.trip_purpose.get().
    • Airport On-Time Performance: Check airport reliability via amadeus.airport.predictions.on_time.get().
    • Air Traffic Analytics: Analyze booked/traveled destinations using amadeus.travel.analytics.air_traffic.booked.get() or traveled.get().
    # Flight Delay Prediction
    amadeus.travel.predictions.flight_delay.get(
        originLocationCode='NCE', 
        destinationLocationCode='IST', 
        departureDate='2022-08-01', 
        departureTime='18:20:00', 
        arrivalDate='2022-08-01', 
        arrivalTime='22:15:00', 
        aircraftCode='321', 
        carrierCode='TK', 
        flightNumber='1816', 
        duration='PT31H10M'
    )
  4. Manage flight orders and bookings

    master

    You can create and manage flight orders using the amadeus.booking.flight_orders and amadeus.shopping modules.

    1. Search for offers: Use flight_offers_search.get() to find available flights.
    2. Price the offers: Use flight_offers.pricing.post() to get the final price for specific flight objects.
    3. Create an order: Use flight_orders.post(flights, traveler) to book the flight.
    4. Manage orders: Retrieve or delete an order using its ID via amadeus.booking.flight_order(order_id).
    # 1. Search for flights
    flights = amadeus.shopping.flight_offers_search.get(originLocationCode='SYD', destinationLocationCode='BKK', departureDate='2022-11-01', adults=1).data
    
    # 2. Price the first flight offer
    amadeus.shopping.flight_offers.pricing.post(flights[0])
    
    # 3. Create a flight order
    flight_booking = amadeus.booking.flight_orders.post(flights[0], traveler).data
    
    # 4. Retrieve the order by ID
    amadeus.booking.flight_order(flight_booking['id']).get()
    
    # 5. Delete the order
    amadeus.booking.flight_order(flight_booking['id']).delete()
  5. Configure Logging and Debugging

    master

    To debug API interactions, you can provide a custom Python logging.Logger instance to the Client or set the log_level parameter. You can also control verbosity using the AMADEUS_LOG_LEVEL environment variable.

    import logging
    from amadeus import Client
    
    # Option 1: Pass a custom logger
    logger = logging.getLogger('my_app')
    logger.setLevel(logging.DEBUG)
    
    amadeus = Client(
        client_id='KEY', 
        client_secret='SECRET', 
        logger=logger
    )
    
    # Option 2: Set log level via parameter
    amadeus = Client(
        client_id='KEY', 
        client_secret='SECRET', 
        log_level='debug'
    )
  6. Initialize the Amadeus Client

    master

    You can initialize the Client in three ways:

    1. Explicitly with credentials: Pass client_id and client_secret directly to the constructor.
    2. Using environment variables: If AMADEUS_CLIENT_ID and AMADEUS_CLIENT_SECRET are set in your environment, you can call Client() without arguments.
    3. Switching environments: By default, the SDK uses the test environment. To use the production (pay-as-you-go) environment, set the hostname parameter to 'production'.

    You can also pass a custom logger or set a log_level (e.g., 'debug') during initialization.

    from amadeus import Client
    
    # 1. Initialize with parameters
    amadeus = Client(client_id='YOUR_API_KEY', client_secret='YOUR_API_SECRET')
    
    # 2. Initialize via environment variables (AMADEUS_CLIENT_ID and AMADEUS_CLIENT_SECRET)
    amadeus = Client()
    
    # 3. Initialize for production
    amadeus = Client(client_id='YOUR_API_KEY', client_secret='YOUR_API_SECRET', hostname='production')
    
    # With custom logging
    import logging
    logger = logging.getLogger('my_logger')
    amadeus = Client(client_id='KEY', client_secret='SECRET', logger=logger, log_level='debug')
  7. Manage flight bookings using Booking/FlightOrders

    master

    The amadeus.booking module allows you to manage flight orders:

    • amadeus.booking.FlightOrders.post(): Create a new flight order.
    • amadeus.booking.FlightOrder.get(): Retrieve details of an existing flight order.
    • amadeus.booking.FlightOrder.delete(): Cancel/delete a flight order.
  8. Access Reference Data for locations and airlines

    master

    The amadeus.reference_data module provides access to static or semi-static data:

    • amadeus.reference_data.Location.get(): Get location information.
    • amadeus.reference_data.Locations.get(): Get multiple locations.
    • amadeus.reference_data.locations.Airports.get(): Get airport data.
    • amadeus.reference_data.Airlines.get(): Get airline data.
    • amadeus.reference_data.locations.Cities.get(): Get city data.
  9. Navigate Paginated Results

    master

    If an endpoint supports pagination, you can navigate through pages using the Response object with the following methods:

    • amadeus.next(response): Returns a new response for the next page.
    • amadeus.previous(response): Returns a new response for the previous page.
    • amadeus.first(response): Returns the first page.
    • amadeus.last(response): Returns the last page.

    If no page is available (e.g., calling .next() on the last page), the method returns None.

    response = amadeus.reference_data.locations.get(keyword='LON', subType=Location.ANY)
    
    # Get the next page
    next_page_response = amadeus.next(response)
    if next_page_response:
        print(next_page_response.data)
  10. Search for flights using Shopping/Flights

    master

    The amadeus.shopping module provides several classes for flight-related searches:

    • amadeus.shopping.FlightDestinations.get(): Find flight destinations.
    • amadeus.shopping.FlightDates.get(): Find flight dates.
    • amadeus.shopping.FlightOffersSearch.get(): Search for flight offers using GET.
    • amadeus.shopping.FlightOffersSearch.post(): Search for flight offers using POST.
  11. Use the amadeus.Client for API interactions

    master

    The amadeus.Client is the primary entry point for interacting with the Amadeus API. It provides methods for making HTTP requests and navigating paginated responses.

    Key methods:

    • get(...): Performs a GET request.
    • post(...): Performs a POST request.
    • request(...): Low-level request method.
    • previous(), next(), first(), last(): Used for navigating through paginated result sets.
    import amadeus
    
    client = amadeus.Client(
        client_id='YOUR_API_KEY',
        client_secret='YOUR_API_SECRET'
    )