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