
# JavaScript Client

The `@finopsbricks/fob-mdm` package is also a client library. The CLI is built on it, so every command has a method here that makes the same call.

```bash
npm install @finopsbricks/fob-mdm
```

It needs Node.js 18 or later.

## Create a client

```js
import { fobMdm } from '@finopsbricks/fob-mdm';

const mdm = fobMdm({
  api_key: process.env.MDM_API_KEY,
  api_secret: process.env.MDM_API_SECRET,
});

const vendor = await mdm.objects.findBySlug('vendor');
const { data } = await mdm.records.list({ object: vendor.id, 'values.gstin': '29ABCDE1234F1Z5' });
await mdm.records.setValues(data[0].id, { bank_ifsc: 'HDFC0005678' });
```

You pass the key and secret in. The client doesn't read environment variables or the CLI's profiles itself, so your code decides where they come from. `api_url` is optional and defaults to `https://mdm.finopsbricks.com`.

## What methods return

| Kind of method | Returns |
|---|---|
| `get`, `create`, `update`, `setValues`, `pull`, `reconcile`, `enforce`, `confirm`, `findBySlug` | The record itself, without the `data` envelope |
| `delete` | `null` |
| `list`, `history`, `lookups`, `states`, `events` | One page: `{ data, page_context }`, as in [Pagination](/docs/api/pagination) |
| `getAll`, `historyAll`, `lookupsAll`, `statesAll`, `eventsAll` | Every page: `{ data, page_context, truncated }` |

The all-pages methods fetch 500 rows a page and stop at 15,000 rows, with `truncated: true` if there were more. They ignore `page` and `limit`.

`findBySlug` returns `null` when there's no object with that slug.

## Methods

Ids can be numbers or strings. Query parameters and request bodies are the API's, so each method's API page lists them.

### objects

| Method | API |
|---|---|
| `objects.list(params?)`, `objects.getAll(params?)` | [List Objects](/docs/api/endpoints/objects/list) |
| `objects.findBySlug(slug)` | [List Objects](/docs/api/endpoints/objects/list) with `slug` |
| `objects.get(id)` | [Get Object](/docs/api/endpoints/objects/get) |
| `objects.create({ slug, label })` | [Create Object](/docs/api/endpoints/objects/create) |
| `objects.update(id, { label })` | [Update Object](/docs/api/endpoints/objects/update) |
| `objects.delete(id)` | [Delete Object](/docs/api/endpoints/objects/delete) |

### fields

| Method | API |
|---|---|
| `fields.list(object, params?)`, `fields.getAll(object, params?)` | [List Fields](/docs/api/endpoints/fields/list) |
| `fields.get(id)` | [Get Field](/docs/api/endpoints/fields/get) |
| `fields.create(object, { key, label, type?, required?, is_identity? })` | [Create Field](/docs/api/endpoints/fields/create) |
| `fields.update(id, data)` | [Update Field](/docs/api/endpoints/fields/update) |
| `fields.retire(id)` | [Retire Field](/docs/api/endpoints/fields/delete) |

### records

| Method | API |
|---|---|
| `records.list({ object, ...params })`, `records.getAll({ object, ...params })` | [List Records](/docs/api/endpoints/records/list) |
| `records.get(id)` | [Get Record](/docs/api/endpoints/records/get) |
| `records.create(object, values?)` | [Create Record](/docs/api/endpoints/records/create) |
| `records.setValues(id, values)` | [Set Record Values](/docs/api/endpoints/records/set-values) |
| `records.history(id, params?)`, `records.historyAll(id, params?)` | [List Record History](/docs/api/endpoints/records/transitions) |
| `records.delete(id)` | [Delete Record](/docs/api/endpoints/records/delete) |

`values` is an object keyed by field key: `{ bank_ifsc: 'HDFC0005678' }`. `setValues` returns the record with a `changes` array of what actually changed.

### integrations

| Method | API |
|---|---|
| `integrations.list(params?)`, `integrations.getAll(params?)` | [List Integrations](/docs/api/endpoints/integrations/list) |
| `integrations.get(id)` | [Get Integration](/docs/api/endpoints/integrations/get) |
| `integrations.create({ name, type, config })` | [Create Integration](/docs/api/endpoints/integrations/create) |
| `integrations.update(id, data)` | [Update Integration](/docs/api/endpoints/integrations/update) |
| `integrations.delete(id)` | [Delete Integration](/docs/api/endpoints/integrations/delete) |
| `integrations.pull(id)` | [Pull Records](/docs/api/endpoints/integrations/pull) |
| `integrations.lookups(id, params?)`, `integrations.lookupsAll(id, params?)` | [List Record Links](/docs/api/endpoints/integrations/lookups) |

### contracts

| Method | API |
|---|---|
| `contracts.list(params?)`, `contracts.getAll(params?)` | [List Contracts](/docs/api/endpoints/conformance-contracts/list) |
| `contracts.get(id)` | [Get Contract](/docs/api/endpoints/conformance-contracts/get) |
| `contracts.create({ integration, field, must_match?, enforcement_mode? })` | [Create Contract](/docs/api/endpoints/conformance-contracts/create) |
| `contracts.update(id, data)` | [Update Contract](/docs/api/endpoints/conformance-contracts/update) |
| `contracts.delete(id)` | [Delete Contract](/docs/api/endpoints/conformance-contracts/delete) |

`field` is a field id here. The CLI also takes a field key, and looks up the id for you.

### conformance

| Method | API |
|---|---|
| `conformance.reconcile(integration?)` | [Reconcile](/docs/api/endpoints/conformance/reconcile) |
| `conformance.states(params?)`, `conformance.statesAll(params?)` | [List States](/docs/api/endpoints/conformance/states) |
| `conformance.enforce(state)` | [Enforce](/docs/api/endpoints/conformance/enforce) |
| `conformance.confirm(state)` | [Confirm Fix](/docs/api/endpoints/conformance/confirm) |
| `conformance.events(params?)`, `conformance.eventsAll(params?)` | [List Enforcement Events](/docs/api/endpoints/conformance/events) |

## Errors

A failed call throws an `ApiError` with the API's [error](/docs/api/errors) on it:

```js
import { fobMdm, ApiError } from '@finopsbricks/fob-mdm';

try {
  await mdm.records.setValues(100, { ifsc: 'HDFC0005678' });
} catch (err) {
  if (!(err instanceof ApiError)) throw err;
  err.code;     // 'VALIDATION_ERROR'
  err.status;   // 400
  err.message;  // 'Unknown field: ifsc'
  err.details;  // { unknown: ['ifsc'], fields: ['name', 'gstin', 'bank_ifsc'] }
}
```

Besides the API's codes, `code` can be `NO_CREDENTIALS` (no key or secret was passed to `fobMdm()`), `INVALID_RESPONSE` (the response wasn't JSON) or `HTTP_ERROR` (an error with no error body).

## Related

- [Command-line Interface](/docs/cli): the same calls from the terminal
- [API Authentication](/docs/api/authentication): creating a key, and its permissions
