Skip to content

Instantly share code, notes, and snippets.

@mattsilv
Last active February 27, 2024 21:39
Show Gist options
  • Select an option

  • Save mattsilv/d99cd145cc2d44d71fa5d15dd4829e03 to your computer and use it in GitHub Desktop.

Select an option

Save mattsilv/d99cd145cc2d44d71fa5d15dd4829e03 to your computer and use it in GitHub Desktop.
Nutritionix API v2: Natural Exercise Endpoint Sample
@sayanti-mondal

Copy link
Copy Markdown

getting this error : UnicodeEncodeError: 'latin-1' codec can't encode character '\u2014' in position 33: ordinal not in range(256)

@joebuggy561

Copy link
Copy Markdown

Please can anyone help me out i l keep getting a 401 client error- attached below is my code;
"""
import requests

GENDER = "MALE"
WEIGHT_KG = "60"
HEIGHT = "7.5"
AGE = "50"

APP_ID = MY_ID
API_KEY = MY_KEY

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

exercise_input = input("Tell which exercise you did today?: ")

header = {
"x-app-id": "",
"x-remote-user-id": "",
}

parameters = {
"gender": GENDER,
"weight_kg": WEIGHT_KG,
"height": HEIGHT,
"age": AGE,
}

response = requests.post(url=exercise_endpoint, json=parameters, headers=header)
response.raise_for_status()
result = response.json()
print(result)"""

please I will really appreciate the advice

@egluna-dev

Copy link
Copy Markdown

Please can anyone help me out i l keep getting a 401 client error- attached below is my code; """ import requests

GENDER = "MALE" WEIGHT_KG = "60" HEIGHT = "7.5" AGE = "50"

APP_ID = MY_ID API_KEY = MY_KEY

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

exercise_input = input("Tell which exercise you did today?: ")

header = { "x-app-id": "", "x-remote-user-id": "", }

parameters = { "gender": GENDER, "weight_kg": WEIGHT_KG, "height": HEIGHT, "age": AGE, }

response = requests.post(url=exercise_endpoint, json=parameters, headers=header) response.raise_for_status() result = response.json() print(result)"""

please I will really appreciate the advice

@joebuggy561 In your parameters, instead of simply "height", it should be "height_cm". Additionally, the constants for WEIGHT_KG, HEIGHT, and AGE should be float types for weight and height and int for AGE.

@a45hif

a45hif commented Oct 7, 2022

Copy link
Copy Markdown

Please can anyone help me out i l keep getting a 401 client error- attached below is my code; """ import requests

GENDER = "MALE" WEIGHT_KG = "60" HEIGHT = "7.5" AGE = "50"

APP_ID = MY_ID API_KEY = MY_KEY

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

exercise_input = input("Tell which exercise you did today?: ")

header = { "x-app-id": "", "x-remote-user-id": "", }

parameters = { "gender": GENDER, "weight_kg": WEIGHT_KG, "height": HEIGHT, "age": AGE, }

response = requests.post(url=exercise_endpoint, json=parameters, headers=header) response.raise_for_status() result = response.json() print(result)"""

please I will really appreciate the advice

Please can anyone help me out i l keep getting a 401 client error- attached below is my code; """ import requests
GENDER = "MALE" WEIGHT_KG = "60" HEIGHT = "7.5" AGE = "50"
APP_ID = MY_ID API_KEY = MY_KEY
exercise_endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"
exercise_input = input("Tell which exercise you did today?: ")
header = { "x-app-id": "", "x-remote-user-id": "", }
parameters = { "gender": GENDER, "weight_kg": WEIGHT_KG, "height": HEIGHT, "age": AGE, }
response = requests.post(url=exercise_endpoint, json=parameters, headers=header) response.raise_for_status() result = response.json() print(result)"""
please I will really appreciate the advice

@joebuggy561 In your parameters, instead of simply "height", it should be "height_cm". Additionally, the constants for WEIGHT_KG, HEIGHT, and AGE should be float types for weight and height and int for AGE.

401 error comes when you don't have permission to that particular API endpoint.
In your code you didn't passed your API key. Use 'x-app-key' as key and give it API_KEY as value in your header.
Few more things to do. Add query key in your parameters dict and pass the exercise_input
Your parameters are also incorrect. I'm attaching the correct code below:

import requests

GENDER = "MALE"
WEIGHT_KG = "60"
HEIGHT = "160.5" #entered random height in cm
AGE = "50"

APP_ID = MY_ID
API_KEY = MY_KEY

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

exercise_input = input("Tell which exercise you did today?: ")

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

parameters = {
    'query': exercise_input,
    "gender": GENDER,
    "weight_kg": WEIGHT_KG,
    "height_cm": HEIGHT,
    "age": AGE,
}

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

@muztim

muztim commented Oct 9, 2022

Copy link
Copy Markdown

codes are okay but getting this even after generating a new API
{'message': 'unauthorized', 'id': '79e62a36-f08a-4cfb-af38-58294498767f'}

@a45hif

a45hif commented Oct 9, 2022

Copy link
Copy Markdown

