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 inbundle.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:/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:POST /cart/clear.js first.
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’sGET /cart.js returns, with Shopify’s own snake_case field names.
A cart that meets a tier returns the code:
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. WhendiscountNeeded is false, go straight to checkout:
?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.