Skip to content

openfoodfacts/openfoodfacts-js

Repository files navigation

Open Food Facts - JS SDK

NPM Version Documentation

This is the official JS/TS SDK for the Open Food Facts API.

Installation

From NPM

npm install @openfoodfacts/openfoodfacts-nodejs

Using the latest git version

npm install git+https://github.com/openfoodfacts/openfoodfacts-js.git

Usage

Warning

Be sure to read the Open Food Facts API documentation to understand how the API should be used and what data is available BEFORE starting to use the SDK.

Import the SDK in your project and create a client instance:

import { OpenFoodFacts } from "@openfoodfacts/openfoodfacts-nodejs";

// if you're on the browser, you can pass the fetch function as a parameter
const client = new OpenFoodFacts(window.fetch);
// or if you're on Node.js, you can pass the global fetch function
const client = new OpenFoodFacts(globalThis.fetch);
// or if you're using a custom fetch implementation
import fetch from "node-fetch";

const client = new OpenFoodFacts(fetch);

(async () => {
  // then you can use the client to access the Open Food Facts API
  const { data, error } = await client.getProduct("5000112546415");
  if (!data) {
    console.error("Error fetching product:", error);
    return;
  }
  console.log("Product data:", data);
})();

Development

Prerequisites

  • Node.js
  • Yarn v4

API bindings

The project uses openapi-typescript to generate the API bindings automatically from the OpenAPI specification.

To generate the API bindings, run yarn api. The files are to be committed to the repository, so that the SDK can be used without having to download the specs every time.

Building

  • Clone the repository and run yarn install in the directory.
  • Run yarn build to generate the OpenAPI bindings and build the project.
  • Run yarn test to run the tests.

Contribute

We accept contributions of any kind: new features, bug fixes, documentation improvements, etc.

You can also help us by reporting bugs, suggesting improvements or testing new features.

When submitting a PR, please use the angular commit guideline.

Third party applications

If you use this SDK, feel free to open a PR to add your application in this list.

About

Official JavaScript package for the Open Food Facts API

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 25

Languages