codes are okay but getting this even after generating a new API {'message': 'unauthorized', 'id': '79e62a36-f08a-4cfb-af38-58294498767f'}

Make sure you're sending correct APP_ID and API_KEY in header and passing header in headers argument of requests.post() function!

@joebuggy561

Copy link
Copy Markdown

thanks guy i

codes are okay but getting this even after generating a new API {'message': 'unauthorized', 'id': '79e62a36-f08a-4cfb-af38-58294498767f'}

Make sure you're sending correct APP_ID and API_KEY in header and passing header in headers argument of requests.post() function!

thanks boss yeah i saw those mistakes later i really appreciate it the codes are working now

@SadSack963

SadSack963 commented Oct 29, 2022

Copy link
Copy Markdown

@sayanti-mondal

getting this error : UnicodeEncodeError: 'latin-1' codec can't encode character '\u2014' in position 33: ordinal not in range(256)

I know it's been a long time, but for others searching for a solution. This error is caused when requests is formulating the header. It is caused by the API key. The em-dash is NOT a part of your key:

nutritionix API Key

@tammy-blitz

Copy link
Copy Markdown

@sayanti-mondal

getting this error : UnicodeEncodeError: 'latin-1' codec can't encode character '\u2014' in position 33: ordinal not in range(256)

I know it's been a long time, but for others searching for a solution. This error is caused when requests is formulating the header. It is caused by the API key. The em-dash is NOT a part of your key:

nutritionix API Key

Thanks buddy...helped alot

@anniexxx9999

Copy link
Copy Markdown

thanks a lot

@PavlosPo

Copy link
Copy Markdown

Thank you for that, this was really helpful!

@mcfurland

Copy link
Copy Markdown

do we have an option for unspecified gender - thinking about those that might not conform to the two major genders? Can we send an "Unspeciifed" or "Other" in the POST request?

lol... This is strictly biological. Your gender identity doesnt impact calories burned during exercise. You should put your biological gender unless, for instance, you were born female AND are currently engaged in testosterone replacement therapy.

@Erlangsbp

Copy link
Copy Markdown

can anyone help me? i keep getting this error message
{"message":"child "query" fails because ["query" is required]","id":"d552f45c-f500-46ff-8c98-357abd32da38"}

here is my code

import requests as req

# get endpoint link

api_key = "my api key"
api_id = "my key id"
endpoint = "https://trackapi.nutritionix.com/v2/natural/exercise"

header = {
    "x-app-id": api_id,
    "x-app-key": api_key,
    "Content-Type": "json"
}

query = {
    'query': "ran 3 miles",
}

resp = req.post(url=endpoint, json=query, headers=header)
print(resp.text)

@SadSack963

Copy link
Copy Markdown

@Erlangsbp
"Content-Type": "application/json",

@AtcoCoder

Copy link
Copy Markdown

Can't thank you enough

@raduxapostolescu

raduxapostolescu commented Mar 2, 2023

Copy link
Copy Markdown

Solution below

Leaving this here so people can feel better about themselves.
When calling the .post() method I was giving params as an argument and not json. Fixed it in the code below to reflect the correct way.

I keep getting 400 for the following params:

{'message': 'child "query" fails because ["query" is required]', 'id': '09ef9905-8b97-4f45-a593-7faabb239c89'}

nutritionix_headers = {
    "x-app-id": APP_ID,
    "x-app-key": API_KEY,
    "Content-Type": "application/json",
    # "x-remote-user-id": "0"
}

exercise_params = {
    "query": "ran 2 kilometers",
    "gender": "male",
    "weight_kg": "77",
    "height_cm": "181",
    "age": "30",
}
nutritionix_exercise_ep = "https://trackapi.nutritionix.com/v2/natural/exercise"
requests.post(nutritionix_exercise_ep, headers=nutritionix_headers, json=exercise_params)

Tried giving the post() method a "query" argument as well but that's invalid. Clearly something is off with my syntax, just don't see how it's different than what was stated in the example.

@olalyska

olalyska commented Mar 2, 2023

Copy link
Copy Markdown

@raduxapostolescu try removing the "Content_Type" from nutritionix _headers

@raduxapostolescu

Copy link
Copy Markdown

@olalyska thanks for checking, Content-Type was recommended in this thread but it works with or without it. The issue was me calling the wrong args in POST.

@ispeedyg

ispeedyg commented Apr 3, 2023

Copy link
Copy Markdown

Thanks for your input. These have answered a lot of my problems especially with the x-app-key one.

@Priyanexodus

Priyanexodus commented May 4, 2023

Copy link
Copy Markdown

`import requests
import os

query = input("Enter your exercise data: ")
gender = "male"
age = 17
weight = 57
height = 172

app_id = os.environ.get('AppID')
api_key = os.environ.get('APIKey')
print(app_id)
print(api_key)

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

nutrition_headers = {
"x-app-id": app_id,
"x-app-key": api_key,
"Content-Type": "json"
}

nutrition_json = {
"query": query,
"gender": gender,
"age": age,
"weight_kg": weight,
}

response = requests.post(url=nutrition_endpoint,
json=nutrition_json,
headers=nutrition_headers)
print(response.status_code)
print(response.json())`

