JavaScript SDK basics
Snipcart offers a JavaScript SDK that lets you configure, customize and manage the cart programmatically.
After the snipcart.js
file has loaded, and its content is done executing, a Snipcart
object is attached to the window
. From there, you can directly interact with the SDK.
We trigger an event named snipcart.ready
on the the global document
object when the SDK is ready. We recommend you encapsulate all your API calls or event subscribers in the callback of that event:
document.addEventListener('snipcart.ready', () => {
// You can safely use window.Snipcart here
Snipcart.events.on(...);
});
The SDK is separated into three main sections:
- The
api
object allows you to retrieve relevant information about the current Snipcart session and apply certain operations to the cart. - The
events
object allows you to listen to incoming events and trigger callbacks dynamically. - The
store
object allows you to listen to state changes as well as retrieving a full snapshot of the state of the cart.
For a more detailed description of everything that's available inside the SDK, see the technical reference.