Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created October 21, 2020 10:34
Show Gist options
  • Select an option

  • Save angelabauer/dd71d7072626afd728e1730584c6e4b8 to your computer and use it in GitHub Desktop.

Select an option

Save angelabauer/dd71d7072626afd728e1730584c6e4b8 to your computer and use it in GitHub Desktop.
import requests
GENDER = YOUR GENDER
WEIGHT_KG = YOUR WEIGHT
HEIGHT_CM = YOUR HEIGHT
AGE = YOUR AGE
APP_ID = YOUR APP ID
API_KEY = YOUR API KEY
exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
exercise_text = input("Tell me which exercises you did: ")
headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY,
}
parameters = {
"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE
}
response = requests.post(exercise_endpoint, json=parameters, headers=headers)
result = response.json()
print(result)
@stevenson007

Copy link
Copy Markdown

Everything was perfect. No errors for me.

@Dabro1203

Copy link
Copy Markdown

I got mine working eventually - persistence and resilience :)

Here is my code below. I have put a few pointers in brackets in my code to highlight the areas were I was getting problems.

Hope this is of some help :)

import requests

exercise = input("Please tell us what exercise you have done?")
APP_ID = "xxxxxxxxx" (Make sure your APP_ID is submitted as a string)
APP_KEY = "xxxxxxxxxxxxxxxxxxxxxxxx " (Make sure the APP_KEY is a string and a carbon copy of what appears in your account, just take off the final -, but leave the dead space at the end)
API_ENDPOINT = "https://trackapi.nutritionix.com/v2/natural/exercise"

headers = {
"x-app-id": APP_ID,
"x-app-key": APP_KEY
}

parameters = {
"query": exercise,
"gender": "male", (Please note the gender is a string but the weight, height and age are all integer values)
"weight_kg": 56,
"height_cm": 120,
"age": 35
}

response = requests.post(API_ENDPOINT, headers= headers, json= parameters)
print(response)
result = response.json()
print(result)

@john-sc

john-sc commented May 29, 2024

Copy link
Copy Markdown

"Weight Kg not allowed"
Check your endpoint.
It should be v2/natural/exercise
You have v2/natural/nutrients and for that endpoint Weight Kg is not a valid parameter.

@Lord-Abiolla

Lord-Abiolla commented Jun 12, 2024

Copy link
Copy Markdown

That documentation is so hard to understand, couldn't find query parameters for some reason.

@Hubist-tech

Copy link
Copy Markdown

That documentation is so hard to understand, couldn't find query parameters for some reason.

It's there it's just tucked under and expand menu which was not the easiest to find.

@Harsha0130

Copy link
Copy Markdown

Tell me Which exercise you did: ran 3miles and walked 2km. {'message': 'child "query" fails because ["query" is required]', 'id': '95593e38-789e-4cee-8627-15d9193afdac'} This message i received anytime i ran this code , can any one explain it to me

Can you share your cade, So that i can help you to solve the problem

@Simonmmr

Simonmmr commented Jul 16, 2024

Copy link
Copy Markdown

Hi I cant find the documentation that takes weight height and age for the parameters there is just query in the parameters list

Yeah I could not find other parameters too, plus I'm first time facing an api doc like this.

check please the API endpoint, first I also found it as an error and later my API endpoint is different from the one we should use https://trackapi.nutritionix.com/v2/natural/exercise

@MasterYodaPresents

Copy link
Copy Markdown

I don't work in cm/kg...I work in lbs and in...I converted our first exercise and bmi calculator to lbs and in...sorry...kg and cm are useless to me....can you please tell me how to do that here....Please and thank you!

@MasterYodaPresents

Copy link
Copy Markdown

I think I found the answer....thank you

@Vigilante120

Copy link
Copy Markdown

import requests

NUTRITION_API_KEY = "API KEY"
NUTRITION_NATURAL_ENDPOINT = "https://trackapi.nutritionix.com/v2/natural/exercise"
APP_ID = "APP ID"

headers = {
'Content-Type': 'application/json',
"x-app-id": f"{APP_ID}",
"x-app-key": f"{NUTRITION_API_KEY}"
}
parameters = {
"query": input("What Exercise You Just Did: ")
}

nutrition = requests.post(url=NUTRITION_NATURAL_ENDPOINT, json=parameters, headers=headers)
print(nutrition.json())

OUTPUT:
WhatExerciseYouJustDid: Cricket{
'exercises': [
{
'tag_id': 215,
'user_input': 'cricket',
'duration_min': 30,
'met': 4.8,
'nf_calories': 168,
'photo': {
'highres': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/215_highres.jpg',
'thumb': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/215_thumb.jpg',
'is_user_uploaded': False
},
'compendium_code': 15150,
'name': 'cricket',
'description': None,
'benefits': None
}
]
}

@Shlok1613

Copy link
Copy Markdown

