Skip to main content

Ajax API overview

The Ajax API provides a suite of lightweight REST API endpoints for the development of Shoplazza themes.

note

The Ajax API can only be used by themes that are hosted by Shoplazza.

Use cases

Possible uses of the Ajax API include:

  • Add products to the cart and update the cart item counter.
  • Display related product recommendations.
  • Suggest products and collections to visitors as they type in a search field.
  • Register a customer, sign them in, and manage their profile and addresses.
  • Show a customer their orders and the tracking status of a shipment.
  • Pull blog articles, custom pages, and navigation menus into a section.
  • Read the metafields of a product, a collection, or the shop from JavaScript.

See the Ajax API reference for a full list of available API endpoints.


Making requests to the API

The Ajax API accepts four types of HTTP requests:

  • GET requests to read store data, such as the cart, products, collections, customers, and orders
  • POST requests to create data or submit a form, such as adding to the cart or signing a customer in
  • PATCH requests to modify data, such as a cart line item or a customer address
  • DELETE requests to delete data, such as a cart line item or a customer address

For instance, to fetch the current content of the cart, send a client-side request to the store /api/cart endpoint.

Example
fetch(window.SHOPLAZZA.routes.root + '/api/cart')
.then(response => response.json())
.then(data => { /* do something */});

Locale-aware URLs

Stores can have dynamic URLs generated for them when they sell internationally or in multiple languages. When using the Ajax API, it’s important to use dynamic, locale-aware URLs so that you can give visitors a consistent experience for the language and country that they’ve chosen.

The global value window.SHOPLAZZA.routes.root is available to use as a base when building locale-aware URLs in JavaScript. The global value will always end in an empty string, so you can safely use simple string concatenation to build the full URLs.