TheCocktailDB
Food & DrinkUsing TheCocktailDB API with JavaScript
Are you a cocktail enthusiast? Do you want to create a cocktail-themed app or website? TheCocktailDB API is the perfect tool for you! With this API, you can access a vast database of cocktails, ingredients, and even random drinks.
Getting Started
To use TheCocktailDB API, you first need to sign up to get an API key. You can sign up at https://www.thecocktaildb.com/signup.php. Once you have an API key, you can start making API calls.
Example Code
Here are some example API calls you can make using JavaScript:
Search for a Cocktail by Name
This will search for a cocktail by name and return information about it.
const getDrinkByName = async (name) => {
const response = await fetch(`https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${name}`);
const data = await response.json();
return data.drinks;
}
getDrinkByName('margarita')
.then(data => console.log(data));
Lookup a Cocktail by ID
This will look up a cocktail by its ID and return information about it.
const getDrinkById = async (id) => {
const response = await fetch(`https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=${id}`);
const data = await response.json();
return data.drinks;
}
getDrinkById('11007')
.then(data => console.log(data));
Search for Ingredient by Name
This will search for an ingredient by name and return information about it.
const getIngredientByName = async (name) => {
const response = await fetch(`https://www.thecocktaildb.com/api/json/v1/1/search.php?i=${name}`);
const data = await response.json();
return data.ingredients;
}
getIngredientByName('lemon')
.then(data => console.log(data));
Random Cocktail
This will return information about a random cocktail.
const getRandomDrink = async () => {
const response = await fetch('https://www.thecocktaildb.com/api/json/v1/1/random.php');
const data = await response.json();
return data.drinks;
}
getRandomDrink()
.then(data => console.log(data));
Conclusion
With TheCocktailDB API, you can create all kinds of awesome cocktail-related apps and websites. These example API calls should get you started, but there are many more available on the API documentation page. So, get coding and let's make some excellent cocktail-related projects!