Skip to main content

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.

npm create vite@latest my-shop -- --template vanilla
cd my-shop
npm install @expivi/sdk

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.

.env
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=
warning

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:

src/expivi.config.js
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:

index.html
<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/components contains the option controls, such as tiles, lists and color pickers.
  • @expivi/sdk/ui-composer/components contains the visualizer and the layout composer, which renders the options.
src/main.js
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();
note

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.

index.html
<!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>
src/main.js
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);
}

A few things to note:

  • data-xpv-theme-prefix applies 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.

src/style.css
.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():

OptionDescription
apiBaseUrlRequired. The URL of the Expivi API, such as https://expivi.net/api/v2.
apiTokenRequired. Your API token.
localeThe 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.
countryThe country of the shopper. When you leave it out, the API uses the account default.
currencyThe currency of the prices. When you leave it out, the API uses the account default.
snapshotIdA snapshot to restore while the product loads. See Save and restore configurations.
productConfigurationIdentifierA saved product configuration to restore while the product loads. Ignored when snapshotId is set.
getConfigurationLinkA function that builds the link to a configuration from a snapshot ID, for example for quote emails.

Next steps​