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.
npm install @finopsbricks/fob-mdm
It needs Node.js 18 or later.
Create a client
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 |
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 |
objects.findBySlug(slug) | List Objects with slug |
objects.get(id) | Get Object |
objects.create({ slug, label }) | Create Object |
objects.update(id, { label }) | Update Object |
objects.delete(id) | Delete Object |
fields
| Method | API |
|---|---|
fields.list(object, params?), fields.getAll(object, params?) | List Fields |
fields.get(id) | Get Field |
fields.create(object, { key, label, type?, required?, is_identity? }) | Create Field |
fields.update(id, data) | Update Field |
fields.retire(id) | Retire Field |
records
| Method | API |
|---|---|
records.list({ object, ...params }), records.getAll({ object, ...params }) | List Records |
records.get(id) | Get Record |
records.create(object, values?) | Create Record |
records.setValues(id, values) | Set Record Values |
records.history(id, params?), records.historyAll(id, params?) | List Record History |
records.delete(id) | Delete Record |
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 |
integrations.get(id) | Get Integration |
integrations.create({ name, type, config }) | Create Integration |
integrations.update(id, data) | Update Integration |
integrations.delete(id) | Delete Integration |
integrations.pull(id) | Pull Records |
integrations.lookups(id, params?), integrations.lookupsAll(id, params?) | List Record Links |
contracts
| Method | API |
|---|---|
contracts.list(params?), contracts.getAll(params?) | List Contracts |
contracts.get(id) | Get Contract |
contracts.create({ integration, field, must_match?, enforcement_mode? }) | Create Contract |
contracts.update(id, data) | Update Contract |
contracts.delete(id) | Delete Contract |
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 |
conformance.states(params?), conformance.statesAll(params?) | List States |
conformance.enforce(state) | Enforce |
conformance.confirm(state) | Confirm Fix |
conformance.events(params?), conformance.eventsAll(params?) | List Enforcement Events |
Errors
A failed call throws an ApiError with the API's error on it:
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: the same calls from the terminal
- API Authentication: creating a key, and its permissions
Using an AI agent? Give it the raw Markdown of this page, or the docs index.