React SDK overview

Learn about Monite's React SDK and its components

Overview

Monite’s React SDK is a library of ready-to-use React components that are connected to the Monite API. These components can render and operate with data served by the API, such as payables, counterparts, and others. Monite partners can use UI Components to build React-based web applications for their client SMEs, powered by the Monite API.

A preview of the 'Counterparts' component
A preview of the 'Counterparts' component

Supported browsers

Monite UI Components support the five latest versions of major browsers: Chrome, Firefox, Safari, Edge, Opera.

React SDK components responsiveness

As of this release, the Monite React SDK is optimized for desktop use and may not provide the desired responsiveness on mobile and tablet views. We are actively working to enhance support for these platforms in future updates.

Installation

Before using Monite React components, you must first install the React SDK and API SDK packages available on the NPM and Yarn directories. To install:

1npm install @monite/sdk-react

The command installs the necessary packages for using Monite’s React SDK in your application.

Usage

The example below displays a list of an entity’s counterparts. The MoniteProvider element serves as a wrapper for other Monite-connected components. Any other components imported from @team-monite/ui-widgets-react must be placed inside this element.

Get the credentials

SDK credentials

The Monite React SDK accepts only entity-user tokens as credentials within the fetchToken function. Attempting to use a partner token with the React SDK will throw an error. For more information about entity user tokens and permissions required for each component, see Generate an entity user token and List of permissions.

Before using components from the React SDK, complete the Getting started guide to set up your partner account and get API the credentials.

We also assume you have already mapped out your customers and their users as entities and entity users in Monite, and that you have the ability to:

  • look up the Monite entity user ID for the user who is logged in to your application;
  • look up the Monite entity ID to which the user belongs.

When an entity user logs in to your application, look up the entity ID and user ID for that user, and call Monite’s POST /auth/token endpoint to generate a Monite access token for that user.

Once you have a user access token and an entity ID, you can initialize the Monite client and components.

Install the packages

1npm install @monite/sdk-react

Import the packages

You need to import MoniteSDK, MoniteProvider, and any other components that you want to use (such as CounterpartsTable):

App.js
1import { MoniteSDK } from "@monite/sdk-api";
2import { MoniteProvider, CounterpartsTable } from "@monite/sdk-react";

Create the fetchToken function

The fetchToken function is used to get authorization tokens. You can create it at the backend side, but for illustration let’s do that at frontend:

App.js
1const fetchToken = async () => {
2 const response = await fetch("https://api.sandbox.monite.com/v1/auth/token", {
3 method: "POST",
4 headers: {
5 "Content-Type": "application/json",
6 "x-monite-version": "2024-01-31",
7 },
8 // TODO: Replace the following fields with your Monite credentials
9 body: JSON.stringify({
10 grant_type: "entity_user",
11 client_id: "SECRET_CLIENT_ID",
12 client_secret: "SECRET_CLIENT_SECRET",
13 entity_user_id: "SECRET_ENTITY_USER_ID",
14 }),
15 });
16
17 return response.json();
18};

Monite does not recommend exposing the credentials of your fetchToken function on the client side as shown earlier. For security reasons, we recommend that all Monite tokens are generated from your server side code.

Create a MoniteSDK instance

App.js
1const monite = new MoniteSDK({
2 apiUrl: "https://api.sandbox.monite.com/v1", // Or 'https://api.monite.com/v1' to use Production
3 entityId: "ENTITY_ID", // Monite entity to which the user belongs
4
5 /** Entity whose data you want to access */
6 fetchToken: fetchToken,
7});

Embed MoniteProvider

The MoniteProvider is the root component that must wrap all other Monite-connected components. The wrapper provides extra configuration for all Monite components beneath it.

App.js
1return (
2 <MoniteProvider
3 monite={monite}
4 locale={{
5 code: "en-US", // default: de-DE
6 }}
7 >
8 ...
9 </MoniteProvider>
10);

The locale prop is an optional configuration object that handles the localization mechanism for the UI components. With this, you can customize translations, currency formatting and usage of delimiters and decimal separators. For more information, see Localization.

Add components

In this example, we use the CounterpartsTable component to display a list of an entity’s counterparts:

App.js
1<MoniteProvider monite={monite} locale={{ code: "en-US" }}>
2 <CounterpartsTable />
3</MoniteProvider>

Review the result

If you followed this tutorial, your code should look like this:

App.js
1import { MoniteSDK } from "@monite/sdk-api";
2import { MoniteProvider, CounterpartsTable } from "@monite/sdk-react";
3
4// Define a function that retrieves access tokens
5const fetchToken = async () => {
6 const response = await fetch("https://api.sandbox.monite.com/v1/auth/token", {
7 method: "POST",
8 headers: {
9 "Content-Type": "application/json",
10 "x-monite-version": "2024-01-31", //Monite API version
11 },
12 body: JSON.stringify({
13 grant_type: "entity_user",
14 client_id: "SECRET_CLIENT_ID",
15 client_secret: "SECRET_CLIENT_SECRET",
16 entity_user_id: "SECRET_ENTITY_USER_ID",
17 }),
18 });
19
20 return await response.json();
21};
22
23// Initialize the SDK
24const monite = new MoniteSDK({
25 apiUrl: "https://api.sandbox.monite.com/v1", // Or 'https://api.monite.com/v1' to use Production
26 entityId: "ENTITY_ID",
27 fetchToken: fetchToken,
28});
29
30function App() {
31 return (
32 <MoniteProvider monite={monite} locale={{ code: "en-US" }}>
33 <CounterpartsTable />
34 </MoniteProvider>
35 );
36}
37
38export default App;

Run the application

1npm run start