Skip to content

js-api-client

Terminal window
npx skills add https://github.com/crystallizeapi/ai --skill js-api-client

Crystallize JS API Client

The @crystallize/js-api-client package provides typed utilities for working with all Crystallize APIs in JavaScript/TypeScript environments.

Consultation Approach

Before writing code, understand the context. Ask clarifying questions:

  1. What are you trying to do? Read data, write data, manage carts, handle webhooks?
  2. Do you need raw GraphQL or a high-level helper? Helpers reduce boilerplate for common workflows (orders, carts, navigation). Use raw GraphQL via API callers for custom queries or when helpers don’t cover the use case.
  3. Which API? Catalogue/Discovery for storefront reads, PIM for admin writes, Shop Cart for checkout flows.
  4. What auth do you have? staticAuthToken is enough for read-only. PIM/Shop operations need accessTokenId + accessTokenSecret.
  5. Is this server-side or client-side? The client works in both, but credentials should only live server-side.

Installation

Terminal window
pnpm add @crystallize/js-api-client
# or npm install @crystallize/js-api-client
# or yarn add @crystallize/js-api-client

Quick Start

import { createClient } from "@crystallize/js-api-client";
const api = createClient({
tenantIdentifier: "your-tenant",
// For protected APIs, provide credentials:
// accessTokenId: '…',
// accessTokenSecret: '…',
// staticAuthToken: '…', // for read-only catalogue/discovery
});
// Call any GraphQL API with string queries
const { catalogue } = await api.catalogueApi(
`query Q($path: String!, $language: String!) {
catalogue(path: $path, language: $language) {
name
path
}
}`,
{ path: "/shop", language: "en" },
);
// Close when using HTTP/2 option
api.close();

Configuration

createClient(configuration, options?)

Configuration Options

OptionDescription
tenantIdentifierRequired. Your tenant name
tenantIdOptional tenant ID
accessTokenId / accessTokenSecretFor PIM/Shop operations
sessionIdAlternative to token-based auth
staticAuthTokenFor read-only catalogue/discovery
shopApiTokenAuto-fetched if not provided
shopApiStagingUse staging Shop API
originCustom host suffix (default: .crystallize.com)

Client Options

OptionDescription
useHttp2Enable HTTP/2 transport
profilingProfiling callbacks for debugging
extraHeadersExtra request headers for all calls
shopApiTokenControl auto-fetch behavior

API Callers

All callers share the same signature:

<T>(query: string, variables?: Record<string, unknown>) => Promise<T>;
CallerPurpose
catalogueApiCatalogue GraphQL
discoveryApiDiscovery GraphQL (search/browse)
pimApiPIM GraphQL (legacy — prefer nextPimApi)
nextPimApiPIM Next GraphQL (scoped to tenant, recommended)
shopCartApiShop Cart GraphQL (token auto-handled)

High-Level Helpers

Available helpers (see High-Level Helpers Reference for full examples):

HelperImportPurpose
createCatalogueFetcher@crystallize/js-api-clientBuild typed catalogue queries with object syntax
createNavigationFetcher@crystallize/js-api-clientFetch navigation trees by depth
createProductHydrater@crystallize/js-api-clientFetch products/variants by path or SKU
createOrderFetcher@crystallize/js-api-clientFetch orders with type-safe field selection
createOrderManager@crystallize/js-api-clientCreate/update orders, set payments, move pipeline stages
createCustomerManager@crystallize/js-api-clientCreate and update customers
createCustomerGroupManager@crystallize/js-api-clientManage customer groups
createSubscriptionContractManager@crystallize/js-api-clientCreate and manage subscription contracts
createCartManager@crystallize/js-api-clientHydrate carts, add items, place orders (token auto-handled)

Authentication

Auth TypeUse Case
staticAuthTokenRead-only catalogue/discovery
accessTokenId + accessTokenSecretPIM/Shop operations
sessionIdAlternative to token pair
shopApiTokenOptional; auto-fetched if omitted

Generate access tokens in the Crystallize App: Settings → Access Tokens

References


Crystallize AI