Skip to content

Instantly share code, notes, and snippets.

@mattsilv
Last active February 27, 2024 21:39
Show Gist options
  • Save mattsilv/d99cd145cc2d44d71fa5d15dd4829e03 to your computer and use it in GitHub Desktop.
Save mattsilv/d99cd145cc2d44d71fa5d15dd4829e03 to your computer and use it in GitHub Desktop.
Nutritionix API v2: Natural Exercise Endpoint Sample
@anniexxx9999
Copy link

thanks a lot

@PavlosPo
Copy link

Thank you for that, this was really helpful!

@mcfurland
Copy link

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

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

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

@AtcoCoder
Copy link

Can't thank you enough

@raduxapostolescu
Copy link

raduxapostolescu commented Mar 2, 2023

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

olalyska commented Mar 2, 2023

@raduxapostolescu try removing the "Content_Type" from nutritionix _headers

@raduxapostolescu
Copy link

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

ispeedyg commented Apr 3, 2023

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

@Priyanexodus
Copy link

Priyanexodus commented May 4, 2023

`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

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

@MichaelOgunjimi
Copy link

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

rajdama commented Jun 11, 2023

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

@raduxapostolescu
Copy link

@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

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

parthCJ commented Nov 17, 2023

Screensho01
im getting this error! please help

@trade2003
Copy link

trade2003 commented Nov 27, 2023

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

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

@val-afk
Copy link

val-afk commented Feb 7, 2024

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

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