Client API
The client module provides the main interface for interacting with the USDA Food Data Central API.
FdcClient
- class usda_fdc.client.FdcClient(api_key=None, base_url='https://api.nal.usda.gov/fdc/v1/')[source]
Bases:
objectClient for interacting with the USDA Food Data Central API.
- session
A session object for making HTTP requests.
- Type:
- __init__(api_key=None, base_url='https://api.nal.usda.gov/fdc/v1/')[source]
Initialize the FDC client.
- Parameters:
- Raises:
ValueError – If no API key is provided or found in environment variables.
- search(query, data_type=None, page_size=50, page_number=1, sort_by=None, sort_order=None, brand_owner=None)[source]
Search for foods using keywords.
- Parameters:
query (str) – One or more search terms.
data_type (List[str] | None) – Filter on specific data types (e.g., [“Branded”, “Foundation”]).
page_size (int) – Maximum number of results to return (1-200).
page_number (int) – Page number to retrieve.
sort_by (str | None) – Field to sort by.
sort_order (str | None) – Sort direction (“asc” or “desc”).
brand_owner (str | None) – Filter by brand owner (Branded Foods only).
- Returns:
A SearchResult object containing the search results.
- Return type:
- get_food(fdc_id, format='full', nutrients=None)[source]
Get detailed information for a specific food by FDC ID.
- get_foods(fdc_ids, format='full', nutrients=None)[source]
Get detailed information for multiple foods by FDC ID.