Build a post-purchase upsell backend with Gadget 

Topics covered: HTTP routes
Time to build: ~1 hour

Post-purchase upsell applications are a great way for merchants to increase sales by presenting shoppers with a one-click option to add additional items to their purchases. There are countless different strategies to pick between when determining what products a merchant wants to push for an upsell opportunity: it might be products that are related or frequently bought together with the products that the customer has already bought, it could be an item that the merchant is particularly interested in selling, or it could just be an item that is on sale.

Thankfully, Shopify is aware of the usefulness of a post-purchase upsell built into the checkout experience! They have a post-purchase upsell UI extension that will be used for this tutorial. Before tackling this tutorial, it might be helpful to go through Shopify's docs on the post-purchase checkout extension to understand how the extension works.

An example of a post-purchase upsell page presented to a customer

The logic required to determine what products are presented to users is the interesting part of a post-purchase upsell application. But to get to the point where you are writing this business logic code, you need to go through the process of setting up a server and ensuring that it will be able to scale to handle potentially high traffic.

Diagram of the tech stack used in this tutorial. Gadget handling sync and custom routing, Shopify handling the store and frontend

Using Gadget, this tutorial shows you how to skip the boilerplate server work and brings you directly to writing your post-purchase upsell logic.

Requirements

Before starting this tutorial you need the following:

You can fork this Gadget project and try it out yourself.

You still need to set up the Shopify Post-purchase Extension, add environment variables, and complete the application frontend.

Fork on Gadget

Create a new Shopify Connection 

You will want the development store's Product Variant data in your Gadget app, so you need to set up a new Shopify Connection. You will need a Shopify CLI 3.X app to generate the post-purchase upsell extension, so we will connect our Gadget app to Shopify through a new CLI app.

Once you have your app installed on a store, you can click on Shop Installs on the Shopify connections page in Gadget and click on the Sync button for the store. The sync will run and your imported Shopify Product models will have some data that can be used to offer products during the post-purchase upsell.

Set up Shopify App Bridge Extension 

Good news! Shopify already has a tutorial that covers setting up a new frontend application using the Shopify App Bridge Extension.

You should go through the entire first page of their tutorial. It will help you set up a front-end application on a development store and allows you to inject a post-purchase upsell page into your checkout workflow. Instead of creating a new CLI app, you can use the existing app that you created to make your Gadget connection.

Fixes to the Shopify tutorial

When running your app locally, you need to start your app with a Cloudflare tunnel like so: yarn dev --tunnel-url https://my-tunnel-url:port. You must include a port, and the default Cloudflare tunnel port is 8080.

The index.jsx code file that Shopify creates may require an additional import statement. If you see any React errors in your web console, adding import React from "react"; at the top of your code file should be the fix!

Once your Shopify CLI app is up and running, Shopify gives you a button in your store Admin to add test products to your store automatically. Add some products!

The default Shopify CLI app embedded in a store Admin page. It gives you a button to add test products

You should also make sure your products are enabled and visible in your store, and that some products are being offered at a discounted price. To offer some products at a discount, fill in the Compare at price and Price fields on a product. You can also add some test products manually.

Do not proceed until you have the frontend of Shopify's post-purchase extension demo working!

Once you have completed the first portion of the Shopify tutorial, move on to the Upsell example. Your Gadget app will replace the Node server that you set up in Part 2: Build an app server.

This Gadget application will sync product data with your development store. This product data will be used along with custom routing endpoints to determine what product will be offered to a shopper.

Set up Gadget routing 

Shopify's upsell tutorial has you set up a local Node server to test out the post-purchase upsell frontend. You will replace this server with a Gadget app!

There are 3 things you need to add to your Gadget app to get it up and running:

  • a custom route to handle the /offer request
  • a custom route to handle the /sign-changeset request
  • custom configuration to handle CORS

Custom route for /offer 

The first thing you want to do is set up the /offer route. This route will accept a payload from the frontend app, determine what product will be presented as part of the post-purchase upsell, and then return the product variant information required to render on the frontend.

Go to the Gadget file explorer and add a new file in the routes folder.

The button you can use to create a new route file in Gadget

You can then rename the auto-generated file to POST-offer.js. Custom routes in Gadget need to be prepended with one of the accepted Fastify request types. The remainder of the file name is the path of the route! You can read more information about creating custom routes in the Gadget documentation.

You are going to pass information from the post-purchase Shopify App Bridge Extension to our Gadget app. This information includes the line items the customer is purchasing, as well as the total price of the order. You can use this information to determine what kind of offer you want to present to the customer. For this tutorial, you will select a random product variant that is not included as part of the original purchase and offer it to the customer at a discount.

