# @smartwithfood/js-sdk

> [![NPM Version](https://img.shields.io/npm/v/@smartwithfood/js-sdk.svg)](https://www.npmjs.com/package/@smartwithfood/js-sdk)

Latest version **2.0.4** (published 2026-02-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install @smartwithfood/js-sdk
pnpm add @smartwithfood/js-sdk
yarn add @smartwithfood/js-sdk
bun add @smartwithfood/js-sdk
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2026-02-12 |
| First published | 2020-10-08 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 91.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Fritz Hoste, Jef Braem |
| Maintainers | smartwithfood |

## Links

- npm: https://www.npmjs.com/package/@smartwithfood/js-sdk
- npm.io page: https://npm.io/package/@smartwithfood/js-sdk

## Dependencies (3)

- [events](https://npm.io/package/events.md) ^3.3.0
- [js-base64](https://npm.io/package/js-base64.md) ^3.7.5
- [short-unique-id](https://npm.io/package/short-unique-id.md) ^4.4.4

## Recent versions

- 2.0.4 (latest) — 2026-02-12
- 1.5.24-beta.1 (beta) — 2023-03-17
- 2.0.3 — 2025-12-18
- 2.0.2 — 2023-07-17
- 2.0.1 — 2023-07-17
- 2.0.0 — 2023-07-11
- 2.0.0-alpha.23 — 2023-07-11
- 2.0.0-alpha.22 — 2023-07-11
- 2.0.0-alpha.21 — 2023-07-11
- 2.0.0-alpha.20 — 2023-07-11
- 2.0.0-alpha.19 — 2023-07-11
- 2.0.0-alpha.18 — 2023-07-07
- 2.0.0-alpha.17 — 2023-07-07
- 2.0.0-alpha.16 — 2023-07-07
- 2.0.0-alpha.15 — 2023-07-07
- … 48 more at https://npm.io/package/@smartwithfood/js-sdk/versions

## README

# SmartWithFood JS SDK

[![NPM Version](https://img.shields.io/npm/v/@smartwithfood/js-sdk.svg)](https://www.npmjs.com/package/@smartwithfood/js-sdk)

SmartWithFood JS SDK is designed to integrate a widget on your website or SPA

## Table of Contents

- [SmartWithFood JS SDK](#smartwithfood-js-sdk)
  - [Table of Contents](#table-of-contents)
  - [Installation](#installation)
  - [Usage](#usage)
  - [API](#api)
    - [`function` Recipe2Basket.initialize(options: InitializeOptions)](#function-recipe2basketinitializeoptions-initializeoptions)
    - [`function` Recipe2Basket.openModal(options: ModalOptions)](#function-recipe2basketopenmodaloptions-modaloptions)
    - [`function` Recipe2Basket.closeModal()](#function-recipe2basketclosemodal)
    - [`function` Recipe2Basket.renderButtons(options: ButtonOptions): CleanupFunction](#function-recipe2basketrenderbuttonsoptions-buttonoptions-cleanupfunction)
    - [`function` Recipe2Basket.getBasketUri(options: ModalOptions): String](#function-recipe2basketgetbasketurioptions-modaloptions-string)
    - [`function` Recipe2Basket.on(name: String, callback: Function)](#function-recipe2basketonname-string-callback-function)
    - [`function` CleanupFunction](#function-cleanupfunction)
    - [`type` Recipe](#type-recipe)
    - [`object` InitializeOptions](#object-initializeoptions)
    - [`object` RecipeObject](#object-recipeobject)
    - [`object` ModalOptions](#object-modaloptions)
    - [`object` ButtonOptions](#object-buttonoptions)
    - [`enum` Language](#enum-language)
    - [`enum` ButtonType](#enum-buttontype)
    - [`object` GenericEventData](#object-genericeventdata)


## Installation

```sh
npm install @smartwithfood/js-sdk
```

Using yarn:
```sh
yarn add @smartwithfood/js-sdk
```

Alternatively, you can include it in your project from our CDN.
Replace `{version}` with the desired package version.
```html
<script src="https://unpkg.com/@smartwithfood/js-sdk@{version}/dist/index.min.js"></script>
```

## Usage

ESModules
```ts
import Recipe2Basket, { Language } from '@smartwithfood/js-sdk';

Recipe2Basket.initialize({
  token: '<your-widget-token>',
  language: Language.NL,
});

Recipe2Basket.renderButtons({ ... });
```

Browser
```html
<script src="https://unpkg.com/@smartwithfood/js-sdk@{version}/dist/index.min.js"></script>
<script>
  window.Recipe2Basket.initialize({
    token: '<your-widget-token>',
    language: 'fr',
  });

  Recipe2Basket.renderButtons({ ... });
</script>
```

## API

### `function` Recipe2Basket.initialize(options: [InitializeOptions](#object-initializeoptions))

```js
Recipe2Basket.initialize({
  token: '<your-widget-token>',
  language: Language.NL,
});
```

### `function` Recipe2Basket.openModal(options: [ModalOptions](#object-modaloptions))

Open the widget modal.

```js
Recipe2Basket.openModal({
  partnerId: '00000000-0000-0000-0000-000000000000',
  recipes: [
    {
      name: 'Recept',
      language: 'nl',
      media: 'https://example.com/image.png',
      ingredients: [
        "1 ei",
        "100 g aardappelen"
      ],
      yield: 4
    }
  ]
});
```

### `function` Recipe2Basket.closeModal()

```js
Recipe2Basket.closeModal();
```

### `function` Recipe2Basket.renderButtons(options: [ButtonOptions](#object-buttonoptions)): [CleanupFunction](#function-cleanupfunction)

Render a button which opens the widget modal when pressed. Returns a [CleanupFunction](#function-cleanupfunction) which will remove the button when called.

```js
Recipe2Basket.renderButtons({
  type: ButtonType.SINGLE_RECIPE,
  selectorOrElement: '.r2b-button-container',
  recipes: [
    {
      name: 'Recept',
      language: 'nl',
      media: 'https://example.com/image.png',
      ingredients: [
        "1 ei",
        "100 g aardappelen"
      ],
      yield: 4
    }
  ],
});
```

### `function` Recipe2Basket.getBasketUri(options: [ModalOptions](#object-modaloptions)): String

Returns the URI which can be used in an `iframe` or `redirect` to show the widget.

```js
Recipe2Basket.getBasketUri({
  partnerId: '00000000-0000-0000-0000-000000000000',
  recipes: [
    {
      name: 'Recept',
      language: 'nl',
      media: 'https://example.com/image.png',
      ingredients: [
        "1 ei",
        "100 g aardappelen"
      ],
      yield: 4
    }
  ]
});
```

### `function` Recipe2Basket.on(name: String, callback: Function)

Each event's `Data` object extends the [GenericEventData](#object-genericeventdata) object.

| Name            | Data                   | Description |
| --------------- | ---------------------- | ----------- |
| inited          |                        | Widget interface is loaded and is fetching data |
| loaded          |                        | Widget interface is ready |
| modalClosed     |                        | Widget is closed |
| basketCompleted | `{ basketId: string }` | User has completed a basket |

Sample code to handle events

```js
r2b.on('inited', function() {
  console.log('Recipe2Basket is initialized');
})
```

### `function` CleanupFunction

A cleanup function which when called will remove the previously made element and modal from the DOM. This is usefull when using frameworks like `React`. Here is some example code on how this could be used.

```jsx
import React, { useLayout } from 'react';
import Recipe2Basket from '@smartwithfood/js-sdk';

Recipe2Basket.initialize({ ... });

export function MyComponent() {
  useEffect(() => {
    const buttonCleanup = Recipe2Basket.renderButon({
      selectorOrElement: '#add-recipe-button',
      ...
    });

    return () => {
      buttonCleanup();
    }
  });

  return <div id="add-recipe-button"></div>;
}
```

### `type` Recipe

A Recipe is a [RecipeObject](#object-recipeobject) **OR** a `String`. When a string is provided we assume this is a `uuid` from our `Recipe Management` service.

### `object` InitializeOptions

| Property        | Default         | Type                       | Description |
| --------------- | --------------- | -------------------------- | ----------- |
| token*          | `None`          | String                     | Your widget identification token |
| language*       | `None`          | [Language](#enum-language) | Language of the widget |

### `object` RecipeObject

| Property    | Type                        |
| ----------- | --------------------------- |
| name        | String                      |
| language    | [Language](#enum-language)  |
| media       | String                      |
| yield       | Number                      |
| ingredients | [String]                    |

### `object` ModalOptions


| Property        | Default                | Type                     | Description |
| --------------- | ---------------------- | ------------------------ | ----------- |
| recipes*        | `None`                 | [[Recipe](#type-recipe)] | Array of recipes to add to the shoppinglist |
| partnerId*      | `None`                 | String                   | Id of the warehouse we want to productize the recipes in |
| externalUrl     | `window.location.href` | String                   | The url where the recipes where selected |
| warehouseId     | `None`                 | String                   | Id of the warehouse we want to productize the recipes in |

### `object` ButtonOptions

| Property           | Default            | Type                             | Description |
| ------------------ | ------------------ | -------------------------------- | ----------- |
| type               | single_recipe      | [[ButtonType](#enum-buttontype)] | The type of button to render |
| selectorOrElement  | `None`             | String \| Element                | Where the button should render |
| recipes*           | `None`             | [[Recipe](#type-recipe)]         | Array of recipes to add to the shoppinglist |

### `enum` Language

| Property | Value |
| -------- | ----- |
| NL       | `nl`  |
| FR       | `fr`  |
| EN       | `en`  |

### `enum` ButtonType

| Property      | Value            |
| ------------- | ---------------- |
| SINGLE_RECIPE | `single_recipe`  |

### `object` GenericEventData

| Property    | Type                        |
| ----------- | --------------------------- |
| name        | String                      |
| id          | String                      |

---
_Source: https://npm.io/package/@smartwithfood/js-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
