JavaScript APIs
The storefront JSON endpoints a theme calls from the browser - cart, wishlist, back-in-stock alerts, search suggestions, menu levels, newsletter - and platform events.
Pages are cached and the same for every visitor, so anything personal - the cart count, items in the wishlist - is read in the browser. The storefront offers small JSON endpoints for it, modelled on Shopify's AJAX API: field names and amounts in minor units match, so ported code needs little change.
All endpoints are on the store's own domain. Requests that change something (POST) are accepted only from the store's own pages - browsers send Sec-Fetch-Site or Origin with fetch() on their own; there is no CSRF token to pass. Store addresses end with a slash; the .js and .json endpoints are files and do not.
Cart
Read the cart
const cart = await fetch('/cart.js', { headers: { Accept: 'application/json' } }).then((r) => r.json());
{
"token": null,
"item_count": 3,
"items": [
{
"key": "0192f…",
"id": "0192e…",
"title": "Linen shirt",
"product_title": "Linen shirt",
"variant_title": "L / Natural",
"quantity": 1,
"price": 18900,
"compare_at_price": 22900,
"properties": { "Engraving": "For Anna" },
"options_with_values": [{ "name": "Engraving", "value": "For Anna", "price": 1500 }],
"line_price": 18900,
"url": "/products/linen-shirt/",
"image": "https://cdn…/m/…/large.webp",
"available": true
}
],
"items_subtotal_price": 37800,
"total_discount": 0,
"total_price": 37800,
"currency": "PLN",
"requires_shipping": true,
"checkout_url": "/checkout/",
"shipping_from": 1299,
"drawer_buttons": "cart"
}
shipping_from is the cheapest delivery for this cart, computed exactly as checkout computes it - write "Delivery from …" from it, never from a theme setting. null means no method fits. drawer_buttons is the store's choice for a cart drawer: which buttons it shows.
Add to the cart
const response = await fetch('/cart/add.js', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({ id: variantId, quantity: 1, properties: { [fieldId]: 'For Anna' } }),
});
const result = await response.json();
// result: the added line, `cart`, `redirect` and `after_add`
| Field | Description |
|---|---|
id |
The variant id - variant.id, or item.variantId of a cart line. |
quantity |
Units, at least 1. |
properties |
Values of product option fields by field id - use the name from product.optionFields. |
checkout |
true to go straight to checkout - redirect then points there. |
after_add tells you what the store wants after adding: open the cart drawer, go to the cart, stay. redirect is where to go when the answer is to leave the page.
The same POST /cart/add/ works as a plain form, so the add-to-cart button works without JavaScript:
<form action="/cart/add/" method="post">
<input type="hidden" name="id" value="{{ product.selectedOrFirstAvailableVariant.id }}">
<button type="submit"{% if not product.available %} disabled{% endif %}>{{ 'product.add_to_cart'|t }}</button>
</form>
Change and clear
await fetch('/cart/change.js', { method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ line: item.key, quantity: 2 }) }); // 0 removes the line
await fetch('/cart/clear.js', { method: 'POST' });
line is the line key from /cart.js (or item.id of a cart line object). Both answer with the whole cart.
Discount codes
const json = { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' } };
await fetch('/cart/discount.js', { ...json, body: JSON.stringify({ code: 'SPRING10' }) }); // adds a code
await fetch('/cart/discount.js', { ...json, body: JSON.stringify({ remove: 'SPRING10' }) }); // removes one code
await fetch('/cart/discount.js', { ...json, body: JSON.stringify({ code: '' }) }); // removes every code
A cart holds up to five codes. code adds a code to the ones already in the cart; it never replaces them. A code that does not combine with the codes in the cart answers 422 and is not kept. A valid code whose conditions the cart does not meet yet (a minimum value, for example) stays in the cart and applies once it does. /cart.js lists the codes in discount_codes: [{ "code": "SPRING10", "applicable": true, "message": null }], where message says why a code does not apply yet. In Twig the same list is cart.discountCodes.
The cart summary already does this for you. /discount/<CODE>/?redirect=/products/example/ is a link that puts a code in the cart and goes to a page of the store.
Errors
A failed request answers with a status of 4xx and { "status": 422, "message": "…", "description": "…" } - show message to the visitor.
Wishlist
const wishlist = await fetch('/wishlist.js').then((r) => r.json());
// { "count": 2, "items": ["<offer id>", "<offer id>"], "url": "/wishlist/", "logged_in": false }
await fetch('/wishlist/add.js', { method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id: product.id, variant: variantId }) });
await fetch('/wishlist/remove.js', { method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id: product.id }) });
Guests have a wishlist too; it moves to their account when they log in. The wishlist page itself is a platform page in the customer account.
Back-in-stock alerts
When a product is out of stock, show a "notify me when it is back" form instead of a disabled "Add to cart" button. The platform keeps the sign-ups and e-mails each visitor once when the product, or the variant they chose, can be bought again. The e-mail has a link to the product and an opt-out link. The theme only sends the form.
The merchant sees the sign-ups in Back in stock alerts in the store panel and edits the e-mail as the Product back in stock notification. There is nothing to turn on in the theme settings and no Twig function for it: the theme draws the form itself.
Show the form when product.available is false - no variant can be bought. When only some variants are sold out, either keep the normal picker with the sold-out variants greyed out, or show the form for the selected variant when variant.available is false.
{% set variant = product.selectedOrFirstAvailableVariant %}
{% if not product.available %}
<form class="notify" method="post" action="/back-in-stock/subscribe/" data-notify>
<input type="hidden" name="id" value="{{ product.id }}">
<input type="hidden" name="variant" value="{{ variant ? variant.id : '' }}">
<input type="text" name="website" tabindex="-1" autocomplete="off" hidden>
<label>Your name <input type="text" name="name" required autocomplete="given-name"></label>
<label>E-mail <input type="email" name="email" required autocomplete="email"></label>
<button>Notify me</button>
<p data-notify-message hidden></p>
</form>
{% endif %}
document.querySelectorAll('[data-notify]').forEach((form) => {
form.addEventListener('submit', async (event) => {
event.preventDefault();
const res = await fetch('/back-in-stock/subscribe.js', { method: 'POST', body: new FormData(form), headers: { Accept: 'application/json' } });
const data = await res.json();
// 200: { "status": "ok", "message": "…", "email": "[email protected]" }
// 422: { "status": "error", "message": "…", "field": "email" }
const message = form.querySelector('[data-notify-message]');
message.textContent = data.message;
message.hidden = false;
});
});
| Field | Description |
|---|---|
id |
The product id (product.id). Required. |
variant |
The variant id. Optional - empty means "any variant of this product". |
email |
Required, a valid address. |
name |
Required. |
phone |
Optional. |
website |
Keep it empty: it is a trap for bots. A filled one gets a success answer and is not saved. |
- Send the fields as a form (
FormDataorapplication/x-www-form-urlencoded), not as JSON. POST /back-in-stock/subscribe.jsanswers with JSON: 200 when the sign-up is saved, 422 withmessageand, for a wrong field,field(email,nameorphone). Showmessagenext to that field, or above the form when there is nofield. Messages are in the store's language.POST /back-in-stock/subscribe/takes the same fields without JavaScript and sends the visitor back to the product page.- The same e-mail for the same product and variant is one sign-up - sending again only updates the name and phone. One visitor IP can send about 10 sign-ups an hour.
- After a successful sign-up, replace the form with a confirmation that shows
email, and offer to sign up another address. - Password managers like to add their icon to a name + e-mail pair. If that bothers you, keep the inputs outside a
<form>, give them neutral names and build theFormDatain your script with the field names above.
Search suggestions
GET /search/suggest.json?q=linen&limit=6
{
"query": "linen",
"products": [{ "id": "…", "title": "…", "url": "…", "image": "…", "price": 18900, "price_formatted": "189,00 zł", "price_varies": true, "available": true }],
"categories": [],
"pages": [],
"articles": []
}
limit is 1 to 10. Debounce the input - one request per pause in typing, not per key.
Menu levels
GET /categories/<handle>/children.json returns the subcategories of a category for a mega-menu or a mobile drawer. See Menu levels on demand.
Newsletter
<form action="/newsletter/subscribe/" method="post">
<input type="email" name="email" required>
<input type="text" name="website" tabindex="-1" autocomplete="off" hidden>
<button>Subscribe</button>
</form>
POST /newsletter/subscribe.js takes the same fields and answers with JSON. Keep the empty website field: it is a trap for bots and a filled one is rejected. The core newsletter section does all of this - prefer it.
Platform events
shopdepot:cart:changed is dispatched on document by the cart summary after a coupon is applied or removed. detail is { cart, document } - the cart JSON and the freshly rendered cart page. By default the platform then replaces your cart section with the fresh one; call preventDefault() if your theme updated the page itself:
document.addEventListener('shopdepot:cart:changed', (event) => {
renderLines(event.detail.document.querySelector('[data-cart-lines]'));
event.preventDefault();
});
base adds a few conventions of its own you can copy: it emits shopdepot:cart:updated after every cart change it makes and shopdepot:variant:change when the variant picker changes, and exposes window.ShopDepot.cart and window.ShopDepot.menu helpers. Those are theme code, not a platform API - keep them if your theme builds on base.
Consent
Scripts the merchant adds to the store run only with the visitor's consent, decided in the browser from the sd_consent cookie. When the page has such scripts, the platform adds a small loader with window.ShopDepot.consent:
window.ShopDepot?.consent?.set({ analytics: true, marketing: false }); // stores the choice and runs what it allows
window.ShopDepot?.consent?.get(); // { necessary: true, analytics: …, marketing: … }
A cookie banner that writes its own sd_consent cookie (JSON like {"marketing":true} or a list like necessary,analytics) dispatches shopdepot:consent on window afterwards. Your theme does not need any of this unless it ships its own cookie banner.