Skip to main content

Installation

Commenda’s server-side helper libraries (also known as server-side SDKs) reduce the amount of work required to use Commenda’s REST APIs, starting with reducing the boilerplate code you have to write. Below are the installation instructions for these libraries in a variety of popular server-side programming languages. Open your project folder and run the following bash command on your terminal to install the Commenda SDK in your preferred language:
npm i rootfi-api
Next, import the Commenda client in your application code:
import { RootFiClient } from "rootfi-api";

Instantiate Commenda Client

In your server file, instantiate the Commenda instance with your apiKey. You should generate the API keys on the Commenda Dashboard and add them here.
const client = new RootFiClient({ apiKey: "<api_key>", environment: RootFiEnvironment.global });
// default environment is RootFiEnvironment.global
// For the KSA region use RootFiEnvironment.saudi
The resources can be accessed using the instance. All the methods invocations follow the namespaced signature:

Run your first SDK request

Now that you have the Node.js SDK installed, you can access Commenda’s APIs.
1. Core API
2. Unified API
To demonstrate Core APIs, we use the following JavaScript code snippet to make a Create request for creating an invite link. In this example, we create a connect link while specifying the company name, integration_categories and integrations:
// Create an Invite Link
const data = await client.core.inviteLinks.create({
  company_name: "Test Company",
  integration_categories: ["ACCOUNTING", "PAYMENTS"]
  integrations: ["ZOHO_BOOKS", "STRIPE"],
});
To demonstrate Core APIs for fetching a list of all companies, you can use the following JavaScript code snippet:
// Get All Companies
const data = await client.core.companies.list({});
To demonstrate Unified Accounting APIs, we are using the following JavaScript code snippet to make a List request for accounting data. In this example, we fetch all accounts for a specific connection based on certain filters:
// Fetch all accounts for your connection
const data = await client.accounting.accounts.list({
  filter: {
    rootfi_company_id: { eq: 1323 },
    rootfi_updated_at: { gt: "2023-05-12" },
  },
});
To demonstrate the V4 Payments API with the unified Transaction model:
// Fetch all transactions (payments, refunds, credit notes)
const transactions = await client.payments.transactions.list({
  filter: {
    rootfi_company_id: { eq: 1323 },
    type: { eq: "payment" }, // Filter by transaction type
  },
});

// Create a refund transaction
const refund = await client.payments.transactions.create({
  type: "refund",
  contact_id: "cust_123",
  currency_id: "USD",
  amount: 50.00,
  original_transaction_id: "txn_456",
  reason: "customer_request",
});

Error Handling

The SDK throws an error when the API request fails. You can catch the error and handle it as shown below:
  try {
      const client = new RootFiClient({ apiKey: "***" });
      await client.core.syncs.create({...});
    } catch (e: any) {
      const error = e as RootFiError;
      const statusCode = error.statusCode;
      const errorResponse = (error.body as any)?.error;

      const { code, field, message } = errorResponse;

      // Handle the error based on Error Code
    }