Paste the following code snippet into your POST-offer.js file.

JavaScript
1/**
2 * Route handler for GET https://testing-out-post-purchase-stuff.gadget.app/offer
3 *
4 * @param { import("gadget-server").Request } request - incoming request data
5 * @param { import("gadget-server").Reply } reply - reply for customizing and sending a response
6 *
7 * @see {@link https://www.fastify.io/docs/latest/Reference/Request}
8 * @see {@link https://www.fastify.io/docs/latest/Reference/Reply}
9 */
10
11module.exports = async (request, reply) => {
12 const { body } = request;
13 if (body) {
14 // get the purchase information from the Shopify App Bridge Extension
15 const { inputData } = body;
16
17 // get ids of variants that are in the purchase
18 const purchaseIds = inputData.initialPurchase.lineItems.map(
19 (lineItem) => lineItem.product.variant.id
20 );
21
22 // use Gadget's findMany api and filter for variants that are not included in the shoppers original purchase
23 let variants = await request.api.shopifyProductVariant.findMany({
24 filter: {
25 id: {
26 notIn: purchaseIds,
27 },
28 },
29 select: {
30 id: true,
31 price: true,
32 compareAtPrice: true,
33 product: {
34 id: true,
35 title: true,
36 body: true,
37 },
38 },
39 });
40
41 // only present an offer if there are variants that meet the criteria (cheaper than initial purchase and on sale)
42 if (variants.length > 0) {
43 // pick a random variant from the list
44 const randomVariant = Math.floor(Math.random() * (variants.length - 1));
45 const variant = variants[randomVariant];
46 const { product } = variant;
47
48 // get single image for product to be presented on upsell page
49 const productImages = await request.api.shopifyProductImage.findMany({
50 select: {
51 source: true,
52 },
53 filter: {
54 product: {
55 equals: product?.id,
56 },
57 },
58 });
59
60 // grab first image for this product
61 let productImage = productImages[0];
62
63 // discount original price by 15% if there isn't a compareAtPrice available
64 const discountedPrice = variant.compareAtPrice
65 ? variant.price
66 : variant.price
67 ? (parseFloat(variant.price) * 0.85).toFixed(2)
68 : "No discount available";
69
70 // format data to be consumed by Shopify demo frontend application
71 const initialData = {
72 variantId: parseInt(variant.id),
73 productTitle: product?.title,
74 productImageURL: productImage?.source || "https://picsum.photos/200", // random image if product does not have one
75 productDescription:
76 product?.body?.split(/<br.*?>/) || "No product description",
77 originalPrice: variant.compareAtPrice || variant.price,
78 discountedPrice,
79 };
80
81 // send product variant as a response to be offered to shopper
82 reply.send(initialData);
83 } else {
84 defaultResponse(reply);
85 }
86 } else {
87 defaultResponse(reply);
88 }
89};
90
91function defaultResponse(reply) {
92 // empty response as a default - post-purchase upsell page will be empty
93 reply.send({});
94}

This file uses the Gadget API to call shopifyProductVariant.findMany() and then applies a filter condition to get product variants that are not included in the original purchase. An explicit select is also made so we can take advantage of the ShopifyProductVariants's relationship to the ShopifyProduct model.

JavaScript
1let variants = await request.api.shopifyProductVariant.findMany({
2 filter: {
3 id: {
4 notIn: purchaseIds,
5 },
6 },
7 select: {
8 id: true,
9 price: true,
10 compareAtPrice: true,
11 product: {
12 id: true,
13 title: true,
14 body: true,
15 },
16 },
17});

Notice that the Gadget API is available through the request parameter.

You then select a random variant and return it to the frontend. This is the business logic code. You can replace this code when writing a custom post-purchase upsell application.

Custom route for /sign-changeset 

If the customer chooses to purchase the product presented to them in the post-purchase upsell window, you need to modify the original order. Another route is needed to authenticate this transaction - Shopify requires a signed JWT to proceed. It is best to handle this with another custom route.

Create another new file in our routes folder and call it POST-sign-changeset.js.

Paste the following code snippet to respond to requests to this endpoint with a signed JWT. This code is almost identical to the server example provided in the Shopify tutorial.

