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 callsUse
response.items || []defensivelyPass
enabled: trueto only get published productsPass
langfor localized contentMap 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?
