In the modern world, working with data is everything. The accessibility of data opens many doors for developers of various applications/services. Some services provide an interface to check whether the movie is good before going to the cinema. Others focus on business tasks like collecting, analyzing, processing and reusing data elsewhere. As demand for various data rises, so does the demand for APIs.
What is an API?
API stands for Application Programming Interface. It’s a set of endpoints that allows you to use a service without worrying about how exactly the service works internally. To successfully use an API endpoint, you need to know three things:
- The address of an endpoint
- What data you need to send
- What data you will receive from an endpoint
There are different types of API concepts, but in this tutorial, we’ll be using a RESTful API.
What is so special about the REST API?
REST stands for representational state transfer. It’s a standard that ensures that if you give API correct input data, it will return the desired results. Sounds pretty simple, right?
However, a RESTful system has the following constraints:
- Client-server architecture – The user interface is completely separate from the service. The primary advantage of this approach is that the interface for a new platform (Android, Web, iOS) can be developed without much interference with the server.
- Statelessness – Each request contains all the information needed to perform the request. Session data is saved only on the client-side, not the server. This constraint reduces storage usage and improves security.
- Cacheability – As was already mentioned, the client-side keeps parts of request data. This makes things simpler for the user, as they do not need to re-enter or remember data. It also improves productivity and scalability.
- Layered system – Each layer only knows about neighboring layers. So it does not matter for the client whether they are communicating directly with the server or if there is a proxy and a security layer.
- Uniform interface – Endpoints must be unique in the requests. The requests have enough data to describe how to process the message. The REST client must be able to detect all the hyperlinks, which represent endpoints just from having base URI.
What is the Booking.com API?
We can divide the Booking API interface into three parts:
- Endpoints – Here we can get all available endpoints addresses as well as request type (GET, POST, etc.) and search for endpoints. Depending on which endpoint we choose, the content of the other two interface sections will change to represent the endpoint.
- Requests – On the top, we can see an endpoint description as well as choose a RapidAPI account. To the right, there is the button to get the results of the queried request. Please note that you have to subscribe to a billing plan to use this feature.
The lower section is for input parameters, split into three groups:- Header parameters.
- Required (body) parameters.
- Optional (body) parameters.
- Response – In this section, there are two tabs:
- Test result – In this tab, we can see a sample response as well as a result of your request. In this example, we requested a static map, and we can see it at the bottom of the response (in the body part).
- Code snippet – Here, we can choose a programming language and see how to perform a request with the chosen parameters in this language.
- Test result – In this tab, we can see a sample response as well as a result of your request. In this example, we requested a static map, and we can see it at the bottom of the response (in the body part).
Now let’s look at a concrete example.
How to use the Booking.com API
How to get access to the Booking.com API
- Sign up for a RapidAPI account – sign up using GitHub, Facebook, Google, or locally.
On the next screen, you need to enter your name and your organization’s names. Consider making an organization account if you are planning to use RapidAPI with your team in the future. You can read more about this here. - Subscribe to the Booking.com API – There are a few pricing options based on your usage. Hint: There’s a free plan that allows for 500 requests/month.
- Create your RapidAPI App – To use the endpoints we will need a RapidAPI Key, which will be provided within your user dashboard.
You can find your Application Key under ‘Security’:
Booking.com API Example
Let’s say we need to create a service that returns data about the hotels in the area. It’s worth noting that Booking API provides only GET endpoints. So, we can only collect data about hotels and then redirect the user to the desired hotel page. To collect data about hotels in the area, we can use ‘properties/list-by-map’ endpoint. Let us look at the required parameters:
In this example, we are looking for a one-room apartment for one guest. The check-in date is on the 15th of December, and check-out is on the 17th of December. Coordinates bounding box corresponds to the Philippine city Manila.
Now, let’s review the optional parameters:
Most of the parameters here are self-explanatory, except `search_id` and `categories_filter`.
`search_id` is for reusing what someone did earlier. We will not need it.
`categories_filter` is filtered by how many stars (from 0 to 5) you want your hotel to have. In our case, we are looking for 1-3 star hotels.
The result of this request is as follows:
There is a lot of data, but we are interested primarily in `result` which is the list of individual hotel data. Here is one of the hotel’s data:
"review_score":8.1 "location_score_reviews_nr":2052 "deals":{...}3 items "facilities_review_score":{...}3 items "timezone":"Asia/Manila" "ufi":-2437894 "type":"property_card" "cleanliness_score":8.4 "main_photo_url":"http://r-ec.bstatic.com/xdata/images/hotel/square60/153677765.jpg?k=a807f158d950292deedb9859f71bea409b78c0356ae96a8ca62075e3e5461a09&o=" "cc_required":1 "is_no_prepayment_block":0 "mobile_discount_percentage":0 "soldout":0 "longitude":120.983826437126 "checkout":{...}2 items "block_ids":[...]1 item "genius_discount_percentage":0 "districts":"3374,3376" "class":3 "is_city_center":0 "cleanliness_score_reviews_nr":2059 "hotel_id":1933702 "business_review_nr":337 "cc1":"ph" "class_is_estimated":1 "main_photo_id":153677765 "location_score_word":"Very Good" "city":"Manila" "extended":0 "business_review_score_word":"Very Good" "address":"1850 M.H. Del Pilar Street, Malate" "zip":"1004" "urgency_room_c":0 "is_genius_deal":0 "accommodation_type":204 "countrycode":"ph" "country_trans":"Philippines" "id":"property_card_1933702" "business_review_score":8 "review_nr":2088 "hotel_name_trans":"Hop Inn Hotel Ermita Manila" "distance":"6.84" "default_language":"xu" "latitude":14.570172412886 "cleanliness_score_word":"Very Good" "location_score":8.1 "preferred":1 "hotel_facilities":"305,179,25,423,161,204,108,424,158,107,96,419,47,425,160,91,422,420,184,145,60,2,109,8,110,48,421,163,16,418,46,219" "available_rooms":10 "district_id":3374 "accommodation_type_name":"Hotel" "booking_home":{...}4 items "hotel_name":"Hop Inn Hotel Ermita Manila" "review_recommendation":"" "city_in_trans":"in Manila" "hotel_include_breakfast":0 "selected_review_topic":NULL "price_breakdown":{...}7 items "address_trans":"1850 M.H. Del Pilar Street, Malate" "is_free_cancellable":0 "district":"Manila Bay" "currency_code":"PHP" "min_total_price":3542.4 "in_best_district":0 "url":"https://www.booking.com/hotel/ph/hop-inn-ermita-manila.html" "bwallet":{...}1 item "price_is_final":1 "review_score_word":"Very Good" "checkin":{...}2 items "distance_to_cc":"3.00" "city_name_en":"Manila" "is_smart_deal":0 "is_mobile_deal":0 "currencycode":"PHP" "breakfast_review_score":{...}3 items "city_trans":"Manila"
We have a lot of useful data to use in our hotel service already, but some customers would like to have a look at the reviews. And there is an endpoint just for that – `properties/get-featured-reviews`. All we need to do is insert ‘hotel_id’ from the previous request.
Furthermore, that’s not all we can do with this API! We can get a list of facilities from the `properties/get-facilities`, a map from `properties/get-static-map`, and more.
We will get the following result:
Booking.com API Python Implementation
With the API key and endpoints figured out, we can implement a simple program to output hotel reviews to the user.
Related: How to use an API with Python
import requests list_by_map_url = "https://apidojo-booking-v1.p.rapidapi.com/properties/list-by-map" list_by_map_querystring = { "search_id":"none", "children_age":"", "price_filter_currencycode":"USD", "languagecode":"en-us", "travel_purpose":"leisure", "categories_filter":"class%3A%3A1%2Cclass%3A%3A2%2Cclass%3A%3A3", "children_qty":"", "order_by":"popularity", "guest_qty":"1", "room_qty":"1", "departure_date":"2019-12-19", "bbox":"14.291283%2C14.948423%2C120.755688%2C121.136864", "arrival_date":"2019-12-17" } list_by_map_headers = { 'x-rapidapi-host': "apidojo-booking-v1.p.rapidapi.com", 'x-rapidapi-key': "54d78e5c98mshd2b3a25b4adf763p1674f2jsnb7535d954daf" } list_by_map_response = requests.request("GET", list_by_map_url, headers=list_by_map_headers, params=list_by_map_querystring).json() for list_result in list_by_map_response['result']: hotel_id = list_result['hotel_id'] reviews_url = "https://apidojo-booking-v1.p.rapidapi.com/properties/get-featured-reviews" reviews_querystring = { "languagecode":"en-us", "hotel_id":hotel_id } reviews_headers = { 'x-rapidapi-host': "apidojo-booking-v1.p.rapidapi.com", 'x-rapidapi-key': "7bc001f75amsh6d42156d20adcadp15c7aejsn695341e0f83f" } reviews_response = requests.request("GET", reviews_url, headers=reviews_headers, params=reviews_querystring).json() print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-') print(list_result['hotel_name']) for review in reviews_response['vpm_featured_reviews']: if(review['title'] is not None): print(review['title'].encode('utf-8')) if(review['pros'] is not None): print(review['pros'].encode('utf-8')) print('~~~~~~')
The result is a list of reviews for all the hotels in the area, which we can process separately.
Here’s how it looks for one hotel:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- RedDoorz Plus @ Kamuning Quezon City b'Establishment exceeded my expectations. Clean and New.' b'The establishment is clean, no water pressure issues, aircon working well. Safe area. Friendly and helpful staff. A nice cafe on the lobby.' ~~~~~~ b'Cool Place... ' b'The unit was GREAT!nStaffs were courteous.nThe furnitures was awesome...nAffordable ' ~~~~~~ b'Awesome! ' b'The unit was awesomenThe receptionist was very accommodatingnThe price was very affordablenLocation was great' ~~~~~~ b'Overall the hotel is very nice and comfortable to stay with.' b'Super big room. With ref, TV, good bed, WIFI. Really a value for your money.' ~~~~~~ b'Awesome!!! ' b'The unit was GREAT!nThe design and furnitures are awesome!nThe value is AFFORDABLE!nThe staffs were cool! ' ~~~~~~ b'Just be careful when you make your reservations if payments are before or on arrival.' b'The place was very good for the money, it was clean and comfortable.' ~~~~~~
Conclusion
The Booking API provides a convenient way for services to get information about hotels and similar places. In this tutorial, we learned how to use the RapidAPI interface to get started. We also illustrated what we need to do to use an API from RapidAPI. Then we broke down a practical example of Booking API usage: how to get data for the booking service. Finally, we implemented this example in Python.
Abbas says
hello
If i apply for rapidapi for booking.com from you will i be eligible for commissions from booking.com website?