Checking the Dalgona Coffee Nutrition
- books
I was in this hype coffee trend back then during the social distancing period. I love the fluffy coffee on top of any beverage I like!
Registering to the API
# I'm using EDAMAM nutritional analysis API
# You can sign-up for the Developer (Free) edition here:
# https://developer.edamam.com/edamam-nutrition-api
# API documentation:
# https://developer.edamam.com/edamam-docs-nutrition-api
Initial Setup
# Loading the packages
import requests
import json
# Store the ID and key in variables
app_ID = "<insert_app_id_here>"
app_key = "<insert_app_key_here>""
# Replace the ID and key with your own ones that you recieved upon registration
# Setting up the request URL
api_endpoint = "https://api.edamam.com/api/nutrition-details"
url = api_endpoint + "?app_id=" + app_ID + "&app_key=" + app_key
Sending a POST request
# Defining the header (as stated in the documentation)
headers = {
'Content-Type' : 'application/json'
}
# Defining the payload of the request
recipe = {
'title' : 'Dalgona Coffee',
'ingr' : ['14g ground espresso', '14g sugar', '150ml milk']
}
# Submitting a POST request
r = requests.post(url, headers = headers, json = recipe)
r.status_code
200
Inspecting the response
# The response is JSON based on the documentation
dalgona_info = r.json()
print(json.dumps(dalgona_info, indent=4))
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_e02e29b583294b64a2db7784c3083837",
"yield": 2.0,
"calories": 149,
"totalWeight": 182.69915386093172,
"dietLabels": [
"BALANCED"
],
"healthLabels": [
"VEGETARIAN",
"PEANUT_FREE",
"TREE_NUT_FREE",
"ALCOHOL_FREE"
],
"cautions": [
"SULFITES"
],
"totalNutrients": {
"ENERC_KCAL": {
"label": "Energy",
"quantity": 149.80648385516835,
"unit": "kcal"
},
"FAT": {
"label": "Fat",
"quantity": 5.052922500480281,
"unit": "g"
},
"FASAT": {
"label": "Saturated",
"quantity": 2.8980192195063763,
"unit": "g"
},
"FATRN": {
"label": "Trans",
"quantity": 0.0,
"unit": "g"
},
"FAMS": {
"label": "Monounsaturated",
"quantity": 1.2561571293507656,
"unit": "g"
},
"FAPU": {
"label": "Polyunsaturated",
"quantity": 0.31454335002881684,
"unit": "g"
},
"CHOCDF": {
"label": "Carbs",
"quantity": 21.656559385324723,
"unit": "g"
},
"FIBTG": {
"label": "Fiber",
"quantity": 0.0,
"unit": "g"
},
"SUGAR": {
"label": "Sugars",
"quantity": 21.784307269977052,
"unit": "g"
},
"SUGAR.added": {
"label": "Sugars, added",
"quantity": 13.972000000000001,
"unit": "g"
},
"PROCNT": {
"label": "Protein",
"quantity": 4.889823346619349,
"unit": "g"
},
"CHOLE": {
"label": "Cholesterol",
"quantity": 15.469915386093172,
"unit": "mg"
},
"NA": {
"label": "Sodium",
"quantity": 68.62063616020063,
"unit": "mg"
},
"CA": {
"label": "Calcium",
"quantity": 175.23004386285282,
"unit": "mg"
},
"MG": {
"label": "Magnesium",
"quantity": 26.669915386093173,
"unit": "mg"
},
"K": {
"label": "Potassium",
"quantity": 220.58288309642987,
"unit": "mg"
},
"FE": {
"label": "Iron",
"quantity": 0.07160974615827952,
"unit": "mg"
},
"ZN": {
"label": "Zinc",
"quantity": 0.5807868692854473,
"unit": "mg"
},
"P": {
"label": "Phosphorus",
"quantity": 130.92728924318263,
"unit": "mg"
},
"VITA_RAE": {
"label": "Vitamin A",
"quantity": 71.1616107760286,
"unit": "\u00b5g"
},
"VITC": {
"label": "Vitamin C",
"quantity": 0.028000000000000004,
"unit": "mg"
},
"THIA": {
"label": "Thiamin (B1)",
"quantity": 0.07130161077602859,
"unit": "mg"
},
"RIBF": {
"label": "Riboflavin (B2)",
"quantity": 0.2888815700249746,
"unit": "mg"
},
"NIA": {
"label": "Niacin (B3)",
"quantity": 0.8666622469362293,
"unit": "mg"
},
"VITB6A": {
"label": "Vitamin B6",
"quantity": 0.05597169538993542,
"unit": "mg"
},
"FOLDFE": {
"label": "Folate equivalent (total)",
"quantity": 7.874957693046586,
"unit": "\u00b5g"
},
"FOLFD": {
"label": "Folate (food)",
"quantity": 7.874957693046586,
"unit": "\u00b5g"
},
"FOLAC": {
"label": "Folic acid",
"quantity": 0.0,
"unit": "\u00b5g"
},
"VITB12": {
"label": "Vitamin B12",
"quantity": 0.6961461923741927,
"unit": "\u00b5g"
},
"VITD": {
"label": "Vitamin D",
"quantity": 78.89656846907518,
"unit": "IU"
},
"TOCPHA": {
"label": "Vitamin E",
"quantity": 0.1096894077026522,
"unit": "mg"
},
"VITK1": {
"label": "Vitamin K",
"quantity": 0.47809746158279515,
"unit": "\u00b5g"
},
"WATER": {
"label": "Water",
"quantity": 150.03116429763912,
"unit": "g"
}
},
"totalDaily": {
"ENERC_KCAL": {
"label": "Energy",
"quantity": 7.490324192758417,
"unit": "%"
},
"FAT": {
"label": "Fat",
"quantity": 7.773726923815817,
"unit": "%"
},
"FASAT": {
"label": "Saturated",
"quantity": 14.49009609753188,
"unit": "%"
},
"CHOCDF": {
"label": "Carbs",
"quantity": 7.218853128441574,
"unit": "%"
},
"FIBTG": {
"label": "Fiber",
"quantity": 0.0,
"unit": "%"
},
"PROCNT": {
"label": "Protein",
"quantity": 9.779646693238698,
"unit": "%"
},
"CHOLE": {
"label": "Cholesterol",
"quantity": 5.156638462031057,
"unit": "%"
},
"NA": {
"label": "Sodium",
"quantity": 2.859193173341693,
"unit": "%"
},
"CA": {
"label": "Calcium",
"quantity": 17.523004386285283,
"unit": "%"
},
"MG": {
"label": "Magnesium",
"quantity": 6.349979853831708,
"unit": "%"
},
"K": {
"label": "Potassium",
"quantity": 4.693252831838933,
"unit": "%"
},
"FE": {
"label": "Iron",
"quantity": 0.39783192310155285,
"unit": "%"
},
"ZN": {
"label": "Zinc",
"quantity": 5.279880629867703,
"unit": "%"
},
"P": {
"label": "Phosphorus",
"quantity": 18.703898463311802,
"unit": "%"
},
"VITA_RAE": {
"label": "Vitamin A",
"quantity": 7.906845641780954,
"unit": "%"
},
"VITC": {
"label": "Vitamin C",
"quantity": 0.031111111111111114,
"unit": "%"
},
"THIA": {
"label": "Thiamin (B1)",
"quantity": 5.941800898002382,
"unit": "%"
},
"RIBF": {
"label": "Riboflavin (B2)",
"quantity": 22.221659232690353,
"unit": "%"
},
"NIA": {
"label": "Niacin (B3)",
"quantity": 5.416639043351433,
"unit": "%"
},
"VITB6A": {
"label": "Vitamin B6",
"quantity": 4.305515029995032,
"unit": "%"
},
"FOLDFE": {
"label": "Folate equivalent (total)",
"quantity": 1.9687394232616464,
"unit": "%"
},
"VITB12": {
"label": "Vitamin B12",
"quantity": 29.0060913489247,
"unit": "%"
},
"VITD": {
"label": "Vitamin D",
"quantity": 525.9771231271678,
"unit": "%"
},
"TOCPHA": {
"label": "Vitamin E",
"quantity": 0.7312627180176814,
"unit": "%"
},
"VITK1": {
"label": "Vitamin K",
"quantity": 0.39841455131899595,
"unit": "%"
}
},
"totalNutrientsKCal": {
"ENERC_KCAL": {
"label": "Energy",
"quantity": 150,
"unit": "kcal"
},
"PROCNT_KCAL": {
"label": "Calories from protein",
"quantity": 19,
"unit": "kcal"
},
"FAT_KCAL": {
"label": "Calories from fat",
"quantity": 45,
"unit": "kcal"
},
"CHOCDF_KCAL": {
"label": "Calories from carbohydrates",
"quantity": 86,
"unit": "kcal"
}
}
}
# Let's see what kind of info they have about this recipe
dalgona_info.keys()
dict_keys(['uri', 'yield', 'calories', 'totalWeight', 'dietLabels', 'healthLabels', 'cautions', 'totalNutrients', 'totalDaily', 'totalNutrientsKCal'])
# Let's check the 'totalNutrients' key
print(json.dumps(dalgona_info["totalNutrients"], indent=4))
{
"ENERC_KCAL": {
"label": "Energy",
"quantity": 149.80648385516835,
"unit": "kcal"
},
"FAT": {
"label": "Fat",
"quantity": 5.052922500480281,
"unit": "g"
},
"FASAT": {
"label": "Saturated",
"quantity": 2.8980192195063763,
"unit": "g"
},
"FATRN": {
"label": "Trans",
"quantity": 0.0,
"unit": "g"
},
"FAMS": {
"label": "Monounsaturated",
"quantity": 1.2561571293507656,
"unit": "g"
},
"FAPU": {
"label": "Polyunsaturated",
"quantity": 0.31454335002881684,
"unit": "g"
},
"CHOCDF": {
"label": "Carbs",
"quantity": 21.656559385324723,
"unit": "g"
},
"FIBTG": {
"label": "Fiber",
"quantity": 0.0,
"unit": "g"
},
"SUGAR": {
"label": "Sugars",
"quantity": 21.784307269977052,
"unit": "g"
},
"SUGAR.added": {
"label": "Sugars, added",
"quantity": 13.972000000000001,
"unit": "g"
},
"PROCNT": {
"label": "Protein",
"quantity": 4.889823346619349,
"unit": "g"
},
"CHOLE": {
"label": "Cholesterol",
"quantity": 15.469915386093172,
"unit": "mg"
},
"NA": {
"label": "Sodium",
"quantity": 68.62063616020063,
"unit": "mg"
},
"CA": {
"label": "Calcium",
"quantity": 175.23004386285282,
"unit": "mg"
},
"MG": {
"label": "Magnesium",
"quantity": 26.669915386093173,
"unit": "mg"
},
"K": {
"label": "Potassium",
"quantity": 220.58288309642987,
"unit": "mg"
},
"FE": {
"label": "Iron",
"quantity": 0.07160974615827952,
"unit": "mg"
},
"ZN": {
"label": "Zinc",
"quantity": 0.5807868692854473,
"unit": "mg"
},
"P": {
"label": "Phosphorus",
"quantity": 130.92728924318263,
"unit": "mg"
},
"VITA_RAE": {
"label": "Vitamin A",
"quantity": 71.1616107760286,
"unit": "\u00b5g"
},
"VITC": {
"label": "Vitamin C",
"quantity": 0.028000000000000004,
"unit": "mg"
},
"THIA": {
"label": "Thiamin (B1)",
"quantity": 0.07130161077602859,
"unit": "mg"
},
"RIBF": {
"label": "Riboflavin (B2)",
"quantity": 0.2888815700249746,
"unit": "mg"
},
"NIA": {
"label": "Niacin (B3)",
"quantity": 0.8666622469362293,
"unit": "mg"
},
"VITB6A": {
"label": "Vitamin B6",
"quantity": 0.05597169538993542,
"unit": "mg"
},
"FOLDFE": {
"label": "Folate equivalent (total)",
"quantity": 7.874957693046586,
"unit": "\u00b5g"
},
"FOLFD": {
"label": "Folate (food)",
"quantity": 7.874957693046586,
"unit": "\u00b5g"
},
"FOLAC": {
"label": "Folic acid",
"quantity": 0.0,
"unit": "\u00b5g"
},
"VITB12": {
"label": "Vitamin B12",
"quantity": 0.6961461923741927,
"unit": "\u00b5g"
},
"VITD": {
"label": "Vitamin D",
"quantity": 78.89656846907518,
"unit": "IU"
},
"TOCPHA": {
"label": "Vitamin E",
"quantity": 0.1096894077026522,
"unit": "mg"
},
"VITK1": {
"label": "Vitamin K",
"quantity": 0.47809746158279515,
"unit": "\u00b5g"
},
"WATER": {
"label": "Water",
"quantity": 150.03116429763912,
"unit": "g"
}
}
# Taking the sugar content
dalgona_info["totalNutrients"]["SUGAR"]
{'label': 'Sugars', 'quantity': 21.784307269977052, 'unit': 'g'}
# Ohh look at that sugar amount!
dalgona_info["totalNutrients"]["SUGAR"]["quantity"]
21.784307269977052
# Let's put this into a table for better viewing
import pandas as pd
pd.DataFrame(dalgona_info["totalNutrients"])
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
# Table should look better in vertical position
dalgona_nutrients = pd.DataFrame(dalgona_info["totalNutrients"]).transpose()
dalgona_nutrients
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
# Exporting the nutrition values to a CSV file
dalgona_nutrients.to_csv("Dalgona_nutrients.csv")
Testing invalid input
# Sending a request not containing the 'ingridients' parameter
cake = requests.post(url, headers=headers, json = {"title": "cheesecake"})
cake.ok
False
cake.status_code
555
cake.json()
{'error': 'low_quality'}