Lookup Cocktails or Ingredients Using IDs

Learn how we can use the database IDs to look up cocktails or ingredients.

TheCocktailDB API provides us with an endpoint with which we can search for the cocktails or ingredients using the database IDs. The base URI for this endpoint is https://www.thecocktaildb.com/api/json/v1/1/lookup.php.

Note: We can obtain these IDs using the endpoints discussed in the previous lesson.

Query parameter

We can use any one of the following query parameters with this endpoint:

Parameters

Type

Category

Description

i

integer

optional

This parameter is used to search for a cocktail using its database ID.

iid

integer

optional

This parameter is used to search for an ingredient using its database ID.

We need to use at least one of these parameters with this endpoint. Calling it without any query parameters will return an error.

Lookup a cocktail

The query parameter i, combined with the base URI of this endpoint, can be used to look up a cocktail using its ID.

The code below demonstrates how we can get the cocktail recipe using the cocktail ID.

Press + to interact
import json
import requests
url=('https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=11007')
#11007 is the database ID for margarita
response = requests.request("GET", url).json()
print(json.dumps(response, indent=4))

We’ll get the following information in response:

  • Instructions to create the cocktail
  • Link to an image of the cocktail
  • Image attribution
  • Category of the cocktail
  • Ingredients required
  • Amounts of ingredients
  • Glass type
  • Tags
  • Drink ID
  • Drink specification (Alcoholic/Non-Alcoholic)
  • Name of the drink
  • Last modified date of the recipe
  • License (Yes/No)
  • Video

Note: Value of some of the response objects might be null depending on the availability of the data.

We can replace the ID 11007 in line 4 with the ID of any other cocktail to get its details.

Lookup an ingredient

To look up an ingredient, we’ll use the query parameter iid with this endpoint. The code below shows how we can use this endpoint.

Press + to interact
import json
import requests
url=('https://www.thecocktaildb.com/api/json/v1/1/lookup.php?iid=552')
#552 is the database ID for cordial
response = requests.request("GET", url).json()
print(json.dumps(response, indent=4))

We’ll get to know if the ingredient is alcoholic or not, the type of the ingredient, its name, its description, its abbreviation, and its ID by calling this endpoint.

Note: Value of some of the response objects might be null depending on the availability of the data.

We can get the information about other ingredients by replacing 552 in line 4 with the ID of the other ingredients.