Python

Getting Started with the LiteAPI SDK for Python

Installation

To install the LiteAPI Node.js SDK, you can use pip. Run the following command in your project directory:

pip install liteapi-sdk

Basic Usage

Here's a step-by-step guide to get you started with using the LiteAPI Python SDK in your application.

1. Import the SDK

First, import the LiteAPI SDK into your project and initialize with your API key:

from liteapi import LiteApi

api = LiteApi("your_api_key")

Note: you can obtain your API key from the LiteAPI Dashboard.


2. Make your first request

Now you can use the client to make requests to the LiteAPI. Here's an example of how to fetch a list of available flights:

from liteapi import LiteApi

api = LiteApi("your_api_key")
response = api.get_full_rates(data={...})
print(response)

The readme of the SDK in our GitHub repository lists all available methods in the SDK.


End-to-end booking workflow

Here is an example demonstrating the end-to-end booking workflow using the Python SDK:

# Initialize the LiteApi SDK
api = LiteApi('your_api_key')

# Step 1: Fetch a list of hotels in New York, US
hotels_response = api.get_hotels(country_code="US", city_name="New York", limit=200)
hotels = hotels_response["data"]

if not hotels:
    raise Exception("No hotels found")

# Step 2: Fetch rates for the found hotels
checkin_date = (datetime.now() + timedelta(days=180)).strftime('%Y-%m-%d')
checkout_date = (datetime.now() + timedelta(days=181)).strftime('%Y-%m-%d')

rates_response = api.get_full_rates({
    "hotelIds": [hotel['id'] for hotel in hotels],
    "checkin": checkin_date,
    "checkout": checkout_date,
    "occupancies": [{"adults": 2}],
    "currency": "USD",
    "guestNationality": "US",
    "timeout": 10
})

rates = rates_response["data"]
if not rates:
    raise Exception("No rates found")

offer_id = rates[0]["roomTypes"][0]["offerId"]
print(f"Found offer: {offer_id}")

# Step 3: Make a prebooking request
prebook_response = api.prebook({"offerId": offer_id, "usePaymentSdk": False})
prebook_id = prebook_response["data"]["prebookId"]
print(f"Prebooked with prebookId: {prebook_id}")

# Step 4: Make a booking request
booking_data = {
    "holder": {
        "firstName": "John",
        "lastName": "Doe",
        "email": "[email protected]"
    },
    "payment": {
        "method": "ACC_CREDIT_CARD"
    },
    "prebookId": prebook_id,
    "guests": [
        {
            "occupancyNumber": 1,
            "remarks": "quiet room please",
            "firstName": "John",
            "lastName": "Doe",
            "email": "[email protected]"
        }
    ]
}

booking_response = api.book(booking_data)
if booking_response["data"]["status"] != "CONFIRMED":
    raise Exception("Booking failed")

# Step 5: Signal success
print("Booking completed")

Here is one more example which first looks for hotels in Manhattan:

# Get the place ID for Manhattan
response = api.get_places("manhattan")
print(response)

place_id = response["data"][0]["placeId"]

And then makes a rates search request for all hotels in the area:

from datetime import datetime, timedelta

checkin_date = (datetime.now() + timedelta(days=180)).strftime('%Y-%m-%d')
checkout_date = (datetime.now() + timedelta(days=181)).strftime('%Y-%m-%d')

rates_response = api.get_full_rates({
    "placeId": place_id,
    "checkin": checkin_date,
    "checkout": checkout_date,
    "occupancies": [{"adults": 2}],
    "currency": "USD",
    "guestNationality": "US",
    "timeout": 5
})

rates = rates_response["data"]
if not rates:
    raise Exception("No rates found")

offer_id = rates[0]["roomTypes"][0]["offerId"]
print(f"Found offer: {offer_id}")

Build a fully-fledged web application to book hotels

We have created an open-source example web app using the LiteAPI Node.js SDK, available on GitHub. This example demonstrates the complete process of building a hotel booking website, including checkout, payment, and booking confirmation. Explore the source code here: https://github.com/liteapi-travel/build-website-example


Next steps

For more details and advanced usage, please refer to the GitHub documentation for the SDK.

If you encounter any issues or have any questions, feel free to open an issue on the GitHub repository.

Happy coding!