Difference between revisions of "Open Food Facts Search API Version 2"

From Open Food Facts wiki
Jump to navigation Jump to search
(→‎Conditions on nutrients: added links to API examples)
Line 35: Line 35:
 
Tags fields are now specified directly:
 
Tags fields are now specified directly:
 
* [https://world.openfoodfacts.org/api/v2/search?labels_tags=en:organic&fields=code,product_name,labels_tags labels_tags=en:organic]
 
* [https://world.openfoodfacts.org/api/v2/search?labels_tags=en:organic&fields=code,product_name,labels_tags labels_tags=en:organic]
* [[https://world.openfoodfacts.org/api/v2/search?labels_tags_fr=bio&fields=code,product_name,labels_tags_en labels_tags_fr=bio -> bio is assumed to be in French and thus converted to en:organic
+
* [https://world.openfoodfacts.org/api/v2/search?labels_tags_fr=bio&fields=code,product_name,labels_tags_en labels_tags_fr=bio] -> bio is assumed to be in French and thus converted to en:organic
  
 
For AND queries, use comma separated values:
 
For AND queries, use comma separated values:

Revision as of 14:12, 13 October 2020

Open Food Facts Search API Version 2

Introduction

Version 1 of the Open Food Facts search API is outdated and hacky (it was built on top of the OFF web site search form and is unnecessarily convoluted) and does not support some of the requirements for the Project:Personalized_Search (in particular being able to retrieve a given set of products using their barcodes).

We have thus created a new Open Food Facts Search API Version 2 that is simpler but also more powerful.

  • Version 1 of the API will continue to be supported for the years to come, as there are many apps that are using it.
  • Version 2 of the API was launched in production in September 2020. It has not been widely publicized yet as we may change it based on the feedback of the first users.

Examples

Products from the chocolate category, with both the organic label and the fair trade label:

Features

New /api/v2/search (JSON) and /search (OFF web site) endpoints

V1 of the search API is a hack on the web site search form and is accessed by adding &json=1 to the /cgi/search.pl web form.

The new search API introduces 2 new endpoints that mimick the product page endpoints (/product/[barcode] for the Web, and /api/v0/product/[barcode] for the API with JSON results):

  • /search will replace /cgi/search.pl as the url of search results
  • /api/v2/search will return JSON results, with the exact same parameters as the /search endpoint

Simplified parameters specification

Tags fields are now specified directly:

For AND queries, use comma separated values:

For negative queries, prefix tag with -

OR tags fields queries

For OR queries, use | to separate values

Conditions on nutrients

Development and deployment

The new Search API V2 has been developed and deployed incrementally in August and September 2020. The new V2 API co-exists with the existing Search API V1.

V1 of the API will be supported for the years to come as many apps are using it.

Related issues

Related PRs