Skip to main content

TypeScript SDK

A 100% type-safe TypeScript SDK for the CommerceEngine Storefront API — automatic token management, universal compatibility (browser, Node.js, Next.js, TanStack Start, Astro, SvelteKit), and production-ready error handling.

Package

@commercengine/storefront

Universal TypeScript SDK with first-class bindings for React, Next.js, TanStack Start, Astro, SvelteKit, and Node.js.

Pick your integration

https://svgl.app/library/react_light.svg

React

Context providers, hooks, and component patterns for React SPAs.
https://svgl.app/library/nextjs_icon_dark.svg

Next.js

SSR, Server Actions, static rendering, and cookie-backed sessions.
https://svgl.app/library/tanstack.svg

TanStack Start

Server functions, route loaders, and pre-rendering.
https://svgl.app/library/astro-icon-light.svg

Astro

SSR pages, API routes, middleware, and static prerendering.
https://svgl.app/library/svelte.svg

SvelteKit

Server load functions, form actions, hooks, and universal loads.
https://svgl.app/library/nodejs.svg

Node.js

Express middleware, background jobs, and server-side patterns.

Installation

npm install @commercengine/storefront

Quick Start

1

Initialize the SDK

lib/storefront.ts
import { createStorefront, Environment, BrowserTokenStorage } from "@commercengine/storefront";

const storefront = createStorefront({
  storeId: "your-store-id",
  environment: Environment.Staging, // or Environment.Production
  apiKey: "your-api-key",
  session: {
    tokenStorage: new BrowserTokenStorage("myapp_"),
  },
});
2

Browse products (public — no auth needed)

const { data, error } = await storefront.public().catalog.listProducts({
  page: 1,
  limit: 20,
});

if (error) {
  console.error("Failed:", error.message);
} else {
  console.log(`Found ${data?.products.length} products`);
}
3

Start a session for cart, auth, and orders

const sdk = storefront.session();
await sdk.ensureAccessToken(); // Bootstraps anonymous session

const { data: cart } = await sdk.cart.createCart({
  items: [{ product_id: "prod_123", variant_id: null, quantity: 1 }],
});
console.log("Cart created:", cart?.cart.id);
That’s it — you’re making API calls. The SDK handles token refresh, error typing, and session management automatically.

Learn More

Client Overview

Public/session split, available clients, and the SDK architecture.

Configuration

All config options, environments, custom base URLs, and debugging.

Token Management

Automatic token refresh, storage options, and auth lifecycle.

Type Safety

Auto-generated types, IntelliSense, and the { data, error } pattern.