Inspiration

MHL GHW API WEEK

What it does

Learn to connect API

How we built it

import requests

Replace YOUR_API_KEY with your actual API key

api_key = "YOUR_API_KEY"

def suggest_food(preferences): # Build the API request URL url = "https://www.themealdb.com/api/json/v1/1/filter.php?" for key, value in preferences.items(): url += f"{key}={value}&" url += f"api_key={api_key}"

# Make the API request and parse the response
response = requests.get(url)
data = response.json()
dishes = [d["strMeal"] for d in data["meals"]]

# Return the list of recommended dishes
return dishes

def get_recipe(dish): # Build the API request URL url = f"https://www.themealdb.com/api/json/v1/1/search.php?s={dish}&api_key={api_key}"

# Make the API request and parse the response
response = requests.get(url)
data = response.json()
recipe = data["meals"][0]

# Extract the ingredients and instructions
ingredients = []
for i in range(1, 21):
    if recipe[f"strIngredient{i}"] is not None:
        ingredient = recipe[f"strIngredient{i}"]
        measure = recipe[f"strMeasure{i}"]
        ingredients.append(f"{measure} {ingredient}")
instructions = recipe["strInstructions"]

# Return the recipe as a dictionary
return {"name": dish, "ingredients": ingredients, "instructions": instructions}

Built With

Share this project:

Updates