Add Expivi to a web page
In this tutorial you add an Expivi product to a web page: the product visualization on one side, its options on the other. Everything else in these tutorials builds on this page.
The finished code is the index.html page of the example apps: vanilla JS, Vue and React.
1. Create a project and install the SDK
Create a project with Vite and install @expivi/sdk. If you already have a project, only install the SDK.
- Vanilla JS
- Vue
- React
npm create vite@latest my-shop -- --template vanilla
cd my-shop
npm install @expivi/sdk
npm create vite@latest my-shop -- --template vue-ts
cd my-shop
npm install @expivi/sdk
npm create vite@latest my-shop -- --template react-ts
cd my-shop
npm install @expivi/sdk
The examples use React 19. Earlier React versions pass objects to web components as HTML attributes instead of properties, which doesn't work with the Expivi components.
2. Add your credentials
The configurator needs the URL of the Expivi API, an API token and the ID of the product to load. Put them in a .env file in the project root. Vite makes variables that start with VITE_ available to your code.
VITE_EXPIVI_API_BASE_URL=https://expivi.net/api/v2
VITE_EXPIVI_API_TOKEN=your-api-token
VITE_EXPIVI_PRODUCT_ID=your-product-id
# Optional: a locale enabled for your Expivi team, such as en-US or nl-NL.
# Leave it empty to use the team's default locale.
VITE_EXPIVI_LOCALE=
Everything in VITE_ variables ends up in the JavaScript that visitors download, so they can read your API token. Use the token meant for your public configurator, never a personal or admin token. Keep .env out of version control and commit a .env.example without real values instead.
Then read the variables in one place:
- Vanilla JS
- Vue
- React
export const expiviConfig = {
apiBaseUrl: import.meta.env.VITE_EXPIVI_API_BASE_URL || 'https://expivi.net/api/v2',
apiToken: import.meta.env.VITE_EXPIVI_API_TOKEN,
productId: import.meta.env.VITE_EXPIVI_PRODUCT_ID,
locale: import.meta.env.VITE_EXPIVI_LOCALE || undefined,
};
export const expiviConfig = {
apiBaseUrl: import.meta.env.VITE_EXPIVI_API_BASE_URL || 'https://expivi.net/api/v2',
apiToken: import.meta.env.VITE_EXPIVI_API_TOKEN ?? '',
productId: import.meta.env.VITE_EXPIVI_PRODUCT_ID ?? '',
locale: import.meta.env.VITE_EXPIVI_LOCALE || undefined,
};
export const expiviConfig = {
apiBaseUrl: import.meta.env.VITE_EXPIVI_API_BASE_URL || 'https://expivi.net/api/v2',
apiToken: import.meta.env.VITE_EXPIVI_API_TOKEN ?? '',
productId: import.meta.env.VITE_EXPIVI_PRODUCT_ID ?? '',
locale: import.meta.env.VITE_EXPIVI_LOCALE || undefined,
};
3. Load the 3D viewer
The 3D viewer is a script on Expivi's CDN. Add it to the <head> of your HTML page, before your own scripts:
<script src="https://assets.expivi.net/viewer/latest/viewer.js"></script>
The script registers window.expivi, which <xpv-product-visualizer> uses to render 3D products. Products with a 2D visualization or only images don't use it, but loading it on every product page means any product works.
4. Register the web components and load the theme
The Expivi UI consists of web components (<xpv-…> elements). Register them once, before you render them, and import the theme that styles them.
@expivi/sdk/ui-kit/componentscontains the option controls, such as tiles, lists and color pickers.@expivi/sdk/ui-composer/componentscontains the visualizer and the layout composer, which renders the options.
- Vanilla JS
- Vue
- React
import { defineCustomElements as defineUiKitElements } from '@expivi/sdk/ui-kit/components';
import { defineCustomElements as defineUiComposerElements } from '@expivi/sdk/ui-composer/components';
import '@expivi/sdk/theme/style.css';
import './style.css';
defineUiKitElements();
defineUiComposerElements();
import { createApp } from 'vue';
import { defineCustomElements as defineUiKitElements } from '@expivi/sdk/ui-kit/components';
import { defineCustomElements as defineUiComposerElements } from '@expivi/sdk/ui-composer/components';
import '@expivi/sdk/theme/style.css';
import './style.css';
import App from './App.vue';
defineUiKitElements();
defineUiComposerElements();
createApp(App).mount('#app');
Tell Vue that <xpv-…> tags are web components, not Vue components:
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [
vue({
template: {
compilerOptions: {
isCustomElement: (tag) => tag.startsWith('xpv-'),
},
},
}),
],
});
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { defineCustomElements as defineUiKitElements } from '@expivi/sdk/ui-kit/components';
import { defineCustomElements as defineUiComposerElements } from '@expivi/sdk/ui-composer/components';
import '@expivi/sdk/theme/style.css';
import './style.css';
import App from './App';
// Register the elements before the first render, so React sees their properties (such as
// `configurator`) and assigns them as properties instead of attributes.
defineUiKitElements();
defineUiComposerElements();
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
);
Tell TypeScript which props the <xpv-…> elements accept:
import type { DetailedHTMLProps, HTMLAttributes } from 'react';
import type { ProductConfigurator } from '@expivi/sdk';
type ExpiviElementProps = DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement> & {
configurator?: ProductConfigurator;
};
declare module 'react' {
namespace JSX {
interface IntrinsicElements {
'xpv-product-visualizer': ExpiviElementProps;
'xpv-layout-composer': ExpiviElementProps;
}
}
}
Import the web components from …/components, not from …/loader. The loader entry points load each component from a separate file at runtime, which breaks when a bundler moves the files. See Troubleshooting.
5. Create the configurator and render the product
Create a ProductConfigurator with your credentials and load the product. Then wait until the configurator reports the READY status before you add the Expivi elements to the page: they read the configurator when they are added, and the options panel throws an error if the product hasn't loaded yet.
Give each element the configurator through its configurator property. It is a JavaScript property, not an HTML attribute, because its value is an object.
- Vanilla JS
- Vue
- React
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My product</title>
<script src="https://assets.expivi.net/viewer/latest/viewer.js"></script>
</head>
<body>
<main class="product-page">
<section class="product-page__viewer" id="viewer" data-xpv-theme-prefix></section>
<aside class="product-page__options" id="options" data-xpv-theme-prefix>
<p id="status">Loading product…</p>
</aside>
</main>
<script type="module" src="/src/main.js"></script>
</body>
</html>
import { ProductConfigurator, ProductConfiguratorStatus, useProductConfigurator } from '@expivi/sdk';
import { defineCustomElements as defineUiKitElements } from '@expivi/sdk/ui-kit/components';
import { defineCustomElements as defineUiComposerElements } from '@expivi/sdk/ui-composer/components';
import '@expivi/sdk/theme/style.css';
import './style.css';
import { expiviConfig } from './expivi.config.js';
defineUiKitElements();
defineUiComposerElements();
const statusElement = document.querySelector('#status');
// The configurator holds the product state and talks to the Expivi API.
const configurator = new ProductConfigurator({
apiBaseUrl: expiviConfig.apiBaseUrl,
apiToken: expiviConfig.apiToken,
locale: expiviConfig.locale,
});
let elementsMounted = false;
// Follow the loading status. Once the product is READY, add the Expivi elements.
useProductConfigurator(configurator).subscribeStatus((status) => {
if (status === ProductConfiguratorStatus.READY && !elementsMounted) {
elementsMounted = true;
statusElement.remove();
mountExpiviElements();
}
if (status === ProductConfiguratorStatus.ERROR) {
statusElement.textContent = 'The product could not be loaded.';
}
});
configurator.loadProduct(expiviConfig.productId).catch((error) => {
console.error(error);
statusElement.textContent = 'The product could not be loaded.';
});
function mountExpiviElements() {
// Set `configurator` before adding the element: the element reads it when it is added.
const visualizer = document.createElement('xpv-product-visualizer');
visualizer.configurator = configurator;
document.querySelector('#viewer').append(visualizer);
const layoutComposer = document.createElement('xpv-layout-composer');
layoutComposer.configurator = configurator;
document.querySelector('#options').append(layoutComposer);
}
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue';
import { ProductConfigurator, ProductConfiguratorStatus, useProductConfigurator } from '@expivi/sdk';
import { expiviConfig } from './expivi.config';
const isReady = ref(false);
const hasError = ref(false);
// The configurator holds the product state and talks to the Expivi API.
// It is not reactive state, so it is not wrapped in ref().
const configurator = new ProductConfigurator({
apiBaseUrl: expiviConfig.apiBaseUrl,
apiToken: expiviConfig.apiToken,
locale: expiviConfig.locale,
});
const unsubscribeStatus = useProductConfigurator(configurator).subscribeStatus((status) => {
if (status === ProductConfiguratorStatus.READY) isReady.value = true;
if (status === ProductConfiguratorStatus.ERROR) hasError.value = true;
});
configurator.loadProduct(expiviConfig.productId).catch((error) => {
console.error(error);
hasError.value = true;
});
onBeforeUnmount(() => {
unsubscribeStatus();
configurator.destroy();
});
</script>
<template>
<main class="product-page">
<section class="product-page__viewer" data-xpv-theme-prefix>
<!-- `.configurator` binds a JS property instead of an HTML attribute. -->
<xpv-product-visualizer v-if="isReady" .configurator="configurator" />
</section>
<aside class="product-page__options" data-xpv-theme-prefix>
<p v-if="hasError">The product could not be loaded.</p>
<p v-else-if="!isReady">Loading product…</p>
<xpv-layout-composer v-else .configurator="configurator" />
</aside>
</main>
</template>
import { useEffect, useState } from 'react';
import {
ProductConfigurator,
ProductConfiguratorStatus,
// The SDK's `use…` functions are plain functions, not React hooks. Aliasing it keeps
// React's rules-of-hooks linting happy when it is called inside useEffect.
useProductConfigurator as getConfiguratorController,
} from '@expivi/sdk';
import { expiviConfig } from './expivi.config';
export default function App() {
// `configurator` stays null until the product is READY.
const [configurator, setConfigurator] = useState<ProductConfigurator | null>(null);
const [hasError, setHasError] = useState(false);
useEffect(() => {
// The configurator holds the product state and talks to the Expivi API.
const instance = new ProductConfigurator({
apiBaseUrl: expiviConfig.apiBaseUrl,
apiToken: expiviConfig.apiToken,
locale: expiviConfig.locale,
});
const unsubscribeStatus = getConfiguratorController(instance).subscribeStatus((status) => {
if (status === ProductConfiguratorStatus.READY) setConfigurator(instance);
if (status === ProductConfiguratorStatus.ERROR) setHasError(true);
});
instance.loadProduct(expiviConfig.productId).catch((error) => {
console.error(error);
setHasError(true);
});
return () => {
unsubscribeStatus();
instance.destroy();
setConfigurator(null);
};
}, []);
return (
<main className="product-page">
<section className="product-page__viewer" data-xpv-theme-prefix="">
{configurator && <xpv-product-visualizer configurator={configurator} />}
</section>
<aside className="product-page__options" data-xpv-theme-prefix="">
{hasError && <p>The product could not be loaded.</p>}
{!configurator && !hasError && <p>Loading product…</p>}
{configurator && <xpv-layout-composer configurator={configurator} />}
</aside>
</main>
);
}
The configurator is created inside useEffect, so React's StrictMode, which mounts components twice in development, creates and destroys it cleanly.
A few things to note:
data-xpv-theme-prefixapplies the Expivi theme to the components inside the element. The layout composer needs it on a parent element. See Style and theme the UI.- Clean up. When your page or component goes away, call
configurator.destroy()and the unsubscribe functions, as the Vue and React examples do.
6. Give the visualizer a size
The visualizer fills its parent element, so the parent decides its size. Without a height, the viewer is invisible.
.product-page {
display: grid;
grid-template-columns: minmax(0, 3fr) minmax(320px, 2fr);
gap: 24px;
}
.product-page__viewer {
height: 70vh;
}
.product-page__viewer xpv-product-visualizer {
display: block;
width: 100%;
height: 100%;
}
7. Run it
npm run dev
Open the URL that Vite prints. You see the product visualization and its options. Change an option, and the visualization updates.
The visualizer shows what the product has: a 3D model, a 2D visualization or images. The options panel shows the layout that is set up for the product in Expivi, so you change steps, sections and option controls in Expivi, not in your code.
Configurator options
These are the options you can pass to new ProductConfigurator():
| Option | Description |
|---|---|
apiBaseUrl | Required. The URL of the Expivi API, such as https://expivi.net/api/v2. |
apiToken | Required. Your API token. |
locale | The language of the texts and the price format, such as nl-NL. It must be a locale that is enabled for your Expivi team; otherwise loading the product fails. When you leave it out, the API uses the account default. |
country | The country of the shopper. When you leave it out, the API uses the account default. |
currency | The currency of the prices. When you leave it out, the API uses the account default. |
snapshotId | A snapshot to restore while the product loads. See Save and restore configurations. |
productConfigurationIdentifier | A saved product configuration to restore while the product loads. Ignored when snapshotId is set. |
getConfigurationLink | A function that builds the link to a configuration from a snapshot ID, for example for quote emails. |
Next steps
- How the UI components work explains what you just used.
- Style and theme the UI makes the configurator match your brand.