Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created October 21, 2020 10:34
Show Gist options
  • Save angelabauer/dd71d7072626afd728e1730584c6e4b8 to your computer and use it in GitHub Desktop.
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)
@jenny-lou
Copy link

I don't get what problems you're all are having. I created a brand new account and it just works fine. The service is really not down. Here are my newly created app id and key and they work just fine.

NUTRITIONIX_APP_ID = "9fa3007f"
NUTRITIONIX_APP_KEY = "c00ce89c396f38d2c0ce44add9849537"

Use'm if you want. I can't promise they'll work for very long but go ahead and if they no longer work at least they're here as an example of what they should look like. It's from an account with a fake email address anyway. So far all the keys I've generated were the same amount of characters (32) so maybe that's something worth checking out?
And here's my entire code, just in case. removed all the sheety stuff as that's irrelevant for this problem.

import requests

NUTRITIONIX_ENDPOINT = "https://trackapi.nutritionix.com/v2/natural/exercise"
NUTRITIONIX_APP_ID = "9fa3007f"
NUTRITIONIX_APP_KEY = "c00ce89c396f38d2c0ce44add9849537"

nutritionix_headers = {
    "x-app-id": NUTRITIONIX_APP_ID,
    "x-app-key": NUTRITIONIX_APP_KEY,
    "Content-Type": "application/json",
}

nutritionix_data = {
    "query": input("What did you do? :"),
    "gender": "male",
    "weight_kg": 81,
    "height_cm": 178,
    "age": 43,
}

response = requests.post(NUTRITIONIX_ENDPOINT, headers=nutritionix_headers, json=nutritionix_data)
response.raise_for_status()
print(response.json())

Well, I wondered what I'm doing wrong 😐

import requests
from datetime import datetime

GENDER = "male"
WEIGHT_KG = 75       # Imaginary
HEIGHT_CM = 204     # Imaginary
AGE = 23       # Imaginary

apiKey = "32a33435d031ad8a40f56656ee05e3ac"
appID = "0be6e594"
EndPoint = "https://trackapi.nutritionix.com/v2/natural/nutrients"
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": GENDER,
    "weight_kg": WEIGHT_KG,
    "height_cm": HEIGHT_CM,
    "age": AGE
}

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

Output:

Tell us which exercise you did: Ran for 30 minutes
{'message': '"gender" is not allowed. "weight_kg" is not allowed. "height_cm" is not allowed. "age" is not allowed', 'id': 'bdec56be-ea1f-47ef-9172-60d610c172f7'}

Process finished with exit code 0

your params are not string
it should be:
GENDER = "Female"
WEIGHT_KG = "53"
HEIGHT_CM = "153"
AGE = "25"

@amirovsunnat
Copy link

Assalamu alaykum everyone ! I found the answer. Here how you can get 200 response code and no errors.

import requests

APP_ID = "YOUR_APP_ID"
API_KEY = "YOUR_API_KEY"
API_ENDPOINT = "https://trackapi.nutritionix.com/v2/natural/exercise"
headers = {
"x-app-id": APP_ID,
"x-app-key": API_KEY
}
GENDER = "male"
WEIGHT_KG = 60
HEIGHT_CM = 173
AGE = 19

sentence = input("Tell me which exercise you did? ")
parameters = {
"query": sentence,
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height_cm": HEIGHT_CM,
"age": AGE
}

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

Screenshot 2023-12-18 055121

@koreanssam
Copy link

Assalamu alaykum everyone ! I found the answer. Here how you can get 200 response code and no errors.

import requests

APP_ID = "YOUR_APP_ID" API_KEY = "YOUR_API_KEY" API_ENDPOINT = "https://trackapi.nutritionix.com/v2/natural/exercise" headers = { "x-app-id": APP_ID, "x-app-key": API_KEY } GENDER = "male" WEIGHT_KG = 60 HEIGHT_CM = 173 AGE = 19

sentence = input("Tell me which exercise you did? ") parameters = { "query": sentence, "gender": GENDER, "weight_kg": WEIGHT_KG, "height_cm": HEIGHT_CM, "age": AGE }

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

Screenshot 2023-12-18 055121

Assalamu alaykum! Thanks bro. 감사합니다. 형!

@amirovsunnat
Copy link

Thank you too 😊!

@xzenor
Copy link

xzenor commented Jan 15, 2024

@swati666 2 very tiny things are wrong in your code.

  1. The weird — behind your api key is what's causing the UnicodeEncodeError. It's not suposed to be there. Your api key ends with the last 3. Nothing more.

  2. it's exercise, not excercise (the c behind the x). So the endpoint you try to talk with doesn't exist.
    Use https://trackapi.nutritionix.com/v2/natural/exercise (without that extra c)

