Skip to main content
A Dynamic Pricing Build-a-Box lets a customer pick several products from a list. Each pick is an ordinary subscription line at the product’s own price, and the box’s tiers take a percentage off when the customer picks enough items or spends enough. Appstle’s own box page does all of this for you. This guide shows how to do the same from your own UI: read the box, add the picks to the cart, ask Appstle for the discount code that matches the cart, and send the customer to checkout with it.
Steps 1, 2 and 4 run in the customer’s browser. Step 1 calls Appstle’s Storefront API through Shopify’s App Proxy, so it needs no API key; it isn’t Shopify’s Storefront GraphQL API. Step 2 is Shopify’s Cart API. In Step 3 you choose: call Appstle’s Storefront API from the browser, or call the Admin API from your server with an API key. Never put an API key in code that runs in the browser. If the store changed its App Proxy path, replace /apps/subscriptions with it. Read Build your own frontend first if you haven’t used the App Proxy before.

What you’ll build

  • A page that reads a box’s rules and choices and lets the customer pick products.
  • A cart add that puts each pick in the cart with a selling plan.
  • A call that returns the discount code for the cart, or tells you that no discount applies.
  • A redirect to checkout with the code applied.

Before you start

  • A Dynamic Pricing box (a Dynamic Pricing Bundle) with its status set to Enabled in the Build-A-Box section of the Appstle Subscriptions app, and at least one tiered discount. Note its reference: the box list shows it as Ref, and the API calls it uniqueRef. A box that is disabled, or a reference that matches no box, is treated as having no discount (see Errors).
  • A subscription plan on every product the customer can pick. The plans must have matching delivery frequencies.
  • For Step 3 from your server, an Admin API key with Read & Write access. See Authentication.

How the tiers work

The box’s tiers are in bundle.tieredDiscount, a JSON string. For a box with one tier it looks like this:
When the cart meets several tiers, Appstle uses the one with the largest discount. When it meets none, there’s no discount and no code.

Step 1: Read the box

Get the box’s rules and its choices. The call goes through the App Proxy, so it needs no API key:
The reference lists the same call under /apps/subscriptions/cp/api/, which needs a signed-in customer: see Get public Build-a-Box bundle details by handle (v3). The call answers 204 with an empty body when no box has this reference or the box is disabled. A trimmed response for a box named Coffee Box:

Step 2: Add the picks to the cart

Add each pick as its own line, with the variant, the quantity and the selling plan. This is Shopify’s Cart API, so no Appstle call is involved:
These are the three fields Appstle’s own box page sends. Every line in the cart counts toward the tiers, including products that aren’t in the box, so start from an empty cart: call Shopify’s POST /cart/clear.js first.
Shopify answers 422 with Cannot apply selling plan to variant when the selling plan isn’t attached to the variant’s product. Take the plan from the box’s own plan group, as in Step 1.

Step 3: Get the discount code

Read the cart back from Shopify, and send it to Appstle with the box’s reference. The cart is the JSON that Shopify’s GET /cart.js returns, with Shopify’s own snake_case field names.
From your server, the Admin API has the same call: Generate discount code for bundle. It uses an API key, so run it on your server and pass it the cart your page sends you:
Appstle reads these cart fields and ignores the rest:
Use the snake_case names above. A name Appstle doesn’t know, such as totalPrice or productId, is ignored without an error. With total_price missing, the call fails with the server error described in Errors. With product_id and variant_id misnamed, the call returns 200 with an empty body and no code.
A cart that meets a tier returns the code:
A cart that meets none returns:
Every call that returns a code creates a new discount in the store’s Shopify admin, so call once, when the customer goes to checkout, and not on every change to the cart. If the customer changes the cart after that, ask for a code again. The code gives the matched tier’s percentage off the box’s products in the cart. It applies to subscription purchases, stays on renewal orders, needs at least the tier’s quantity or subtotal, and combines with product, order and shipping discounts. It allows a one-time purchase only when the box does.

Step 4: Send the customer to checkout

Use Shopify’s discount link, which applies the code and then opens the page you name. It is the link Appstle’s own box page uses. When discountNeeded is false, go straight to checkout:
Use ?redirect=/cart to show the cart first. When the customer pays, Shopify creates the subscription with one line for each pick.

Errors

Next steps

Fixed-price Build-a-Box

The same flow for a box with one price and customer-chosen contents.

Build your own frontend

Selling plans, the cart and the customer portal from your own UI.

Authentication

Admin API keys and how storefront calls are authenticated.

Integration guide

Server-side calls for subscriptions and contracts.