`import requests

APP_ID = "7bceec16"
APP_KEY = "844902aaedb6f200acca45f3e3364288"
GENDER = "Male"
AGE = "20"
WEIGHT_KG = "65"
HEIGHT_CM = "175"

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"

exercise_text = input("Tell me which exercise did you do? ")
headers = {
"x-app-id": APP_ID,
"x-app-key": APP_KEY,
"Content-Type": "application/json"
}
parameters = {
"query": exercise_text,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE
}

response = requests.get(url=exercise_endpoint, json=parameters, headers=headers)
response.raise_for_status()

tried this code a lot made many changes and yet getting this output

OUTPUT
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://trackapi.nutritionix.com/v2/natural/exercise`

@ares77717

Copy link
Copy Markdown

`import requests

APP_ID = "7bceec16" APP_KEY = "844902aaedb6f200acca45f3e3364288" GENDER = "Male" AGE = "20" WEIGHT_KG = "65" HEIGHT_CM = "175"

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"

exercise_text = input("Tell me which exercise did you do? ") headers = { "x-app-id": APP_ID, "x-app-key": APP_KEY, "Content-Type": "application/json" } parameters = { "query": exercise_text, "gender": GENDER, "weight_kg": WEIGHT_KG, "height_cm": HEIGHT_CM, "age": AGE }

response = requests.get(url=exercise_endpoint, json=parameters, headers=headers) response.raise_for_status()

tried this code a lot made many changes and yet getting this output

OUTPUT raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://trackapi.nutritionix.com/v2/natural/exercise`

use GENDER = 'male'
AGE = 20 int not str
weight_kg = 65 same int not str
height_cm = 175
then save response in new variable data or results
data = response.json()
print(data)

@Shlok1613

Copy link
Copy Markdown

import requests APP_ID = "7bceec16" APP_KEY = "844902aaedb6f200acca45f3e3364288" GENDER = "Male" AGE = "20" WEIGHT_KG = "65" HEIGHT_CM = "175" exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise" exercise_text = input("Tell me which exercise did you do? ") headers = { "x-app-id": APP_ID, "x-app-key": APP_KEY, "Content-Type": "application/json" } parameters = { "query": exercise_text, "gender": GENDER, "weight_kg": WEIGHT_KG, "height_cm": HEIGHT_CM, "age": AGE } response = requests.get(url=exercise_endpoint, json=parameters, headers=headers) response.raise_for_status() tried this code a lot made many changes and yet getting this output **OUTPUT** raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: [https://trackapi.nutritionix.com/v2/natural/exercise](https://trackapi.nutritionix.com/v2/natural/exercise%60)

use GENDER = 'male' AGE = 20 int not str weight_kg = 65 same int not str height_cm = 175 then save response in new variable data or results data = response.json() print(data)

still the same no changes

@jenachang

Copy link
Copy Markdown

Does anyone has no issue with the environment variable?
Mine all worked fine until I exported some variables to env.
So sad. What's wrong?

@Ken-huea

Copy link
Copy Markdown

Here is the my solution. Im happy if it would be your help.
#########
import requests

apiKey = "0fa18b8d857b750ddf5ec3031da5c3a6x"
appID = "416ea149xq"
EndPoint = "https://trackapi.nutritionix.com/v2/natural/exercisex"
exercise_text = input("Tell us which exercise you did: ")

headers = {
"x-app-id": appID,
"x-app-key": apiKey,
"Content-Type": "application/json",
}
parameters = {
"query": exercise_text,
"gender": "male",
"weight_kg": 58,
"height_cm": 172,
"age": 28
}

response = requests.post(url=EndPoint, headers=headers, json=parameters)
result = response.json()
print(result)
#########
Tell us which exercise you did: running

{'exercises': [{'tag_id': 317, 'user_input': 'running', 'duration_min': 30, 'met': 9.8, 'nf_calories': 284.2, 'photo': {'highres': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/317_highres.jpg', 'thumb': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/317_thumb.jpg', 'is_user_uploaded': False}, 'compendium_code': 12050, 'name': 'running', 'description': None, 'benefits': None}]}

#########

@raketa25

raketa25 commented Sep 6, 2024

Copy link
Copy Markdown

can anybody provide me api id and api key, i am not able to login and get the data

import requests

App_ID = "my_id"
API_Key = "my_api_key"

Define the endpoint URL

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"

Set up the headers with authentication information

headers = {
"x-app-id": App_ID,
"x-app-key": API_Key,
"Content-Type": "application/json"
}

Define the query for the exercise data

exercise_text = input("What exercise have you done?\n")

Prepare the JSON body for the POST request

data = {
"query": exercise_text,
"gender": "male", # Replace with "male" or "female" as appropriate
"weight_kg": 70, # Replace with the user's weight in kg
"height_cm": 172, # Replace with the user's height in cm
"age": 30 # Replace with the user's age
}

Make the POST request to the API

response = requests.post(url=exercise_endpoint, headers=headers, json=data)

