JavaScript SDK

Headless

Document image


This document provides a comprehensive guide on leveraging SparkLayer in conjunction with a Shopify-powered headless storefront. At the present moment, SparkLayer's exclusive support for Shopify is due to the need for a synchronisation mechanism However, in the near future, we plan expand its capabilities, enabling the development of custom integrations that can be compatible with many other platforms.

Requirements

  • A Shopify store
  • The SparkLayer App installed on the store
  • A metafield definition set up for the SparkLayer Authentication metafield
  • Enable storefronts access for this metafield definition

SparkLayer uses a customer metafield to store authentication data, which allows the SparkLayer front-end to authenticate the user with the SparkLayer API.

Overview

This is a high level overview of how SparkLayer runs on a headless site.

  • Get the customer data for the logged in user
  • Check if the customer is a b2b customer (has the tag "b2b") and has the sparklayer.authentication metafield set. If they do, continue.
  • Load the SparkLayer javascript by injecting a script tag such as: <script src="https://cdn.sparklayer.io/spark.1.0.26.js"></script>
  • When the script has loaded call window.sparkInit({ ...options }}), passing in options like siteId. More details on configuration can be found here.
  • Inject a style element into the document which sets CSS variables to customise the look and behaviour of the SparkLayer components. More details here.

Additional code changes are required to: show SparkLayer components, hide elements that should not be seen by b2b customers and integrate SparkLayer with controls on your site.

Elements to hide

The following elements should be hidden for b2c customers on all pages:

  • Prices
  • Add to cart button
  • Quantity selector

By adding the following data attribute to the element data-spark="b2c-only", the element will be hidden when SparkLayer loads. You can learn more here.

Include SparkLayer components

You'll want to include SparkLayer's components in place of the elements that were just hidden.

Product list

HTML


{{ parentProductId }} the syntax for setting parent-id will vary depending on the template system used. Learn more here

Product details page

HTML


Code to load SparkLayer

An example react component to load SparkLayer in a nextjs project:

JS


You can see an example usage of this component:

JS


For more information, please feel free to get in touch and we'd be happy to discuss our Headless solution in more detail.

Updated 30 Jul 2024
Did this page help you?