Loading...
Loading...
Set up the Commerce Engine TypeScript SDK in any project. Framework detection, token storage selection, environment variables, and migration guidance.
npx skill4agent add commercengine/skills ce-setupLLM Docs Header: All requests tomust include thehttps://llm-docs.commercengine.ioheader (or appendAccept: text/markdownto the URL path). Without it, responses return HTML instead of parseable markdown..md
| Step | Action |
|---|---|
| 1. Detect framework | Check |
| 2. Install SDKs | |
| 3. Choose token storage | |
| 4. Set env vars | |
| 5. Initialize SDK | Create |
| 6. Get anonymous token | |
| 7. Hosted Checkout (if used) | Initialize checkout with |
authMode: "provided"updateTokens(...)onTokensUpdatedimport StorefrontSDK, { BrowserTokenStorage } from "@commercengine/storefront-sdk";
import { initCheckout, getCheckout } from "@commercengine/checkout";
const tokenStorage = new BrowserTokenStorage("myapp_");
export const storefront = new StorefrontSDK({
storeId: import.meta.env.VITE_STORE_ID,
apiKey: import.meta.env.VITE_API_KEY,
tokenStorage,
onTokensUpdated: (accessToken, refreshToken) => {
// SDK -> checkout
getCheckout().updateTokens(accessToken, refreshToken);
},
});
const accessToken = await tokenStorage.getAccessToken();
const refreshToken = await tokenStorage.getRefreshToken();
initCheckout({
storeId: import.meta.env.VITE_STORE_ID,
apiKey: import.meta.env.VITE_API_KEY,
authMode: "provided",
accessToken: accessToken ?? undefined,
refreshToken: refreshToken ?? undefined,
onTokensUpdated: ({ accessToken, refreshToken }) => {
// checkout -> SDK
storefront.setTokens(accessToken, refreshToken);
},
});package.json| Indicator | Framework | SDK Package | Token Storage |
|---|---|---|---|
| Next.js | | |
| React SPA | | |
| Vue SPA | | |
| Svelte/SvelteKit | | |
| Solid | | |
| Express/Node.js | | |
| None of above | Vanilla JS | | |
User Request: "Set up Commerce Engine" / "Add e-commerce"
│
├─ Read package.json + config files
│
├─ Next.js detected?
│ ├─ YES → Install @commercengine/storefront-sdk-nextjs
│ │ → Root layout: storefront({ isRootLayout: true }) + StorefrontSDKInitializer
│ │ → See ce-nextjs-patterns for advanced usage
│ └─ NO → Install @commercengine/storefront-sdk
│
├─ Choose token storage based on framework
│
├─ Using Hosted Checkout?
│ ├─ YES → Install @commercengine/checkout
│ │ → Initialize checkout with authMode: "provided"
│ │ → Wire two-way token sync (SDK ↔ checkout)
│ └─ NO → SDK-only setup
│
├─ Set environment variables
│
└─ Initialize SDK + get anonymous tokennpm install @commercengine/storefront-sdk-nextjs// lib/storefront.ts
export { storefront } from "@commercengine/storefront-sdk-nextjs";// app/layout.tsx
import { StorefrontSDKInitializer } from "@commercengine/storefront-sdk-nextjs/client";
import { storefront } from "@/lib/storefront";
// Root Layout has no request context — use isRootLayout flag
const sdk = storefront({ isRootLayout: true });
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<StorefrontSDKInitializer />
{children}
</body>
</html>
);
}For the fullmental model (server components, client components, server actions, SSG), seestorefront().ce-nextjs-patternsUsing Hosted Checkout? Replace the simple re-export inwithlib/storefront.tsto wire two-way token sync. SeecreateStorefront()→ce-cart-checkout§ "Next.js".references/hosted-checkout.md
# .env.local
NEXT_PUBLIC_STORE_ID=your-store-id
NEXT_PUBLIC_API_KEY=your-api-keynpm install @commercengine/storefront-sdk// lib/storefront.ts
import StorefrontSDK, { Environment, BrowserTokenStorage } from "@commercengine/storefront-sdk";
export const sdk = new StorefrontSDK({
storeId: import.meta.env.VITE_STORE_ID,
environment: import.meta.env.PROD ? Environment.Production : Environment.Staging,
apiKey: import.meta.env.VITE_API_KEY,
tokenStorage: new BrowserTokenStorage("myapp_"),
});# .env
VITE_STORE_ID=your-store-id
VITE_API_KEY=your-api-keynpm install @commercengine/storefront-sdk// src/lib/storefront.ts
import StorefrontSDK, { Environment, MemoryTokenStorage } from "@commercengine/storefront-sdk";
const sdk = new StorefrontSDK({
storeId: process.env.CE_STORE_ID!,
environment: process.env.NODE_ENV === "production" ? Environment.Production : Environment.Staging,
apiKey: process.env.CE_API_KEY!,
tokenStorage: new MemoryTokenStorage(),
timeout: 30000,
});
export default sdk;| Storage | Use Case | Persistence | SSR Safe |
|---|---|---|---|
| SPAs (React, Vue, Svelte, Solid) | localStorage | No |
| SSR frameworks (Next.js) | Cookies | Yes |
| Server-side (Node.js, Express) | In-memory | Yes |
// Every visitor starts as anonymous
const { data, error } = await sdk.auth.getAnonymousToken();
if (error) {
console.error("Auth failed:", error.message);
} else {
// Tokens are automatically managed by the SDK
// Now you can call any API
const { data: products } = await sdk.catalog.listProducts();
}| Variable | Required | Description |
|---|---|---|
| Yes | Your store identifier |
| Yes | Storefront API key (safe for client-side) |
| No | Set |
defaultHeadersconst sdk = new StorefrontSDK({
storeId: "...",
apiKey: "...",
tokenStorage: new BrowserTokenStorage("myapp_"),
defaultHeaders: {
customer_group_id: "01JHS28V83KDWTRBXXJQRTEKA0",
},
});customer_group_idcatalog/| Level | Issue | Solution |
|---|---|---|
| CRITICAL | Missing anonymous auth | Must call |
| CRITICAL | App uses Storefront SDK auth + Hosted Checkout | Use |
| CRITICAL | Exposing admin API keys | Only storefront API keys ( |
| HIGH | Wrong token storage for SSR | Use |
| HIGH | Missing env vars | SDK throws if |
| MEDIUM | Missing | Required in Next.js root layout for automatic anonymous auth |
| MEDIUM | Wrong environment | Use |
auth/nextjs-patterns/storefront()cart-checkout/