calorie-api 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- calorie_api-0.1.0/LICENSE +21 -0
- calorie_api-0.1.0/PKG-INFO +249 -0
- calorie_api-0.1.0/README.md +220 -0
- calorie_api-0.1.0/pyproject.toml +58 -0
- calorie_api-0.1.0/setup.cfg +4 -0
- calorie_api-0.1.0/src/calorie_api/__init__.py +69 -0
- calorie_api-0.1.0/src/calorie_api/_version.py +1 -0
- calorie_api-0.1.0/src/calorie_api/client.py +468 -0
- calorie_api-0.1.0/src/calorie_api/errors.py +29 -0
- calorie_api-0.1.0/src/calorie_api/models.py +226 -0
- calorie_api-0.1.0/src/calorie_api/py.typed +0 -0
- calorie_api-0.1.0/src/calorie_api.egg-info/PKG-INFO +249 -0
- calorie_api-0.1.0/src/calorie_api.egg-info/SOURCES.txt +15 -0
- calorie_api-0.1.0/src/calorie_api.egg-info/dependency_links.txt +1 -0
- calorie_api-0.1.0/src/calorie_api.egg-info/top_level.txt +1 -0
- calorie_api-0.1.0/tests/test_client.py +400 -0
- calorie_api-0.1.0/tests/test_local_server.py +110 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 BusyBody FIT LTD
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: calorie-api
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Calorie API client for Python. Nutrition API for food search, barcode lookup, nutrient data, macro targets, portions and recipe nutrition across 6.8M+ foods.
|
|
5
|
+
Author: BusyBody FIT LTD
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://calorieapi.com/nutrition-api
|
|
8
|
+
Project-URL: Documentation, https://calorieapi.com/docs
|
|
9
|
+
Project-URL: Pricing, https://calorieapi.com/pricing
|
|
10
|
+
Project-URL: Repository, https://github.com/Busybody1/calorie-api-sdk
|
|
11
|
+
Keywords: nutrition api,calorie api,food database,barcode,macros,nutrition,calories,food api,food search,upc,recipe nutrition,tdee
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# Calorie API for Python
|
|
31
|
+
|
|
32
|
+
The official Python client for **Calorie API**, the [nutrition API](https://calorieapi.com/nutrition-api) for 6.8M+ foods. Search foods, look up barcodes, read full nutrient data, and calculate macro targets, portion nutrition and recipe totals from one small, typed, zero-dependency package.
|
|
33
|
+
|
|
34
|
+
- Docs: [calorieapi.com/docs](https://calorieapi.com/docs)
|
|
35
|
+
- Pricing: [calorieapi.com/pricing](https://calorieapi.com/pricing)
|
|
36
|
+
- Free API key: [calorieapi.com/auth/register](https://calorieapi.com/auth/register)
|
|
37
|
+
- Try it in the browser: [calorieapi.com/playground](https://calorieapi.com/playground)
|
|
38
|
+
|
|
39
|
+
Python 3.9 or newer. Standard library only (`urllib`). Fully type hinted, with `TypedDict` response types.
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install calorie-api
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Quick start
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from calorie_api import CalorieApi
|
|
51
|
+
|
|
52
|
+
api = CalorieApi("your_api_key")
|
|
53
|
+
|
|
54
|
+
page = api.search_foods("chicken breast", limit=5)
|
|
55
|
+
for food in page["data"]:
|
|
56
|
+
print(food["id"], food["name"], food["calories_100g"], "kcal per 100 g")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
If you leave out the key, the client reads the `CALORIE_API_KEY` environment variable. Requests go to `https://calorieapiadmin.com/api/v1` with your key in the `X-API-Key` header.
|
|
60
|
+
|
|
61
|
+
## Endpoints
|
|
62
|
+
|
|
63
|
+
| Method | HTTP endpoint |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `search_foods(query, **filters)` | `GET /api/v1/search/foods` |
|
|
66
|
+
| `suggest_foods(query, limit=None)` | `GET /api/v1/search/suggest` |
|
|
67
|
+
| `lookup_barcode(barcode)` | `GET /api/v1/search/barcode/{upc}` |
|
|
68
|
+
| `get_food(food_id)` | `GET /api/v1/foods/{food_id}` |
|
|
69
|
+
| `calc_macros(age=, gender=, weight_kg=, height_cm=, activity=, goal=)` | `GET /api/v1/calc/macros` |
|
|
70
|
+
| `calc_portion(food_id, grams)` | `GET /api/v1/calc/portion` |
|
|
71
|
+
| `calc_recipe(ingredients, servings=None)` | `POST /api/v1/calc/recipe` |
|
|
72
|
+
|
|
73
|
+
Every method returns the parsed JSON response as plain dicts and lists, typed with the `TypedDict` classes exported from `calorie_api` (`FoodSearchPage`, `FoodDetails`, `BarcodeLookupResult`, `MacroCalcResult` and so on). Keyword names match the API parameter names.
|
|
74
|
+
|
|
75
|
+
### Search foods
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
page = api.search_foods(
|
|
79
|
+
"greek yogurt",
|
|
80
|
+
limit=10,
|
|
81
|
+
skip=0,
|
|
82
|
+
verified_only=True,
|
|
83
|
+
max_calories=120,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
print(page["total"])
|
|
87
|
+
first = page["data"][0]
|
|
88
|
+
print(first["name"], first["brand_name"])
|
|
89
|
+
print(first["calories_100g"], first["protein_100g"], first["carbs_100g"], first["fat_100g"])
|
|
90
|
+
print(first["nutrients"])
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Returns `{"data", "total", "skip", "limit"}`. Each result has per 100 g values (`calories_100g`, `protein_100g`, `carbs_100g`, `fat_100g`, `fiber_100g`, `sugar_100g`), a `nutrients` list, serving info and a `meal` dict scaled to the default serving.
|
|
94
|
+
|
|
95
|
+
Keyword filters:
|
|
96
|
+
|
|
97
|
+
| Keyword | Notes |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| `limit` | 1 to 100, default 30. Your plan caps the results per search. |
|
|
100
|
+
| `skip` | Offset for paging |
|
|
101
|
+
| `match_mode` | `"any"` (default) or `"all"` words |
|
|
102
|
+
| `verified_only` | Only verified foods |
|
|
103
|
+
| `brand`, `brand_id` | Filter by brand |
|
|
104
|
+
| `category`, `category_id` | Filter by category |
|
|
105
|
+
| `nutrient_id`, `min_amount`, `max_amount` | Require a nutrient in a range |
|
|
106
|
+
| `min_calories`, `max_calories` | Calories per 100 g |
|
|
107
|
+
| `min_protein`, `max_protein` | Grams per 100 g |
|
|
108
|
+
| `min_carbs`, `max_carbs` | Grams per 100 g |
|
|
109
|
+
| `min_fat`, `max_fat` | Grams per 100 g |
|
|
110
|
+
|
|
111
|
+
An empty query (`""`) returns common foods.
|
|
112
|
+
|
|
113
|
+
### Autocomplete
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
suggestions = api.suggest_foods("chi", limit=8)
|
|
117
|
+
# [{"id": ..., "name": ..., "brand_name": ...}, ...]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Barcode lookup
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
product = api.lookup_barcode("3017620422003")
|
|
124
|
+
print(product["product"]["name"], product["product"]["brand"])
|
|
125
|
+
print(product["nutrition_per_100g"]["energy_kcal"], product["nutrition_per_100g"]["protein_g"])
|
|
126
|
+
print(product["micronutrients_per_100g"])
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
UPC and EAN codes are accepted as strings. Spaces and dashes are removed before the request. An unknown barcode raises `CalorieApiError` with status 404.
|
|
130
|
+
|
|
131
|
+
### Food details
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
food = api.get_food(first["id"])
|
|
135
|
+
print(food["name"], food["nutrients"], food["barcodes"], food.get("verified_portions"))
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Macro targets (TDEE)
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
macros = api.calc_macros(
|
|
142
|
+
age=28,
|
|
143
|
+
gender="male",
|
|
144
|
+
weight_kg=80,
|
|
145
|
+
height_cm=178,
|
|
146
|
+
activity="moderately_active",
|
|
147
|
+
goal="maintain",
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
print(macros["bmr"], macros["tdee"])
|
|
151
|
+
print(macros["daily_targets"])
|
|
152
|
+
# {"calories", "protein_g", "carbohydrates_g", "fat_g"}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`gender` is `"male"` or `"female"`. `activity` is `"sedentary"`, `"lightly_active"`, `"moderately_active"`, `"very_active"` or `"extra_active"`. `goal` is `"lose"`, `"maintain"` or `"gain"`. The API uses the Mifflin-St Jeor formula.
|
|
156
|
+
|
|
157
|
+
### Portion nutrition
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
portion = api.calc_portion(first["id"], 150)
|
|
161
|
+
print(portion["food_name"], portion["requested_grams"], portion["nutrition"])
|
|
162
|
+
# nutrition: calories, protein_g, carbohydrates_g, fat_g, fiber_g, sugars_g, saturated_fat_g, sodium_g
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Recipe nutrition
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
recipe = api.calc_recipe(
|
|
169
|
+
[
|
|
170
|
+
{"food_id": 12345, "grams": 300},
|
|
171
|
+
(67890, 200),
|
|
172
|
+
],
|
|
173
|
+
servings=4,
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
print(recipe["total"]["calories"], recipe["per_serving"]["calories"], recipe["ingredients_resolved"])
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Each ingredient is a dict with `food_id` and `grams`, or a `(food_id, grams)` tuple. Up to 50 ingredients and 1 to 100 servings (default 1). `ingredients_resolved` counts the ingredients found in the database.
|
|
180
|
+
|
|
181
|
+
## Error handling
|
|
182
|
+
|
|
183
|
+
Every failure raises `CalorieApiError`:
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
from calorie_api import CalorieApi, CalorieApiError
|
|
187
|
+
|
|
188
|
+
try:
|
|
189
|
+
api.get_food(999999999)
|
|
190
|
+
except CalorieApiError as error:
|
|
191
|
+
print(error) # "Calorie API returned 404: Food not found"
|
|
192
|
+
print(error.status) # 404, or None when no response arrived
|
|
193
|
+
print(error.kind) # "http_error"
|
|
194
|
+
print(error.detail) # the API's "detail" value
|
|
195
|
+
print(error.retry_after) # seconds to wait, from Retry-After, or None
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
| `kind` | When |
|
|
199
|
+
| --- | --- |
|
|
200
|
+
| `http_error` | The API answered with a non-2xx status. The message includes the status and the API's error message. |
|
|
201
|
+
| `timeout` | No response within `timeout` seconds |
|
|
202
|
+
| `network_error` | DNS, TLS or connection failure |
|
|
203
|
+
| `invalid_response` | A 2xx response that was not JSON |
|
|
204
|
+
| `invalid_argument` | A bad argument, caught before any request was sent |
|
|
205
|
+
|
|
206
|
+
Common statuses: `401` for a missing or wrong key, `402` when your monthly quota is used up, `403` when your plan does not cover the request, `404` when a food or barcode is not found, `422` for out-of-range parameters (the message lists each field), and `429` when you go over your plan's per-minute rate limit.
|
|
207
|
+
|
|
208
|
+
## Configuration
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
api = CalorieApi(
|
|
212
|
+
"your_key",
|
|
213
|
+
timeout=10,
|
|
214
|
+
base_url="https://calorieapiadmin.com",
|
|
215
|
+
)
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
| Argument | Default | Notes |
|
|
219
|
+
| --- | --- | --- |
|
|
220
|
+
| `api_key` | `CALORIE_API_KEY` env var | Your Calorie API key |
|
|
221
|
+
| `timeout` | `30.0` | Per request timeout in seconds |
|
|
222
|
+
| `base_url` | `https://calorieapiadmin.com` | API origin, without `/api/v1`. Must be https, except for localhost. |
|
|
223
|
+
| `demo` | `False` | Use the keyless public demo when no key is set |
|
|
224
|
+
|
|
225
|
+
The client never follows redirects, so your key is only sent to the host you configured. It honors the standard `HTTPS_PROXY` and `NO_PROXY` environment variables.
|
|
226
|
+
|
|
227
|
+
### Public demo (no key)
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
demo = CalorieApi(demo=True)
|
|
231
|
+
page = demo.search_foods("apple", limit=5)
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
The demo uses the `/api/v1/public/...` routes, which are limited to 30 requests per hour per IP address and one request per second, with at most 10 results per search. Use it to try the API, then switch to a free key.
|
|
235
|
+
|
|
236
|
+
## Pricing
|
|
237
|
+
|
|
238
|
+
The free tier includes 1,000 API calls a month. Paid plans start at $15 a month for 20,000 calls. Each plan also sets a rate limit and a cap on results per search (20 foods per search on the free plan). Commercial apps need the Plus or Enterprise plan. See [calorieapi.com/pricing](https://calorieapi.com/pricing) for current plans.
|
|
239
|
+
|
|
240
|
+
## Links
|
|
241
|
+
|
|
242
|
+
- [Nutrition API overview](https://calorieapi.com/nutrition-api)
|
|
243
|
+
- [API documentation](https://calorieapi.com/docs)
|
|
244
|
+
- [Pricing](https://calorieapi.com/pricing)
|
|
245
|
+
- [Get a free API key](https://calorieapi.com/auth/register)
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
MIT. Copyright (c) 2026 BusyBody FIT LTD.
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# Calorie API for Python
|
|
2
|
+
|
|
3
|
+
The official Python client for **Calorie API**, the [nutrition API](https://calorieapi.com/nutrition-api) for 6.8M+ foods. Search foods, look up barcodes, read full nutrient data, and calculate macro targets, portion nutrition and recipe totals from one small, typed, zero-dependency package.
|
|
4
|
+
|
|
5
|
+
- Docs: [calorieapi.com/docs](https://calorieapi.com/docs)
|
|
6
|
+
- Pricing: [calorieapi.com/pricing](https://calorieapi.com/pricing)
|
|
7
|
+
- Free API key: [calorieapi.com/auth/register](https://calorieapi.com/auth/register)
|
|
8
|
+
- Try it in the browser: [calorieapi.com/playground](https://calorieapi.com/playground)
|
|
9
|
+
|
|
10
|
+
Python 3.9 or newer. Standard library only (`urllib`). Fully type hinted, with `TypedDict` response types.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install calorie-api
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from calorie_api import CalorieApi
|
|
22
|
+
|
|
23
|
+
api = CalorieApi("your_api_key")
|
|
24
|
+
|
|
25
|
+
page = api.search_foods("chicken breast", limit=5)
|
|
26
|
+
for food in page["data"]:
|
|
27
|
+
print(food["id"], food["name"], food["calories_100g"], "kcal per 100 g")
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
If you leave out the key, the client reads the `CALORIE_API_KEY` environment variable. Requests go to `https://calorieapiadmin.com/api/v1` with your key in the `X-API-Key` header.
|
|
31
|
+
|
|
32
|
+
## Endpoints
|
|
33
|
+
|
|
34
|
+
| Method | HTTP endpoint |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `search_foods(query, **filters)` | `GET /api/v1/search/foods` |
|
|
37
|
+
| `suggest_foods(query, limit=None)` | `GET /api/v1/search/suggest` |
|
|
38
|
+
| `lookup_barcode(barcode)` | `GET /api/v1/search/barcode/{upc}` |
|
|
39
|
+
| `get_food(food_id)` | `GET /api/v1/foods/{food_id}` |
|
|
40
|
+
| `calc_macros(age=, gender=, weight_kg=, height_cm=, activity=, goal=)` | `GET /api/v1/calc/macros` |
|
|
41
|
+
| `calc_portion(food_id, grams)` | `GET /api/v1/calc/portion` |
|
|
42
|
+
| `calc_recipe(ingredients, servings=None)` | `POST /api/v1/calc/recipe` |
|
|
43
|
+
|
|
44
|
+
Every method returns the parsed JSON response as plain dicts and lists, typed with the `TypedDict` classes exported from `calorie_api` (`FoodSearchPage`, `FoodDetails`, `BarcodeLookupResult`, `MacroCalcResult` and so on). Keyword names match the API parameter names.
|
|
45
|
+
|
|
46
|
+
### Search foods
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
page = api.search_foods(
|
|
50
|
+
"greek yogurt",
|
|
51
|
+
limit=10,
|
|
52
|
+
skip=0,
|
|
53
|
+
verified_only=True,
|
|
54
|
+
max_calories=120,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
print(page["total"])
|
|
58
|
+
first = page["data"][0]
|
|
59
|
+
print(first["name"], first["brand_name"])
|
|
60
|
+
print(first["calories_100g"], first["protein_100g"], first["carbs_100g"], first["fat_100g"])
|
|
61
|
+
print(first["nutrients"])
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Returns `{"data", "total", "skip", "limit"}`. Each result has per 100 g values (`calories_100g`, `protein_100g`, `carbs_100g`, `fat_100g`, `fiber_100g`, `sugar_100g`), a `nutrients` list, serving info and a `meal` dict scaled to the default serving.
|
|
65
|
+
|
|
66
|
+
Keyword filters:
|
|
67
|
+
|
|
68
|
+
| Keyword | Notes |
|
|
69
|
+
| --- | --- |
|
|
70
|
+
| `limit` | 1 to 100, default 30. Your plan caps the results per search. |
|
|
71
|
+
| `skip` | Offset for paging |
|
|
72
|
+
| `match_mode` | `"any"` (default) or `"all"` words |
|
|
73
|
+
| `verified_only` | Only verified foods |
|
|
74
|
+
| `brand`, `brand_id` | Filter by brand |
|
|
75
|
+
| `category`, `category_id` | Filter by category |
|
|
76
|
+
| `nutrient_id`, `min_amount`, `max_amount` | Require a nutrient in a range |
|
|
77
|
+
| `min_calories`, `max_calories` | Calories per 100 g |
|
|
78
|
+
| `min_protein`, `max_protein` | Grams per 100 g |
|
|
79
|
+
| `min_carbs`, `max_carbs` | Grams per 100 g |
|
|
80
|
+
| `min_fat`, `max_fat` | Grams per 100 g |
|
|
81
|
+
|
|
82
|
+
An empty query (`""`) returns common foods.
|
|
83
|
+
|
|
84
|
+
### Autocomplete
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
suggestions = api.suggest_foods("chi", limit=8)
|
|
88
|
+
# [{"id": ..., "name": ..., "brand_name": ...}, ...]
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Barcode lookup
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
product = api.lookup_barcode("3017620422003")
|
|
95
|
+
print(product["product"]["name"], product["product"]["brand"])
|
|
96
|
+
print(product["nutrition_per_100g"]["energy_kcal"], product["nutrition_per_100g"]["protein_g"])
|
|
97
|
+
print(product["micronutrients_per_100g"])
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
UPC and EAN codes are accepted as strings. Spaces and dashes are removed before the request. An unknown barcode raises `CalorieApiError` with status 404.
|
|
101
|
+
|
|
102
|
+
### Food details
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
food = api.get_food(first["id"])
|
|
106
|
+
print(food["name"], food["nutrients"], food["barcodes"], food.get("verified_portions"))
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Macro targets (TDEE)
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
macros = api.calc_macros(
|
|
113
|
+
age=28,
|
|
114
|
+
gender="male",
|
|
115
|
+
weight_kg=80,
|
|
116
|
+
height_cm=178,
|
|
117
|
+
activity="moderately_active",
|
|
118
|
+
goal="maintain",
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
print(macros["bmr"], macros["tdee"])
|
|
122
|
+
print(macros["daily_targets"])
|
|
123
|
+
# {"calories", "protein_g", "carbohydrates_g", "fat_g"}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`gender` is `"male"` or `"female"`. `activity` is `"sedentary"`, `"lightly_active"`, `"moderately_active"`, `"very_active"` or `"extra_active"`. `goal` is `"lose"`, `"maintain"` or `"gain"`. The API uses the Mifflin-St Jeor formula.
|
|
127
|
+
|
|
128
|
+
### Portion nutrition
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
portion = api.calc_portion(first["id"], 150)
|
|
132
|
+
print(portion["food_name"], portion["requested_grams"], portion["nutrition"])
|
|
133
|
+
# nutrition: calories, protein_g, carbohydrates_g, fat_g, fiber_g, sugars_g, saturated_fat_g, sodium_g
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Recipe nutrition
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
recipe = api.calc_recipe(
|
|
140
|
+
[
|
|
141
|
+
{"food_id": 12345, "grams": 300},
|
|
142
|
+
(67890, 200),
|
|
143
|
+
],
|
|
144
|
+
servings=4,
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
print(recipe["total"]["calories"], recipe["per_serving"]["calories"], recipe["ingredients_resolved"])
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Each ingredient is a dict with `food_id` and `grams`, or a `(food_id, grams)` tuple. Up to 50 ingredients and 1 to 100 servings (default 1). `ingredients_resolved` counts the ingredients found in the database.
|
|
151
|
+
|
|
152
|
+
## Error handling
|
|
153
|
+
|
|
154
|
+
Every failure raises `CalorieApiError`:
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from calorie_api import CalorieApi, CalorieApiError
|
|
158
|
+
|
|
159
|
+
try:
|
|
160
|
+
api.get_food(999999999)
|
|
161
|
+
except CalorieApiError as error:
|
|
162
|
+
print(error) # "Calorie API returned 404: Food not found"
|
|
163
|
+
print(error.status) # 404, or None when no response arrived
|
|
164
|
+
print(error.kind) # "http_error"
|
|
165
|
+
print(error.detail) # the API's "detail" value
|
|
166
|
+
print(error.retry_after) # seconds to wait, from Retry-After, or None
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
| `kind` | When |
|
|
170
|
+
| --- | --- |
|
|
171
|
+
| `http_error` | The API answered with a non-2xx status. The message includes the status and the API's error message. |
|
|
172
|
+
| `timeout` | No response within `timeout` seconds |
|
|
173
|
+
| `network_error` | DNS, TLS or connection failure |
|
|
174
|
+
| `invalid_response` | A 2xx response that was not JSON |
|
|
175
|
+
| `invalid_argument` | A bad argument, caught before any request was sent |
|
|
176
|
+
|
|
177
|
+
Common statuses: `401` for a missing or wrong key, `402` when your monthly quota is used up, `403` when your plan does not cover the request, `404` when a food or barcode is not found, `422` for out-of-range parameters (the message lists each field), and `429` when you go over your plan's per-minute rate limit.
|
|
178
|
+
|
|
179
|
+
## Configuration
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
api = CalorieApi(
|
|
183
|
+
"your_key",
|
|
184
|
+
timeout=10,
|
|
185
|
+
base_url="https://calorieapiadmin.com",
|
|
186
|
+
)
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
| Argument | Default | Notes |
|
|
190
|
+
| --- | --- | --- |
|
|
191
|
+
| `api_key` | `CALORIE_API_KEY` env var | Your Calorie API key |
|
|
192
|
+
| `timeout` | `30.0` | Per request timeout in seconds |
|
|
193
|
+
| `base_url` | `https://calorieapiadmin.com` | API origin, without `/api/v1`. Must be https, except for localhost. |
|
|
194
|
+
| `demo` | `False` | Use the keyless public demo when no key is set |
|
|
195
|
+
|
|
196
|
+
The client never follows redirects, so your key is only sent to the host you configured. It honors the standard `HTTPS_PROXY` and `NO_PROXY` environment variables.
|
|
197
|
+
|
|
198
|
+
### Public demo (no key)
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
demo = CalorieApi(demo=True)
|
|
202
|
+
page = demo.search_foods("apple", limit=5)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
The demo uses the `/api/v1/public/...` routes, which are limited to 30 requests per hour per IP address and one request per second, with at most 10 results per search. Use it to try the API, then switch to a free key.
|
|
206
|
+
|
|
207
|
+
## Pricing
|
|
208
|
+
|
|
209
|
+
The free tier includes 1,000 API calls a month. Paid plans start at $15 a month for 20,000 calls. Each plan also sets a rate limit and a cap on results per search (20 foods per search on the free plan). Commercial apps need the Plus or Enterprise plan. See [calorieapi.com/pricing](https://calorieapi.com/pricing) for current plans.
|
|
210
|
+
|
|
211
|
+
## Links
|
|
212
|
+
|
|
213
|
+
- [Nutrition API overview](https://calorieapi.com/nutrition-api)
|
|
214
|
+
- [API documentation](https://calorieapi.com/docs)
|
|
215
|
+
- [Pricing](https://calorieapi.com/pricing)
|
|
216
|
+
- [Get a free API key](https://calorieapi.com/auth/register)
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
MIT. Copyright (c) 2026 BusyBody FIT LTD.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "calorie-api"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Calorie API client for Python. Nutrition API for food search, barcode lookup, nutrient data, macro targets, portions and recipe nutrition across 6.8M+ foods."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "BusyBody FIT LTD" }]
|
|
14
|
+
keywords = [
|
|
15
|
+
"nutrition api",
|
|
16
|
+
"calorie api",
|
|
17
|
+
"food database",
|
|
18
|
+
"barcode",
|
|
19
|
+
"macros",
|
|
20
|
+
"nutrition",
|
|
21
|
+
"calories",
|
|
22
|
+
"food api",
|
|
23
|
+
"food search",
|
|
24
|
+
"upc",
|
|
25
|
+
"recipe nutrition",
|
|
26
|
+
"tdee",
|
|
27
|
+
]
|
|
28
|
+
classifiers = [
|
|
29
|
+
"Development Status :: 4 - Beta",
|
|
30
|
+
"Intended Audience :: Developers",
|
|
31
|
+
"Operating System :: OS Independent",
|
|
32
|
+
"Programming Language :: Python :: 3",
|
|
33
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
34
|
+
"Programming Language :: Python :: 3.9",
|
|
35
|
+
"Programming Language :: Python :: 3.10",
|
|
36
|
+
"Programming Language :: Python :: 3.11",
|
|
37
|
+
"Programming Language :: Python :: 3.12",
|
|
38
|
+
"Programming Language :: Python :: 3.13",
|
|
39
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
40
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
41
|
+
"Typing :: Typed",
|
|
42
|
+
]
|
|
43
|
+
dependencies = []
|
|
44
|
+
|
|
45
|
+
[project.urls]
|
|
46
|
+
Homepage = "https://calorieapi.com/nutrition-api"
|
|
47
|
+
Documentation = "https://calorieapi.com/docs"
|
|
48
|
+
Pricing = "https://calorieapi.com/pricing"
|
|
49
|
+
Repository = "https://github.com/Busybody1/calorie-api-sdk"
|
|
50
|
+
|
|
51
|
+
[tool.setuptools.dynamic]
|
|
52
|
+
version = { attr = "calorie_api._version.__version__" }
|
|
53
|
+
|
|
54
|
+
[tool.setuptools.packages.find]
|
|
55
|
+
where = ["src"]
|
|
56
|
+
|
|
57
|
+
[tool.setuptools.package-data]
|
|
58
|
+
calorie_api = ["py.typed"]
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
from ._version import __version__
|
|
2
|
+
from .client import DEFAULT_BASE_URL, DEFAULT_TIMEOUT, CalorieApi, normalize_rapidapi_host
|
|
3
|
+
from .errors import CalorieApiError, ErrorKind
|
|
4
|
+
from .models import (
|
|
5
|
+
ActivityLevel,
|
|
6
|
+
BarcodeLookupResult,
|
|
7
|
+
BarcodeMicronutrient,
|
|
8
|
+
BarcodeNutrition,
|
|
9
|
+
BarcodeProduct,
|
|
10
|
+
BarcodeServing,
|
|
11
|
+
FoodBarcode,
|
|
12
|
+
FoodDetails,
|
|
13
|
+
FoodNutrient,
|
|
14
|
+
FoodSearchPage,
|
|
15
|
+
FoodSearchResult,
|
|
16
|
+
FoodSuggestion,
|
|
17
|
+
Gender,
|
|
18
|
+
Goal,
|
|
19
|
+
Ingredient,
|
|
20
|
+
MacroCalcResult,
|
|
21
|
+
MacroTargets,
|
|
22
|
+
MatchMode,
|
|
23
|
+
Meal,
|
|
24
|
+
MealMacros,
|
|
25
|
+
Mode,
|
|
26
|
+
NutrientItem,
|
|
27
|
+
Portion,
|
|
28
|
+
PortionCalcResult,
|
|
29
|
+
PortionNutrition,
|
|
30
|
+
RecipeCalcResult,
|
|
31
|
+
RecipeTotals,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"__version__",
|
|
36
|
+
"DEFAULT_BASE_URL",
|
|
37
|
+
"DEFAULT_TIMEOUT",
|
|
38
|
+
"CalorieApi",
|
|
39
|
+
"CalorieApiError",
|
|
40
|
+
"ErrorKind",
|
|
41
|
+
"normalize_rapidapi_host",
|
|
42
|
+
"ActivityLevel",
|
|
43
|
+
"BarcodeLookupResult",
|
|
44
|
+
"BarcodeMicronutrient",
|
|
45
|
+
"BarcodeNutrition",
|
|
46
|
+
"BarcodeProduct",
|
|
47
|
+
"BarcodeServing",
|
|
48
|
+
"FoodBarcode",
|
|
49
|
+
"FoodDetails",
|
|
50
|
+
"FoodNutrient",
|
|
51
|
+
"FoodSearchPage",
|
|
52
|
+
"FoodSearchResult",
|
|
53
|
+
"FoodSuggestion",
|
|
54
|
+
"Gender",
|
|
55
|
+
"Goal",
|
|
56
|
+
"Ingredient",
|
|
57
|
+
"MacroCalcResult",
|
|
58
|
+
"MacroTargets",
|
|
59
|
+
"MatchMode",
|
|
60
|
+
"Meal",
|
|
61
|
+
"MealMacros",
|
|
62
|
+
"Mode",
|
|
63
|
+
"NutrientItem",
|
|
64
|
+
"Portion",
|
|
65
|
+
"PortionCalcResult",
|
|
66
|
+
"PortionNutrition",
|
|
67
|
+
"RecipeCalcResult",
|
|
68
|
+
"RecipeTotals",
|
|
69
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|