exercise_stats = response.json()

print(exercise_stats)

Print the response in JSON format

if response.status_code == 200:
exercise_stats = response.json()
print(exercise_stats)
else:
print("Failed to fetch data:", response.status_code, response.text)

@bobscala

Copy link
Copy Markdown

can anybody provide me api id and api key, i am not able to login and get the data

You need to create an account on https://www.nutritionix.com/business/api to get your own app id and api key.

@eyobmdev

Copy link
Copy Markdown

I lave the way you guys writing, my code perfect and it works but i don't love it when i see your code i change my mind and change my code, what would do if you guys ware me

@eyobmdev

Copy link
Copy Markdown

I love the way you guys writing, my code perfect and it works but i don't love it when i see your code i change my mind and change my code, what would do if you guys ware me

@bobscala

Copy link
Copy Markdown

I love the way you guys writing, my code perfect and it works but i don't love it when i see your code i change my mind and change my code, what would do if you guys ware me

Keep writing and modifying your code. As Angela always says, there are different ways to code a solution. If you see something better, use it and learn for your future projects. We all are here to help one another.

@raketa25

raketa25 commented Feb 17, 2025 via email

Copy link
Copy Markdown

@SamarAwadallah

Copy link
Copy Markdown

import requests

APP_ID = "####"
API_KEY = "####"
WEIGHT_KG = 70.1
HEIGHT_CM = 163
AGE = 33
Nutrition_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
parameters = {
"query" : input("what exercise you did today?"),
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE
}

headers = {
'x-app-id': APP_ID,
'x-app-key': API_KEY
}

response = requests.post(url = Nutrition_endpoint,json=parameters,headers=headers)
print(response.text)

@FAISAL1578

Copy link
Copy Markdown

'exercises': [{'tag_id': 316, 'user_input': 'joging', 'duration_min': 20, 'met': 9, 'nf_calories': 309, 'photo': {'highres': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/316_highres.jpg', 'thumb': 'https://d2xdmhkmkbyw75.cloudfront.net/exercise/316_thumb.jpg', 'is_user_uploaded': False}, 'compendium_code': 12040, 'name': 'jogging', 'description': None, 'benefits': None}]}

Receiving this output

@macdonald864

Copy link
Copy Markdown

What am i doing wrong?
{'message': 'unauthorized', 'id': '43dcea1d-fd64-4ad3-831d-deba6c0a011f'}

Here's my code:
import requests

APP_ID = "1e1b0c1e"
API_KEY = "2fa07a2c16006d37e9ad76872599f15f"

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"

user_input = input("Tell me what exercises you did: ")

header = {
"x-app-id": APP_ID,
"x-app-key": API_KEY,
"x-remote-user-id": "0"
}

parameter = {
"query": user_input,
"weight_kg": 85,
"height_cm": 170,
"age": 23
}

response = requests.post(url= exercise_endpoint, headers= header, json= parameter)
result = response.json()
print(result)

@ahmed804096

Copy link
Copy Markdown

My code is like angelat but it does not work
I only get
{'message': 'unauthorized', 'id': '*******************************************'}
What is wrong?
the app is pending and waiting approval. is it the reason?. did it happen with one of you guys?

@mayropirata

Copy link
Copy Markdown

Free public trials for the Nutritionix API are no longer available
thats what is happening to anyone that is trying to figure out

@dheerajdhami2001-cyber

Copy link
Copy Markdown

Free public trials for the Nutritionix API are no longer available thats what is happening to anyone that is trying to figure out

ya same with me

@xzenor

xzenor commented Nov 1, 2025

Copy link
Copy Markdown

Yes of course it's the same with you. They just stopped providing it for free for everyone.

You could try with https://api-ninjas.com/api/caloriesburned
This isn't the same and it might be hard to do at this stage of the course but at least it's some way to be able to finish this day..

If you're also on the Discord server, check here for an example: https://discord.com/channels/743099584242516037/1433042220960972851/1433774742531215443

I suppose the AppBrewery will adjust the course eventually but for now, it's either skip the day, use an alternative, or pay for the api (which is overkill for just 1 exercise). Nutritionix is no longer free.

@Buchi001

Copy link
Copy Markdown

@xzenor I am seriously finding it confusing continuing the course with https://api-ninjas.com/api/caloriesburned .

Like you said, it's hard for me. I'd appreciate some help from you. For now, I guess I'd skip the day. I hope it doesn't hunt me down in subsequent days!

@ForrestF1re

Copy link
Copy Markdown

Hey y'all. Just wanted to drop some insight now that it's on the https://app.100daysofpython.dev/ endpoint.

An oddity I found (not sure why) is that you need to copy and paste the API Key and App ID from outside the text block when your setting up the API on the sample site. It seems to copy some trailing whitespaces that seem to be the key for this to work, whereas if you copy it out of the textbox it doesn't seem to work.

As of 30 Nov 2025, I'm able to successfully complete this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment