Skip to main content

REST API: Nutritional information đŸ§Ș

How to assign nutritional information to a product and calculate it for elaborations

Rest API: How nutritional information works

tspoonlab has nutritional information linked to ingredients/products and can calculate nutritional information for recipes and dishes.

For ingredients/products, this information must always be expressed per 100 g. For recipes and dishes, it can be expressed per 100 g, for the total weight, or per one unit of the recipe or dish. You can calculate a recipe or dish’s nutritional information on demand or assign it directly; the latter is recommended when the process involves actions (frying, oven) that alter the ingredients’ nutritional information. Energy expressed in kJ is calculated automatically from the kcal:

1 kcal = 4,184 kJ

The kcal can be assigned from the values on suppliers’ technical sheets, or calculated with the formula:

Kcal = fat * 9 + carbohydrates * 4 + proteins * 4 + fiber * 2

System login

To make calls to our APIs, the first thing you need is to authenticate through a login call. That call returns a token that you must then attach to every subsequent call.

This shell script shows how to authenticate using curl:

[email protected]
password=XXXXXXX

url=https://app.tspoonlab.com/recipes/api
authenticate='username='$username'&password='$password

echo -n 'rememberme:' > rememberme.txt
curl -v --data $authenticate $url/login >> rememberme.txt

On later calls, add the token received from the login call to your headers:

curl -X PUT -v -H "$(cat rememberme.txt)" $url/integration/llamada

Rest API: Select a cost center/restaurant

The requests described in the following sections refer to a cost center, which in most cases corresponds to a restaurant. To retrieve the list of cost centers and their identifiers, see the REST API: Cost centers article. The cost center identifier corresponds to the idOrderCenter field of the UserOrderCenter class. Once you have the identifier, add it to the headers so it is used on subsequent calls:

echo -n 'order:351583444167656299610202XXXXXXXXXXXX' >> rememberme.txt

So in your request headers you must specify both the token returned by login and the cost center identifier:

rememberme:aGVucnkudXBzYWxsLmRAZXXXXXXXXXXXXXXXXXX
order:351583444167656299610202XXXXXXXXXXXX

Rest API: Assign nutritional information to a product

You need the product identifier idComponent and make the following call:

PUT 
https://app.tspoonlab.com/recipes/api/ingredient/{idComponent}/nutritional
https://app.tspoonlab.com/recipes/api/recipe/{idComponent}/nutritional
https://app.tspoonlab.com/recipes/api/dish/{idComponent}/nutritional

Pass in the request body:

public class NewComponentNutritional {

private Double energy;
private Double fat;
private Double saturated;
private Double proteins;
private Double carbohydrates;
private Double sugar;
private Double salt;
private Double fiber;
private Boolean nutritionalPercent;
private Boolean nutritionalUnitari;
private String idComponentUnitNutritional;
}

nutritionalPercent is true if the values are per 100 g (always for base ingredients/products). nutritionalUnitari is true if you send values for the unit of the recipe or dish; in that case idComponentUnitNutritional indicates the id of the recipe or dish unit the information is sent for. If both (nutritionalPercent, nutritionalUnitari) are null or false, the values are for the whole recipe or dish.

Rest API: Calculate nutritional information

You need the idComponent of the recipe or dish and make the following call:

GET
https://app.tspoonlab.com/recipes/api/recipe/{idComponent}/nutritional
https://app.tspoonlab.com/recipes/api/dish/{idComponent}/nutritional

In the calculation you can indicate whether to do it per 100 g, for the total, or per one unit of quantity.

In the call, add these two parameters:

....?percent=false&unitari=false&idComponentUnit=null

percent=false and unitari=false calculates for the total. For 100 g: percent=true, unitari=false. For the unit: unitari=true — in this case, in idComponentUnit specify the id of the unit within the recipe or dish to calculate for. It returns the calculated value:

class NutritionalInfo {

Double energy;

Double carbohydrates;
Double sugar;

Double fat;
Double saturated;

Double proteins;
Double salt;
Double fiber;

boolean nutritionalPercent;
boolean nutritionalUnitari;
String idComponentUnitNutritional;

List<EntityBaseQuantity> listIncomplete;
List<EntityBaseQuantity> listNoInfo;

List<EntityBaseQuantity> listNoInfoCarbohydrates;
List<EntityBaseQuantity> listNoInfoSugar;
List<EntityBaseQuantity> listNoInfoFat;
List<EntityBaseQuantity> listNoInfoSaturated;
List<EntityBaseQuantity> listNoInfoProteins;
List<EntityBaseQuantity> listNoInfoSalt;
List<EntityBaseQuantity> listNoInfoFiber;
List<EntityBaseQuantity> listNoInfoEnergy;
}

class EntityBaseQuantity {

String id;
String descr;
Short type;
Long color;
}


energy corresponds to the kcal. All values are also returned when they could be calculated, or null otherwise.

The calculation may fail because the app can’t convert the amount of a product/ingredient used to kg; those cases are reflected in listIncomplete.

It may also happen that an ingredient lacks one of the nutritional parameters — if a recipe or dish has three products and one lacks fat, fat is returned null and flagged as not calculable, reflected in listNoInfoFat (or the equivalent for other values).

listNoInfo lists products with no nutritional information at all.

nutritionalPercent is true if the calculation was per 100 g; nutritionalUnitari is true if per the recipe or dish unit (then idComponentUnitNutritional indicates the recipe unit id); otherwise it’s for the whole recipe or dish.

Did you find this article helpful?

If you need further assistance, contact support by clicking the built-in Support Chat in the bottom-right corner of your screen.


Did this answer your question?