Open Food Facts Python SDK
The official Python SDK for the Open Food Facts project, providing a simple interface to the Open Food Facts API. It allows users to retrieve product information, perform text searches, and create/update products. It also includes helpers for taxonomies, data dump iteration, and OCR. The current version is 5.0.1. Please note that this SDK is still in beta, and its API is subject to frequent change. The library is actively maintained with major versions often released within weeks or months.
Warnings
- gotcha The SDK is currently in beta, and the underlying API is subject to change. It is highly recommended to pin the exact version of `openfoodfacts` in your `requirements.txt` to avoid unexpected breaking changes.
- breaking Version 5.0.0 introduced breaking changes related to improvements in the `ImageClassifier` module. If you were using image classification functionalities, your code might require updates.
- breaking Version 4.0.0 included a breaking change related to `warnings.deprecated` being exclusively available on Python 3.13+. If your application used `warnings.deprecated` in conjunction with the SDK on Python versions older than 3.13, you might encounter issues.
- gotcha A `user_agent` is a mandatory parameter when instantiating the `API` object. This helps Open Food Facts identify usage and contact you if necessary.
- gotcha The API should not be used for downloading large amounts of data (e.g., the entire database). For bulk data access, Open Food Facts provides daily data dumps (CSV, JSONL, MongoDB dumps) that should be used instead.
- gotcha Writing data (e.g., creating or updating products) to Open Food Facts requires authentication. You must provide a valid `username` and `password` when instantiating the `API` object.
Install
-
pip install openfoodfacts
Imports
- API
from openfoodfacts import API
Quickstart
import os
from openfoodfacts import API
# User-Agent is mandatory for API calls
# It's recommended to include your application name and version
user_agent = os.environ.get('OPENFOODFACTS_USER_AGENT', 'MyAwesomeApp/1.0 - (https://example.com/myapp)')
api = API(user_agent=user_agent)
# Get information about a product by barcode
product_code = "3017620422003" # Example: Nutella
product_info = api.product.get(product_code, fields=["code", "product_name", "nutrition_grades", "ingredients_text"])
print(f"Product Name: {product_info.get('product', {}).get('product_name')}")
print(f"Nutri-Score: {product_info.get('product', {}).get('nutrition_grades')}")
# Perform a text search
search_results = api.product.text_search("mineral water")
print(f"Found {search_results.get('count')} products for 'mineral water'.")
if search_results.get('products'):
print(f"First result: {search_results['products'][0].get('product_name')}")