when i run this code i get status_code has 400 and {'message': 'child "query" fails because ["query" is required]', 'id': '39cc0948-0fe9-4fd8-8cb4-adb731dd2431'}

@SadSack963

Copy link
Copy Markdown

@Priyanexodus
"Content-Type": "application/json"

@MichaelOgunjimi

Copy link
Copy Markdown

Solution below

Leaving this here so people can feel better about themselves. When calling the .post() method I was giving params as an argument and not json. Fixed it in the code below to reflect the correct way.

I keep getting 400 for the following params:

{'message': 'child "query" fails because ["query" is required]', 'id': '09ef9905-8b97-4f45-a593-7faabb239c89'}

nutritionix_headers = {
    "x-app-id": APP_ID,
    "x-app-key": API_KEY,
    "Content-Type": "application/json",
    # "x-remote-user-id": "0"
}

exercise_params = {
    "query": "ran 2 kilometers",
    "gender": "male",
    "weight_kg": "77",
    "height_cm": "181",
    "age": "30",
}
nutritionix_exercise_ep = "https://trackapi.nutritionix.com/v2/natural/exercise"
requests.post(nutritionix_exercise_ep, headers=nutritionix_headers, json=exercise_params)

Tried giving the post() method a "query" argument as well but that's invalid. Clearly something is off with my syntax, just don't see how it's different than what was stated in the example.

this your correction for JSON not *PARAMS * really helped me have been debugging for hours now.
Thanks

@rajdama

rajdama commented Jun 11, 2023

Copy link
Copy Markdown

Can we get list of multiple exercises is their any endpoint for it?

@raduxapostolescu

Copy link
Copy Markdown

@rajdama it's using a language model so there won't be a defined list of exercises. The whole idea is to allow you to write as you were speaking to a human. It used correctly everything I threw at it however the time statement is iffy. If you say 1 hour and 30 min it will only take an hour. A way around that is to just say 90 minutes.

@TenkorangSamuelYaw

Copy link
Copy Markdown

Please can anyone help me out i l keep getting a 401 client error- attached below is my code; """ import requests

GENDER = "MALE" WEIGHT_KG = "60" HEIGHT = "7.5" AGE = "50"

APP_ID = MY_ID API_KEY = MY_KEY

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

exercise_input = input("Tell which exercise you did today?: ")

header = { "x-app-id": "", "x-remote-user-id": "", }

parameters = { "gender": GENDER, "weight_kg": WEIGHT_KG, "height": HEIGHT, "age": AGE, }

response = requests.post(url=exercise_endpoint, json=parameters, headers=header) response.raise_for_status() result = response.json() print(result)"""

please I will really appreciate the advice

In the header dictionary, where you have the x-app-id and the x-app-key keys, the corresponding values, APP_ID and API_KEY are missing.
You used empty double quotes instead, please check and replace with APP_ID and API_KEY

@parthCJ

parthCJ commented Nov 17, 2023

Copy link
Copy Markdown

Screensho01
im getting this error! please help

@trade2003

trade2003 commented Nov 27, 2023

Copy link
Copy Markdown

import requests

APP_ID = "438a9c40"
APP_KEY = "7215eb448ce325acba56b4ee224fe917"

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

GENDER = "male"
WEIGHT = 70.6
HEIGHT = 176.5
AGE = 20

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

exercise_json = {
"query": input("Tell me which exercise you did. "),
"gender": GENDER,
"weight_kg": WEIGHT,
"height_cm": HEIGHT,
"age": AGE,
}

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

output:
Tell me which exercise you did. Ran 5km
{'message': 'unauthorized', 'id': 'f54bca63-be81-4859-812d-00944f9fa4f3'}

Hihi, I am facing the same error. Please advice!

@Protesilai

Copy link
Copy Markdown

BE SURE to have x-app.. not x-api

@val-afk

val-afk commented Feb 7, 2024

Copy link
Copy Markdown

import requests

APP_ID = "438a9c40" APP_KEY = "7215eb448ce325acba56b4ee224fe917"

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

GENDER = "male" WEIGHT = 70.6 HEIGHT = 176.5 AGE = 20

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

exercise_json = { "query": input("Tell me which exercise you did. "), "gender": GENDER, "weight_kg": WEIGHT, "height_cm": HEIGHT, "age": AGE, }

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

output: Tell me which exercise you did. Ran 5km {'message': 'unauthorized', 'id': 'f54bca63-be81-4859-812d-00944f9fa4f3'}

Hihi, I am facing the same error. Please advice!

in your headers list where x-app_key is written, replace it with x-app-key. you have used an underscore instead of a minus sign. hope this helps because it worked for me

@BartBoxi

Copy link
Copy Markdown

Solution of how to get this done can be found here - https://gist.github.com/angelabauer/dd71d7072626afd728e1730584c6e4b8

Better to check this gits than wasting 2 hours to figure it out :)

Have fun coding!

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