So, small bug, big error. Took me while to find it as well.

@swati666
Copy link

@swati666 2 very tiny things are wrong in your code.

  1. The weird — behind your api key is what's causing the UnicodeEncodeError. It's not suposed to be there. Your api key ends with the last 3. Nothing more.
  2. it's exercise, not excercise (the c behind the x). So the endpoint you try to talk with doesn't exist.
    Use https://trackapi.nutritionix.com/v2/natural/exercise (without that extra c)

So, small bug, big error. Took me while to find it as well.

First of all... thanks a lot for reviewing my code. I thought I wouldn't get a solution that's why deleted it. and you were absolutely right, the problem was with the API key. "-" being the last character of the API key was causing problems.

@xzenor
Copy link

xzenor commented Jan 17, 2024

@swati666

I thought I wouldn't get a solution that's why deleted it.

Yeah a comment on a github gist might not reach a lot of people.
In the course resources of lecture 3 is a link to the Discord server. You'll have a bigger chance of getting an answer there than on GitHub and it's a nice place to talk with other students. The Q&A on Udemy can be very helpful as well.

@swati666
Copy link

@swati666

I thought I wouldn't get a solution that's why deleted it.

Yeah a comment on a github gist might not reach a lot of people. In the course resources of lecture 3 is a link to the Discord server. You'll have a bigger chance of getting an answer there than on GitHub and it's a nice place to talk with other students. The Q&A on Udemy can be very helpful as well.

yes Sir... I agree with your suggestion. I'll soon become active there as well. Thanks. Have a great day.

@thakriti
Copy link

thakriti commented Feb 5, 2024

I am getting the error : "Weight_kg" is not allowed . How it will resolve?

import requests

APP_ID = "0c13387f"
API_KEY = "9be3072c0d2e4981d2ed88d7cfdf0c36"

exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
exercise_text = input("Tell me which exercise you did:")

GENDER = "male"
WEIGHT_KG = 56
HEIGHT_CM = 146
AGE = 24

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(url=exercise_endpoint,json=parameters,headers=headers)
result = response.json()
print(result)

@xzenor
Copy link

xzenor commented Feb 7, 2024

"Weight_kg":WEIGHT_KG

@thakriti try it with a lower case w

So "weight_kg":WEIGHT_KG instead of "Weight_kg":WEIGHT_KG

@TeddyToodles
Copy link

Using copy and paste, I have got this working, however I'm not learning anything doing this. Where in the documents are the API docs to highlight required parameters etc...

@shryge
Copy link

shryge commented Mar 22, 2024

Using copy and paste, I have got this working, however I'm not learning anything doing this. Where in the documents are the API docs to highlight required parameters etc...

Searched the documentation for the term 'Natural Language for Exercise' and took me to the relevant page: https://docx.syndigo.com/developers/docs/natural-language-for-exercise
In the Header section it explains that parameters required there are: “x-app-id” and “x-app-key”
In the Parameters section, you need to click on 'Expand to see the parameters and description'. Turns out only the required parameter is 'query', weight, height and age aren't required so I didn't bother including them when first testing out the API.

@quazador
Copy link

quazador commented Mar 25, 2024

thx for the valid docs, it is much easier knowing the real requirements and not do any revers engineering from the code of Angela. Unfortunately the doc link of the webpage is not working.

@karuna-karan
Copy link

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

I have the same issue, fix it by map the parameters with json, like json: parameters, but not params:parameters(which is used in requests.get method.

i have facing ht same issue please resolve the issue.

@kriswen
Copy link

kriswen commented Apr 4, 2024

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

I have the same issue, fix it by map the parameters with json, like json: parameters, but not params:parameters(which is used in requests.get method.

i have facing ht same issue please resolve the issue.

for requests.post() method you have to use json as the parameter
see here: https://www.w3schools.com/python/ref_requests_post.asp

@darrnih
Copy link

darrnih commented Apr 8, 2024

Doesn't work for me, i keep getting {"message":"invalid app id/key","id":"39386a26-a250-446b-b58b-c2d38fd78932"} or a variation of the same invalid app id/key message

@kriswen
Copy link

kriswen commented Apr 8, 2024

share your code

@stevenson007
Copy link

Everything was perfect. No errors for me.

@Dabro1203
Copy link

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
Copy link

john-sc commented May 29, 2024

"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.

@AbiollaghJames
Copy link

AbiollaghJames commented Jun 12, 2024

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

@Ash-Rich38
Copy link

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.

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