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.

    import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
    import { shopifyApi, ApiVersion } from "@shopify/shopify-api";
    import z from "zod";
    // Make sure to include this import
    // see: https://github.com/Shopify/shopify-api-js/blob/main/packages/shopify-api/docs/guides/runtimes.md
    import "@shopify/shopify-api/adapters/node";
    // Create a Shopify custom app: https://shopify.dev/tutorials/build-a-shopify-app-with-node-and-react
    // Shopify SDK: https://github.com/Shopify/shopify-api-js
    // https://admin.shopify.com/store/<STORE_ID>/settings/apps/development/<APP_ID>/api_credentials
    // Initialize the Shopify API client
    const shopify = shopifyApi({
    apiKey: process.env.SHOPIFY_API_KEY,
    apiSecretKey: process.env.SHOPIFY_API_SECRET!,
    scopes: ["write_products"], // Scopes can be found on the Shopify Admin Apps page
    hostName: process.env.SHOPIFY_HOSTNAME!, // Ex: <STORE_ID>.myshopify.com
    apiVersion: ApiVersion.October23,
    isEmbeddedApp: false,
    });
    // Define a job to update product variant prices
    client.defineJob({
    id: "shopify-product-variant-price",
    name: "Shopify update product variant price",
    version: "1.0.0",
    trigger: eventTrigger({
    name: "shopify-product-variant-price",
    schema: z.object({
    // The product variant ID can be found in the Shopify Admin product variant URL
    // (e.g. https://admin.shopify.com/store/<STORE_NAME>/products/<PRODUCTS_ID>/variants/<VARIANT_ID>)
    productVariantId: z.number(),
    price: z.number(), // The new price of the product variant
    }),
    }),
    run: async (payload, io, ctx) => {
    const { productVariantId: id, price } = payload;
    // Use io.runTask to make the SDK call resumable and log-friendly
    await io.runTask(
    "Shopify update product variant price",
    async () => {
    // Initialize a Shopify session
    const session = shopify.session.customAppSession(
    process.env.SHOPIFY_HOSTNAME!
    );
    // The access token can be found in the Shopify Admin Apps page
    session.accessToken = process.env.SHOPIFY_ADMIN_ACCESS_TOKEN;
    // Initialize a Shopify REST client
    const client = new shopify.clients.Rest({ session });
    // Update the product variant price
    await client.put({
    path: `variants/${id}`,
    data: { variant: { id, price } },
    });
    },
    // Add metadata to improve how the task displays in the logs
    { name: "Shopify update product variant price", icon: "shopify" }
    );
    },
    });
    ,