For the complete documentation index, see llms.txt. This page is also available as Markdown.

Use examples

These patterns show how to use @lightspeed/ecom-headless in Vue composables within a Crane theme.

All examples below assume you have a shared init composable in your project at shared/composables/useInitStorefrontApi.ts:

// shared/composables/useInitStorefrontApi.ts
import { useInstantsiteJsApi } from '@lightspeed/crane'
import { initStorefrontApi } from '@lightspeed/ecom-headless'

const ECWID_CONFIG = {
  clientId: 'your-app-client-id',
  baseURL: undefined, // only set for non-production environments
}

export async function useInitStorefrontApi() {
  const publicToken = useInstantsiteJsApi()?.getAppPublicToken(ECWID_CONFIG.clientId) ?? ''
  const storeId = useInstantsiteJsApi()?.getSiteId()

  if (!publicToken) {
    throw new Error('Missing public token — ensure the app is installed and has a valid client ID')
  }

  await initStorefrontApi({
    publicToken,
    storeId,
    ...(ECWID_CONFIG.baseURL ? { baseURL: ECWID_CONFIG.baseURL } : {}),
  })

  return { publicToken, storeId }
}

This calls initStorefrontApi once with credentials from the Crane runtime. All composables below call it before making API requests. See Overview & Setup for what each config field does.

Fetching Products

A composable that fetches products on mount and provides reactive data:

Key patterns:

  • Always call useInitStorefrontApi() before API calls

  • Use response.items || [] defensively

  • Pass enabled: true to only get published products

  • Pass lang for localized content

  • Map raw API data to a view-friendly shape in a computed

Fetching Categories

Same pattern as products, adapted for categories:

Last updated

Was this helpful?