routes/POST-sign-changeset
JavaScript
1/**
2 * Route handler for GET https://testing-out-post-purchase-stuff.gadget.app/sign-changeset
3 *
4 * @param { import("gadget-server").Request } request - incoming request data
5 * @param { import("gadget-server").Reply } reply - reply for customizing and sending a response
6 *
7 * @see {@link https://www.fastify.io/docs/latest/Reference/Request}
8 * @see {@link https://www.fastify.io/docs/latest/Reference/Reply}
9 */
10
11const jwt = require("jsonwebtoken");
12const { v4: uuidv4 } = require("uuid");
13
14module.exports = async (request, reply) => {
15 const decodedToken = jwt.verify(
16 request.body.token,
17 process.env["SHOPIFY_API_SECRET"]
18 );
19 const decodedReferenceId = decodedToken.input_data.initialPurchase.referenceId;
20
21 if (decodedReferenceId !== request.body.referenceId) {
22 reply.status(400);
23 }
24
25 const payload = {
26 iss: process.env["SHOPIFY_API_KEY"],
27 jti: uuidv4(),
28 iat: Date.now(),
29 sub: request.body.referenceId,
30 changes: request.body.changes,
31 };
32
33 const token = jwt.sign(payload, process.env["SHOPIFY_API_SECRET"]);
34 reply.send({ token });
35};

This will allow the original order to be modified successfully. But first, you need to import the jsonwebtoken and uuid modules and provide the SHOPIFY_API_KEY and SHOPIFY_API_SECRET environment variables required by our snippet.

Load npm modules 

To be able to run this snippet you need to import some modules in the package.json file: jsonwebtoken and uuid. To load these modules in your Gadget application, open the package.json file in the file explorer and add them as dependencies:

json
1{
2 "dependencies": {
3 "jsonwebtoken": "^8.5.1",
4 "uuid": "^8.3.2"
5 }
6}

Click the Run yarn button in the top right of package.json window to import the packages.

View of the package.json file with added dependencies

The status page that opens will let you know when the yarn install process is complete.

Add environment variables 

To add environment variables in Gadget, go to Environment Variables variables in the navigation bar.

The menu users can click on to be brought to Gadget's environment variables page

The SHOPIFY_API_KEY and SHOPIFY_API_SECRET can be found in your Shopify Partners Dashboard in the Overview tab marked as Client ID and Client secret.

The environment variables page with the SHOPIFY_API_KEY and SHOPIFY_API_SECRET entered

Our POST-sign-changeset.js snippet is already set up to read environment variables. You can access them in Gadget the same way you would access them in any other Node project: process.env.<ENVIRONMENT_VARIABLE_NAME>.

Those are the only two routes required for a post-purchase upsell app! Now you just need to enable cross-origin requests so that our tunneled local frontend can retrieve information from our Gadget app.

Handle CORS 

Gadget's custom routes are built on top of Fastify. This means you can use Fastify plugins such as fastify/cors to customize your routing. You can read more about extending route functionality in Gadget in our documentation.

To add the fastify-cors module to our Gadget application, open the package.json file and add it as a dependency: "fastify-cors": "^6.0.3".

Click the Run yarn button in the top right of package.json window to import the package.

Now you need to add a file that handles the injection of Fastify plugins into our router. Create a new file in the routes folder and call it +scope.js. Then paste the following snippet in the file:

routes/+scopes
JavaScript
1const FastifyCors = require("fastify-cors");
2
3module.exports = async (server) => {
4 await server.register(FastifyCors, {
5 // allow CORS requests from any origin
6 // you should configure this to be domain specific for production applications
7 origin: true,
8 // only allow POST requests
9 methods: ["POST"],
10 });
11};

This allows for cross-origin requests from Shopify, our backend Gadget app should now be reachable!

Need a different flavour of CORS handling for your custom app? Check out our docs for more details on how you can handle CORS settings.

Your Gadget file explorer should now look like this:

The final file explorer with all custom routes and CORS handling files created

Complete application frontend 

Our Gadget app is finished! Now you can continue with the Shopify tutorial starting back at Step 3: Update the extension code or copy and paste the snippet below to finish setting up our application extension frontend.

