calorieapi / calorie-api
Calorie API client for PHP. Nutrition API for food search, barcode lookup, nutrient data, macro targets, portions and recipe nutrition across 6.8M+ foods.
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The official PHP client for Calorie API, the 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 package with no Composer dependencies.
- Docs: calorieapi.com/docs
- Pricing: calorieapi.com/pricing
- Free API key: calorieapi.com/auth/register
- Try it in the browser: calorieapi.com/playground
PHP 8.1 or newer with the curl and json extensions. PSR-4 autoloading.
Install
composer require calorieapi/calorie-api
Quick start
<?php require 'vendor/autoload.php'; use CalorieApi\Client; $api = new Client(apiKey: 'your_api_key'); $page = $api->searchFoods('chicken breast', limit: 5); foreach ($page['data'] as $food) { echo $food['id'], ' ', $food['name'], ' ', $food['calories_100g'], " kcal per 100 g\n"; }
If you leave out apiKey, 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 |
|---|---|
searchFoods($query, ...filters) |
GET /api/v1/search/foods |
suggestFoods($query, limit: null) |
GET /api/v1/search/suggest |
lookupBarcode($barcode) |
GET /api/v1/search/barcode/{upc} |
getFood($foodId) |
GET /api/v1/foods/{food_id} |
calcMacros($age, $gender, $weightKg, $heightCm, $activity, $goal) |
GET /api/v1/calc/macros |
calcPortion($foodId, $grams) |
GET /api/v1/calc/portion |
calcRecipe($ingredients, servings: null) |
POST /api/v1/calc/recipe |
Every method returns the decoded JSON response as an associative array. Keys are the API's own snake_case field names. Use PHP 8 named arguments for filters and options.
Search foods
$page = $api->searchFoods( 'greek yogurt', limit: 10, skip: 0, verifiedOnly: true, maxCalories: 120, ); echo $page['total'], "\n"; $first = $page['data'][0]; echo $first['name'], ' ', $first['brand_name'] ?? '', "\n"; echo $first['calories_100g'], ' ', $first['protein_100g'], ' ', $first['carbs_100g'], ' ', $first['fat_100g'], "\n"; print_r($first['nutrients']);
Returns ['data' => [...], 'total' => int, 'skip' => int, 'limit' => int]. 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 array scaled to the default serving.
Named filters:
| Argument | API parameter | Notes |
|---|---|---|
limit |
limit |
1 to 100, default 30. Your plan caps the results per search. |
skip |
skip |
Offset for paging |
matchMode |
match_mode |
'any' (default) or 'all' words |
verifiedOnly |
verified_only |
Only verified foods |
brand, brandId |
brand, brand_id |
Filter by brand |
category, categoryId |
category, category_id |
Filter by category |
nutrientId, minAmount, maxAmount |
nutrient_id, min_amount, max_amount |
Require a nutrient in a range |
minCalories, maxCalories |
min_calories, max_calories |
Calories per 100 g |
minProtein, maxProtein |
min_protein, max_protein |
Grams per 100 g |
minCarbs, maxCarbs |
min_carbs, max_carbs |
Grams per 100 g |
minFat, maxFat |
min_fat, max_fat |
Grams per 100 g |
An empty query ('') returns common foods.
Autocomplete
$suggestions = $api->suggestFoods('chi', limit: 8); // [['id' => ..., 'name' => ..., 'brand_name' => ...], ...]
Barcode lookup
$product = $api->lookupBarcode('3017620422003'); echo $product['product']['name'], ' ', $product['product']['brand'] ?? '', "\n"; echo $product['nutrition_per_100g']['energy_kcal'], ' kcal per 100 g', "\n"; print_r($product['micronutrients_per_100g']);
UPC and EAN codes are accepted as strings. Spaces and dashes are removed before the request. An unknown barcode throws CalorieApiException with status 404.
Food details
$food = $api->getFood($first['id']); print_r($food['nutrients']); print_r($food['barcodes']);
Macro targets (TDEE)
$macros = $api->calcMacros( age: 28, gender: 'male', weightKg: 80, heightCm: 178, activity: 'moderately_active', goal: 'maintain', ); echo $macros['bmr'], ' ', $macros['tdee'], "\n"; print_r($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
$portion = $api->calcPortion($first['id'], 150); echo $portion['food_name'], ' ', $portion['requested_grams'], "\n"; print_r($portion['nutrition']);
Recipe nutrition
$recipe = $api->calcRecipe( [ ['food_id' => 12345, 'grams' => 300], [67890, 200], ], servings: 4, ); echo $recipe['total']['calories'], ' ', $recipe['per_serving']['calories'], ' ', $recipe['ingredients_resolved'], "\n";
Each ingredient is ['food_id' => int, 'grams' => number] or a [food_id, grams] pair. Up to 50 ingredients and 1 to 100 servings (default 1). ingredients_resolved counts the ingredients found in the database.
Error handling
Every failure throws CalorieApi\CalorieApiException:
use CalorieApi\CalorieApiException; try { $api->getFood(999999999); } catch (CalorieApiException $error) { echo $error->getMessage(), "\n"; // "Calorie API returned 404: Food not found" var_dump($error->getStatus()); // 404, or null when no response arrived echo $error->getKind(), "\n"; // "http_error" var_dump($error->getDetail()); // the API's "detail" value var_dump($error->getRetryAfter()); // seconds to wait, from Retry-After, or null }
getKind() |
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 the timeout |
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
$api = new Client( apiKey: 'your_key', timeout: 10.0, baseUrl: 'https://calorieapiadmin.com', );
| Argument | Default | Notes |
|---|---|---|
apiKey |
CALORIE_API_KEY env var |
Your Calorie API key |
timeout |
30.0 |
Per request timeout in seconds |
baseUrl |
https://calorieapiadmin.com |
API origin, without /api/v1. Must be https, except for localhost. |
transport |
CurlTransport |
Any CalorieApi\Http\TransportInterface, for proxies or tests |
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.
Public demo (no key)
$demo = new Client(demo: true); $page = $demo->searchFoods('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 for current plans.
Tests
php tests/run.php
The tests use a fake transport and never call the real API.
Links
License
MIT. Copyright (c) 2026 BusyBody FIT LTD.