Open Food Facts Python SDK

5.0.1 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

Instantiate the API client with a mandatory user agent, then retrieve product details by barcode or perform a text search. For write operations, authentication (username and password) is required.

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')}")

view raw JSON →