JavaScript
1import React, { useEffect, useState } from "react";
2import {
3 extend,
4 render,
5 useExtensionInput,
6 BlockStack,
7 Button,
8 CalloutBanner,
9 Heading,
10 Image,
11 Text,
12 TextContainer,
13 Separator,
14 Tiles,
15 TextBlock,
16 Layout,
17} from "@shopify/post-purchase-ui-extensions-react";
18
19extend("Checkout::PostPurchase::ShouldRender", async ({ inputData, storage }) => {
20 const postPurchaseOffer = await fetch(
21 "https://<gadget-app-name>.gadget.app/offer",
22 {
23 method: "POST",
24 headers: {
25 "Content-Type": "application/json",
26 },
27 body: JSON.stringify({ inputData }),
28 }
29 ).then((res) => res.json());
30
31 await storage.update(postPurchaseOffer);
32
33 return { render: true };
34});
35
36render("Checkout::PostPurchase::Render", () => <App />);
37
38export function App() {
39 const { storage, inputData, calculateChangeset, applyChangeset, done } =
40 useExtensionInput();
41 const [loading, setLoading] = useState(true);
42 const [calculatedPurchase, setCalculatedPurchase] = useState();
43
44 useEffect(() => {
45 async function calculatePurchase() {
46 // Request Shopify to calculate shipping costs and taxes for the upsell
47 const result = await calculateChangeset({ changes });
48
49 setCalculatedPurchase(result.calculatedPurchase);
50 setLoading(false);
51 }
52
53 calculatePurchase();
54 }, []);
55
56 const {
57 variantId,
58 productTitle,
59 productImageURL,
60 productDescription,
61 originalPrice,
62 discountedPrice,
63 } = storage.initialData;
64
65 const changes = [{ type: "add_variant", variantId, quantity: 1 }];
66
67 // Extract values from the calculated purchase
68 const shipping =
69 calculatedPurchase?.addedShippingLines[0]?.priceSet?.presentmentMoney?.amount;
70 const taxes =
71 calculatedPurchase?.addedTaxLines[0]?.priceSet?.presentmentMoney?.amount;
72 const total = calculatedPurchase?.totalOutstandingSet.presentmentMoney.amount;
73 // const discountedPrice =
74 // calculatedPurchase?.updatedLineItems[0].totalPriceSet.presentmentMoney
75 // .amount;
76 // const originalPrice =
77 // calculatedPurchase?.updatedLineItems[0].priceSet.presentmentMoney.amount;
78
79 async function acceptOffer() {
80 setLoading(true);
81
82 // Make a request to your app server to sign the changeset
83 const token = await fetch(
84 "https://<gadget-app-name>.gadget.app/sign-changeset",
85 {
86 method: "POST",
87 headers: { "Content-Type": "application/json" },
88 body: JSON.stringify({
89 referenceId: inputData.initialPurchase.referenceId,
90 changes: changes,
91 token: inputData.token,
92 }),
93 }
94 )
95 .then((response) => response.json())
96 .then((response) => response.token);
97
98 // Make a request to Shopify servers to apply the changeset
99 await applyChangeset(token);
100
101 // Redirect to the thank-you page
102 done();
103 }
104
105 function declineOffer() {
106 setLoading(true);
107 done();
108 }
109
110 return (
111 <BlockStack spacing="loose">
112 <CalloutBanner>
113 <BlockStack spacing="tight">
114 <TextContainer>
115 <Text size="medium" emphasized>
116 It&#39;s not too late to add this to your order
117 </Text>
118 </TextContainer>
119 <TextContainer>
120 <Text size="medium">Add the {productTitle} to your order and </Text>
121 <Text size="medium" emphasized>
122 save 15%.
123 </Text>
124 </TextContainer>
125 </BlockStack>
126 </CalloutBanner>
127 <Layout
128 media={[
129 { viewportSize: "small", sizes: [1, 0, 1], maxInlineSize: 0.9 },
130 { viewportSize: "medium", sizes: [532, 0, 1], maxInlineSize: 420 },
131 { viewportSize: "large", sizes: [560, 38, 340] },
132 ]}
133 >
134 <Image description="product photo" source={productImageURL} />
135 <BlockStack />
136 <BlockStack>
137 <Heading>{productTitle}</Heading>
138 <PriceHeader
139 discountedPrice={discountedPrice}
140 originalPrice={originalPrice}
141 loading={!calculatedPurchase}
142 />
143 <ProductDescription textLines={productDescription} />
144 <BlockStack spacing="tight">
145 <Separator />
146 <MoneyLine
147 label="Subtotal"
148 amount={discountedPrice}
149 loading={!calculatedPurchase}
150 />
151 <MoneyLine
152 label="Shipping"
153 amount={shipping}
154 loading={!calculatedPurchase}
155 />
156 <MoneyLine label="Taxes" amount={taxes} loading={!calculatedPurchase} />
157 <Separator />
158 <MoneySummary
159 label="Total"
160 amount={total}
161 loading={!calculatedPurchase}
162 />
163 </BlockStack>
164 <BlockStack>
165 <Button onPress={acceptOffer} submit loading={loading}>
166 Pay now · {formatCurrency(total)}
167 </Button>
168 <Button onPress={declineOffer} subdued loading={loading}>
169 Decline this offer
170 </Button>
171 </BlockStack>
172 </BlockStack>
173 </Layout>
174 </BlockStack>
175 );
176}
177
178function PriceHeader({ discountedPrice, originalPrice, loading }) {
179 return (
180 <TextContainer alignment="leading" spacing="loose">
181 <Text role="deletion" size="large">
182 {!loading && formatCurrency(originalPrice)}
183 </Text>
184 <Text emphasized size="large" appearance="critical">
185 {" "}
186 {!loading && formatCurrency(discountedPrice)}
187 </Text>
188 </TextContainer>
189 );
190}
191
192function ProductDescription({ textLines }) {
193 return (
194 <BlockStack spacing="xtight">
195 {textLines.map((text, index) => (
196 <TextBlock key={index} subdued>
197 {text}
198 </TextBlock>
199 ))}
200 </BlockStack>
201 );
202}
203
204function MoneyLine({ label, amount, loading = false }) {
205 return (
206 <Tiles>
207 <TextBlock size="small">{label}</TextBlock>
208 <TextContainer alignment="trailing">
209 <TextBlock emphasized size="small">
210 {loading ? "-" : formatCurrency(amount)}
211 </TextBlock>
212 </TextContainer>
213 </Tiles>
214 );
215}
216
217function MoneySummary({ label, amount }) {
218 return (
219 <Tiles>
220 <TextBlock size="medium" emphasized>
221 {label}
222 </TextBlock>
223 <TextContainer alignment="trailing">
224 <TextBlock emphasized size="medium">
225 {formatCurrency(amount)}
226 </TextBlock>
227 </TextContainer>
228 </Tiles>
229 );
230}
231
232function formatCurrency(amount) {
233 if (!amount || parseInt(amount, 10) === 0) {
234 return "Free";
235 }
236 return `$${amount}`;
237}

