Skip to content

Instantly share code, notes, and snippets.

@mattsilv
Last active November 10, 2023 06:59
Show Gist options
  • Save mattsilv/9dfb709e7609537ffd3b1b8c097e9bfb to your computer and use it in GitHub Desktop.
Save mattsilv/9dfb709e7609537ffd3b1b8c097e9bfb to your computer and use it in GitHub Desktop.
Natural Language Request for Food Logging

Natural Language Request for Food Logging

This endpoint requires an app ID and app key from https://developer.nutritionix.com/admin/access_details

POST https://trackapi.nutritionix.com/v2/natural/nutrients

HEADERS Content-Type:application/json, x-app-id:NutritionixAppID, x-app-key:NutritionixAppKey

BODY:

{
 "query":"for breakfast i ate 2 eggs, bacon, and french toast",
 "timezone": "US/Eastern"
}

Notes:

  • The end user's timezone parameter is optional, but it should be provided to ensure accuracy in time derivations. For example, if the user says "for breakfast" it will assume 8AM in that user's time zone.
  • All consumed_at timetamps are reported back from the API in UTC
@rdhungerf0rd
Copy link

I get status 400 back when I run the following Swift code to do a query of an apple. I'd appreciate any help in debugging. I've put dummy characters in the app-id and app-key fields. I'm sure those fields aren't causing the problem because I can do a upc search just fine. Thanks.

    let headers: [String: String] = ["Content-Type": "application/json", "accept": "application/json", "x-app-id": "xxxxxxxxxx", "x-app-key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "x-remote-user-id": "0"]
    
    let request = NSMutableURLRequest(url: NSURL(string: "https://trackapi.nutritionix.com/v2/natural/nutrients?query=apple")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
    
    request.httpMethod = "POST"
    request.allHTTPHeaderFields = headers
    
    let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { [self] data, response, error in
        guard let data = data, error == nil else {
        }

@xjohnfit
Copy link

xjohnfit commented Jun 5, 2023

const options = {
            method: 'POST',
            url: 'https://trackapi.nutritionix.com/v2/natural/nutrients',
            headers: {
                'content-type': 'application/json',
                'accept': 'application/json',
                'x-app-id': 'MYID',
                'x-app-key': 'MYKEY'
            },
            body: {
                "query": "banana",
                "timezone": "US/Eastern"
            }
        };

Im trying this code above in react, but i get a 400 Bad Request Error down below

{message: "child "query" fails because ["query" is required]",…}
id
: 
"9e69e3bb-a6c0-4b3c-964f-faeff72929e8"
message
: 
"child \"query\" fails because [\"query\" is required]"

@siddharth201983
Copy link

const options = {
            method: 'POST',
            url: 'https://trackapi.nutritionix.com/v2/natural/nutrients',
            headers: {
                'content-type': 'application/json',
                'accept': 'application/json',
                'x-app-id': 'MYID',
                'x-app-key': 'MYKEY'
            },
            body: {
                "query": "banana",
                "timezone": "US/Eastern"
            }
        };

Im trying this code above in react, but i get a 400 Bad Request Error down below

{message: "child "query" fails because ["query" is required]",…}
id
: 
"9e69e3bb-a6c0-4b3c-964f-faeff72929e8"
message
: 
"child \"query\" fails because [\"query\" is required]"

Use data instead of body

@MakersMarked
Copy link

MakersMarked commented Nov 10, 2023

I'm convinced Nutrients API is broken. all other endpoints work for me but this one just keeps giving err after err.

async function food () {
 const res =  fetch('https://trackapi.nutritionix.com/v2/natural/nutrients',{
  method: 'post',
  headers:{
    'x-app-id': process.env.NUTRITIONIX_ID,
    'x-app-key': process.env.NUTRITIONIX_KEY,
    'Content-Type':'application/json',
    'accept': 'application/json',
    'x-remote-user-id': '0'
  },
  body:  {
    "query": "turkey and mashed potatoes this morning"
  }
});
try {
const data = await res
 console.log(data)  
}
 catch(error){
  console.log(error)
 }
}

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