Skip to main content

How the API Pattern Works

Most Upcart API scripts follow the same simple pattern: Listen for a cart event → Check a condition → Take an action For example: “When the cart loads → check if it’s empty → hide the sticky button.” 💡 New to APIs? Start with What is an API? before diving into the examples below.

Where to Add Your Scripts

All scripts below go in: Cart Editor → Settings → Custom HTML → Scripts (before load) Wrap each snippet in <script>...</script> tags and save. To test, open your browser’s Dev Tools console (F12) and look for any console.log messages.

A note on legacy vs. modern callbacks

Upcart has two ways to listen for cart events: All examples below use the modern API. Existing scripts using the old style will continue working.

Example 1: Hide the Sticky Cart Button When the Cart is Empty

How it works: upcartSubscribeCartLoaded fires every time the cart is loaded. The callback receives an event with a cart object containing an items array. We sum the quantity of each item to determine if the cart is empty. ⚠️ IMPORTANT: event.cart does NOT have an item_count property. You must calculate the total by iterating event.cart.items.

Example 2: Log When an Item is Added to the Cart

Properties available on event.item:

Example 3: Integrate with a Third-Party Analytics App (e.g. TripleWhale)

Note: Each third-party app is different. Check with your app’s support team for the correct event format.

Example 4: Open the Cart Automatically After a Product is Added

Note: If “Open cart drawer on add to cart” is already enabled in Cart Editor → Settings → Cart Settings, you don’t need this script.

Quick Reference: Subscribe Functions (Modern API)


Direct Action Functions

For the full API documentation, see the Upcart Public API Documentation.

Troubleshooting

  • Script not running? Double-check placement: it should be in Scripts (before load), not after load.
  • Element not found? Make sure the selector (e.g. #upCartStickyButton) matches the actual element ID in your cart.
  • Something broke? Comment out your script by adding // to the start of each line, save, and refresh.
  • Still stuck? See the API FAQ for more troubleshooting steps.