If you don't copy and paste the above snippet, there are tweaks you need to make to the provided Shopify frontend code:

  • replace the postPurchaseOffer fetch request with the following snippet to change the /offers request to a POST, allowing you to send inputData to your Gadget app in the request body (make sure to replace the placeholder URL with the URL of your Gadget app!):
JavaScript
1extend("Checkout::PostPurchase::ShouldRender", async ({ inputData, storage }) => {
2 const postPurchaseOffer = await fetch(
3 "https://<gadget-app-name>.gadget.app/offer",
4 {
5 method: "POST",
6 headers: {
7 "Content-Type": "application/json",
8 },
9 body: JSON.stringify({ inputData }),
10 }
11 ).then((res) => res.json());
12
13 await storage.update(postPurchaseOffer);
14
15 return { render: true };
16});
  • replace the default URL for the /sign-changeset request with the URL for your Gadget app (for example: https://post-purchase-demo.gadget.app/sign-changeset)

  • the current Shopify demo is not making use of the returned originalPrice and purchasePrice; you can comment out the below snippets and instead include these fields as part of the storage.inputData destructuring.

JavaScript
1const {
2 variantId,
3 productTitle,
4 productImageURL,
5 productDescription,
6 originalPrice, // <-- add this field
7 discountedPrice, // <-- add this field
8} = storage.initialData;
9
10const changes = [{ type: "add_variant", variantId, quantity: 1 }];
11
12// Extract values from the calculated purchase
13const shipping =
14 calculatedPurchase?.addedShippingLines[0]?.priceSet?.presentmentMoney?.amount;
15const taxes =
16 calculatedPurchase?.addedTaxLines[0]?.priceSet?.presentmentMoney?.amount;
17const total = calculatedPurchase?.totalOutstandingSet.presentmentMoney.amount;
18// const discountedPrice =
19// calculatedPurchase?.updatedLineItems[0].totalPriceSet.presentmentMoney <-- comment out these fields
20// .amount;
21// const originalPrice =
22// calculatedPurchase?.updatedLineItems[0].priceSet.presentmentMoney.amount;

Try it out! 

And you're done, congrats!

If you simulate a fake purchase in our Shopify store, you should now be redirected to the post-purchase upsell screen before our purchase summary. And choosing to purchase the offered product will add it to the order.

You can use this as a template for writing your own post-purchase upsell application. All you need to do is replace the business logic in the POST-offer.js file with your custom code!