Metadata-Version: 2.4
Name: calorie-api
Version: 0.1.0
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.
Author: BusyBody FIT LTD
License-Expression: MIT
Project-URL: Homepage, https://calorieapi.com/nutrition-api
Project-URL: Documentation, https://calorieapi.com/docs
Project-URL: Pricing, https://calorieapi.com/pricing
Project-URL: Repository, https://github.com/Busybody1/calorie-api-sdk
Keywords: nutrition api,calorie api,food database,barcode,macros,nutrition,calories,food api,food search,upc,recipe nutrition,tdee
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# Calorie API for Python

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.

- Docs: [calorieapi.com/docs](https://calorieapi.com/docs)
- Pricing: [calorieapi.com/pricing](https://calorieapi.com/pricing)
- Free API key: [calorieapi.com/auth/register](https://calorieapi.com/auth/register)
- Try it in the browser: [calorieapi.com/playground](https://calorieapi.com/playground)

Python 3.9 or newer. Standard library only (`urllib`). Fully type hinted, with `TypedDict` response types.

## Install

```bash
pip install calorie-api
```

## Quick start

```python
from calorie_api import CalorieApi

api = CalorieApi("your_api_key")

page = api.search_foods("chicken breast", limit=5)
for food in page["data"]:
    print(food["id"], food["name"], food["calories_100g"], "kcal per 100 g")
```

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.

## Endpoints

| Method | HTTP endpoint |
| --- | --- |
| `search_foods(query, **filters)` | `GET /api/v1/search/foods` |
| `suggest_foods(query, limit=None)` | `GET /api/v1/search/suggest` |
| `lookup_barcode(barcode)` | `GET /api/v1/search/barcode/{upc}` |
| `get_food(food_id)` | `GET /api/v1/foods/{food_id}` |
| `calc_macros(age=, gender=, weight_kg=, height_cm=, activity=, goal=)` | `GET /api/v1/calc/macros` |
| `calc_portion(food_id, grams)` | `GET /api/v1/calc/portion` |
| `calc_recipe(ingredients, servings=None)` | `POST /api/v1/calc/recipe` |

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.

### Search foods

```python
page = api.search_foods(
    "greek yogurt",
    limit=10,
    skip=0,
    verified_only=True,
    max_calories=120,
)

print(page["total"])
first = page["data"][0]
print(first["name"], first["brand_name"])
print(first["calories_100g"], first["protein_100g"], first["carbs_100g"], first["fat_100g"])
print(first["nutrients"])
```

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.

Keyword filters:

| Keyword | Notes |
| --- | --- |
| `limit` | 1 to 100, default 30. Your plan caps the results per search. |
| `skip` | Offset for paging |
| `match_mode` | `"any"` (default) or `"all"` words |
| `verified_only` | Only verified foods |
| `brand`, `brand_id` | Filter by brand |
| `category`, `category_id` | Filter by category |
| `nutrient_id`, `min_amount`, `max_amount` | Require a nutrient in a range |
| `min_calories`, `max_calories` | Calories per 100 g |
| `min_protein`, `max_protein` | Grams per 100 g |
| `min_carbs`, `max_carbs` | Grams per 100 g |
| `min_fat`, `max_fat` | Grams per 100 g |

An empty query (`""`) returns common foods.

### Autocomplete

```python
suggestions = api.suggest_foods("chi", limit=8)
# [{"id": ..., "name": ..., "brand_name": ...}, ...]
```

### Barcode lookup

```python
product = api.lookup_barcode("3017620422003")
print(product["product"]["name"], product["product"]["brand"])
print(product["nutrition_per_100g"]["energy_kcal"], product["nutrition_per_100g"]["protein_g"])
print(product["micronutrients_per_100g"])
```

UPC and EAN codes are accepted as strings. Spaces and dashes are removed before the request. An unknown barcode raises `CalorieApiError` with status 404.

### Food details

```python
food = api.get_food(first["id"])
print(food["name"], food["nutrients"], food["barcodes"], food.get("verified_portions"))
```

### Macro targets (TDEE)

```python
macros = api.calc_macros(
    age=28,
    gender="male",
    weight_kg=80,
    height_cm=178,
    activity="moderately_active",
    goal="maintain",
)

print(macros["bmr"], macros["tdee"])
print(macros["daily_targets"])
# {"calories", "protein_g", "carbohydrates_g", "fat_g"}
```

`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.

### Portion nutrition

```python
portion = api.calc_portion(first["id"], 150)
print(portion["food_name"], portion["requested_grams"], portion["nutrition"])
# nutrition: calories, protein_g, carbohydrates_g, fat_g, fiber_g, sugars_g, saturated_fat_g, sodium_g
```

### Recipe nutrition

```python
recipe = api.calc_recipe(
    [
        {"food_id": 12345, "grams": 300},
        (67890, 200),
    ],
    servings=4,
)

print(recipe["total"]["calories"], recipe["per_serving"]["calories"], recipe["ingredients_resolved"])
```

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.

## Error handling

Every failure raises `CalorieApiError`:

```python
from calorie_api import CalorieApi, CalorieApiError

try:
    api.get_food(999999999)
except CalorieApiError as error:
    print(error)              # "Calorie API returned 404: Food not found"
    print(error.status)       # 404, or None when no response arrived
    print(error.kind)         # "http_error"
    print(error.detail)       # the API's "detail" value
    print(error.retry_after)  # seconds to wait, from Retry-After, or None
```

| `kind` | When |
| --- | --- |
| `http_error` | The API answered with a non-2xx status. The message includes the status and the API's error message. |
| `timeout` | No response within `timeout` seconds |
| `network_error` | DNS, TLS or connection failure |
| `invalid_response` | A 2xx response that was not JSON |
| `invalid_argument` | A bad argument, caught before any request was sent |

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.

## Configuration

```python
api = CalorieApi(
    "your_key",
    timeout=10,
    base_url="https://calorieapiadmin.com",
)
```

| Argument | Default | Notes |
| --- | --- | --- |
| `api_key` | `CALORIE_API_KEY` env var | Your Calorie API key |
| `timeout` | `30.0` | Per request timeout in seconds |
| `base_url` | `https://calorieapiadmin.com` | API origin, without `/api/v1`. Must be https, except for localhost. |
| `demo` | `False` | Use the keyless public demo when no key is set |

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.

### Public demo (no key)

```python
demo = CalorieApi(demo=True)
page = demo.search_foods("apple", limit=5)
```

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.

## Pricing

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.

## Links

- [Nutrition API overview](https://calorieapi.com/nutrition-api)
- [API documentation](https://calorieapi.com/docs)
- [Pricing](https://calorieapi.com/pricing)
- [Get a free API key](https://calorieapi.com/auth/register)

## License

MIT. Copyright (c) 2026 BusyBody FIT LTD.
