JavaScript SDK

The callback-based merchi_sdk_js package uses getter/setter methods on entities. It differs from the promise/property interface of the TypeScript SDK. JavaScript applications can also use the compiled TypeScript SDK when promises are preferred.

This guide follows JavaScript SDK commit f6bf63d. Check the installed version before using newer helpers.

Installation and initialization

npm install merchi_sdk_js
import { merchi as createMerchi } from 'merchi_sdk_js';
const merchi = createMerchi('https://api.merchi.co/');

The factory configures shared/global SDK state, including the backend URI and session. Avoid configuring concurrent clients for different backends or users in the same global context. Do not add /v6/ to the base URL: the request layer supplies it. Use a package-aware bundler for browser applications. The package entry is an ES module; do not assume an unverified legacy CDN bundle exports the same interface.

Authentication

Public reads and authenticated operations have different access policies; there is no universal dashboard-generated API key required by every endpoint. merchi.initSessionByToken(token, success, error, embed) initializes a session from an existing session token, and getCurrentUser can use the session cookie. Obtain tokens through your application’s login flow and keep staff credentials out of public bundles. The API still enforces origin/domain/model permissions. See REST authentication.

Getter/setter methods

const product = new merchi.Product();
product.name('New name');
console.log(product.name());

Setters return the entity for chaining; getters return its value. Use product.id(123), not product.id = 123. Entity-specific properties and methods are declared in the SDK source.

List, search and paginate

function onError(...args) { console.error(...args); }
merchi.products.get((products) => {
  for (const product of products) console.log(product.id(), product.name());
  console.log(products.meta.available, products.meta.offset);
}, onError, { limit: 20, offset: 0, q: 'shirt', embed: { domain: {} } });

The success callback receives an array of entities with an attached .meta object (available, count, canCreate, limit, offset). This is different from the TypeScript SDK’s {items, metadata} result. Pagination is controlled through the third argument. Advance the offset by the number of received items; stop when a page is empty/short and account for collection changes between requests.

Use q for supported server-side search. Filters are model-specific; see the filter reference. The legacy SDK forwards a defined set of parameters in getList; a newly added backend filter may need direct REST access or an SDK update rather than simply adding an unknown option.

Fetch, create, edit and delete

const product = new merchi.Product();
product.id(123);
product.get((loaded) => {
  console.log(loaded.name());
}, onError, { domain: {} });

The instance get(success, error, embed) loads an existing record. For an authorized update:

product.name('Updated product name');
product.patch((updated) => console.log(updated.id()), onError);

patch uses PATCH. Do not call a TypeScript-style save() on this Product interface. Creation uses product.create(success, error, embed, asDomain) and requires the fields required by the target model/product type. For example, set product.domain(new merchi.Domain().id(domainId)) alongside the other required fields before creating. The domain must exist and the caller must have creation rights.

product.destroy(success, error) calls DELETE for the selected product. Invoke it deliberately only on an authorized record; model restrictions and archival behavior still apply. Callback signatures vary between actions: inspect the specific helper rather than assuming every error callback receives an Error instance.

Nested entities, relationships and embedding

Embeds are nested objects such as { domain: { company: {} } }. An embedded relation becomes an SDK entity with its own accessor methods. Missing embeds can leave relations absent: check before reading them. Related collections use arrays of entities.

Use entity constructors for nested values, not arbitrary plain objects, when the SDK serializer expects a relationship. Existing nested entities need IDs; new ones need their model-required fields. Nested creation and deletion depend on each API model. For an existing child, use its own patch helper where the endpoint requires a separate update. Do not assume assigning null or an empty array is allowed for every relationship.

Jobs, assignments and custom actions

Jobs expose fields and workflow helpers beyond generic CRUD. Product selection, variations, assignments, quote calculation, inventory and shipment actions have distinct validation and side effects. Consult job.js and assignment.js before invoking an action. Do not infer job completion or payable totals by summing a subset of local fields; the server owns those calculations and states.

For new endpoints missing from this SDK, follow the REST guide and API reference. A missing SDK helper does not imply the backend route is unavailable, and an old helper does not guarantee that a server still accepts its payload.