> ## Documentation Index
> Fetch the complete documentation index at: https://integrations.docs.commenda.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Typescript SDK

> Install RootFi Typescript server-side SDK to interact with RootFi' APIs

## Installation

RootFi' server-side helper libraries (also known as server-side SDKs) reduce the amount of work required to use RootFi' 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 RootFi SDK in your preferred language:

<CodeGroup>
  ```bash NodeJS theme={null}
  npm i rootfi-api
  ```
</CodeGroup>

Next, import the RootFi client in your application code:

<CodeGroup>
  ```javascript NodeJS theme={null}
  import { RootFiClient } from "rootfi-api";
  ```
</CodeGroup>

***

## Instantiate RootFi

In your server file, instantiate the RootFi instance with your `apiKey`. You should generate the API keys on the [RootFi Dashboard](https://integrate.rootfi.dev/settings) and add them here.

<CodeGroup>
  ```javascript NodeJS theme={null}
  const rootfi = new RootFiClient({ apiKey: "<api_key>", environment: RootFiEnvironment.global });
  // default environment is RootFiEnvironment.global
  // For the KSA region use RootFiEnvironment.saudi
  ```
</CodeGroup>

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 RootFi's APIs.

##### 1. [Core API](/api-reference/v3/core/companies/get-all-companies)

##### 2. [Unified API](/api-reference/v3/accounting/accounts/get-all-accounts)

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`:

<CodeGroup>
  ```javascript NodeJS theme={null}
  // Create an Invite Link
  const data = await rootfi.core.inviteLinks.create({
    company_name: "Test Company",
    integration_categories: ["ACCOUNTING"]
    integrations: ["ZOHO_BOOKS"],
  });
  ```
</CodeGroup>

To demonstrate Core APIs for fetching a list of all companies, you can use the following JavaScript code snippet:

<CodeGroup>
  ```javascript NodeJS theme={null}
  // Get All Companies
  const data = await rootfi.core.companies.list({});
  ```
</CodeGroup>

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:

<CodeGroup>
  ```javascript NodeJS theme={null}
  // Fetch all accounts for your connection
  const data = await rootfi.accounting.accounts.list({
    filter: {
      rootfi_company_id: { eq: 1323 },
      rootfi_updated_at: { gt: "2023-05-12" },
    },
  });
  ```
</CodeGroup>

***

## Error Handling

The SDK throws an error when the API request fails. You can catch the error and handle it as shown below:

<CodeGroup>
  ```javascript NodeJS theme={null}
    try {
        const rootfiClient = new RootFiClient({ apiKey: "***" });
        await rootfiClient.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
      }
  ```
</CodeGroup>
