Back to APIs

Automate online store management, products, orders, and customers.

Using the Shopify API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Shopify background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Example code using Shopify

Below is a working code example of how you can use Shopify with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
2
import { shopifyApi, ApiVersion } from "@shopify/shopify-api";
3
import z from "zod";
4
5
// Make sure to include this import
6
// see: https://github.com/Shopify/shopify-api-js/blob/main/packages/shopify-api/docs/guides/runtimes.md
7
import "@shopify/shopify-api/adapters/node";
8
9
// Create a Shopify custom app: https://shopify.dev/tutorials/build-a-shopify-app-with-node-and-react
10
// Shopify SDK: https://github.com/Shopify/shopify-api-js
11
// https://admin.shopify.com/store/<STORE_ID>/settings/apps/development/<APP_ID>/api_credentials
12
// Initialize the Shopify API client
13
const shopify = shopifyApi({
14
apiKey: process.env.SHOPIFY_API_KEY,
15
apiSecretKey: process.env.SHOPIFY_API_SECRET!,
16
scopes: ["write_products"], // Scopes can be found on the Shopify Admin Apps page
17
hostName: process.env.SHOPIFY_HOSTNAME!, // Ex: <STORE_ID>.myshopify.com
18
apiVersion: ApiVersion.October23,
19
isEmbeddedApp: false,
20
});
21
22
// Define a job to update product variant prices
23
client.defineJob({
24
id: "shopify-product-variant-price",
25
name: "Shopify update product variant price",
26
version: "1.0.0",
27
trigger: eventTrigger({
28
name: "shopify-product-variant-price",
29
schema: z.object({
30
// The product variant ID can be found in the Shopify Admin product variant URL
31
// (e.g. https://admin.shopify.com/store/<STORE_NAME>/products/<PRODUCTS_ID>/variants/<VARIANT_ID>)
32
productVariantId: z.number(),
33
price: z.number(), // The new price of the product variant
34
}),
35
}),
36
run: async (payload, io, ctx) => {
37
const { productVariantId: id, price } = payload;
38
39
// Use io.runTask to make the SDK call resumable and log-friendly
40
await io.runTask(
41
"Shopify update product variant price",
42
async () => {
43
// Initialize a Shopify session
44
const session = shopify.session.customAppSession(
45
process.env.SHOPIFY_HOSTNAME!
46
);
47
48
// The access token can be found in the Shopify Admin Apps page
49
session.accessToken = process.env.SHOPIFY_ADMIN_ACCESS_TOKEN;
50
51
// Initialize a Shopify REST client
52
const client = new shopify.clients.Rest({ session });
53
54
// Update the product variant price
55
await client.put({
56
path: `variants/${id}`,
57
data: { variant: { id, price } },
58
});
59
},
60
61
// Add metadata to improve how the task displays in the logs
62
{ name: "Shopify update product variant price", icon: "shopify" }
63
);
64
},
65
});