Tasks are executed after the job is triggered and are the main building blocks of a job. You can string together as many tasks as you want.


Tasks

All tasks are using Shopify’s REST Resources and can be used with the same general pattern:

await io.shopify.rest.<Resource>.<method>("cacheKey", params)
cacheKey
string
required

Should be a stable and unique cache key inside the run(). See resumability for more information.

params
object

Resource-specific parameters.

all()

Fetch all resources of a given type.

await io.shopify.rest.Variant.all("get-all-variants", {
  autoPaginate: true, // Pagination helper, disabled by default
  product_id: 123456 // Optional, resource-specific parameter
})

Returns

data
array
required

An array of Shopify resources.

pageInfo
PageInfo

The PageInfo object. Will be undefined if there are no further pages.

count()

Fetch the number of resources of a given type.

await io.shopify.rest.Product.count("count-products", {
  product_type: "amazing stuff" // Optional, resource-specific parameters
})

Returns

count
number
required

The number of resources.

find()

Fetch a single resource by its ID.

await io.shopify.rest.Product.find("find-product", {
  id: 123456
})

Returns

A Promise that resolves to the Shopify resource.

save()

Create or update a resource of a given type. The resource will be created if no ID is specified.

// Create a product
await io.shopify.rest.Product.save("create-product", {
  fromData: {
    title: "Some Product",
  },
})

// Update a product
await io.shopify.rest.Product.save("update-product", {
  fromData: {
    id: 123456
    title: "New Product Name",
  },
})

Returns

A Promise that resolves to the Shopify resource.

delete()

Delete an existing resource.

await io.shopify.rest.Product.delete("delete-product", {
  id: 123456
})

Returns

A Promise that resolves to undefined when the resource has been deleted. Throws an error otherwise.

Resources

This is a list of REST Resources that can be used directly as Tasks. They all implement the same methods described above. For resources with non-standard methods, you will have to use the raw Shopify API Client instead - please see the end of this page for further instructions.

Example usage

In this example we’ll create some products in response to a customer sign-up, count them all before and after, and do a few other things too.

client.defineJob({
  id: "shopify-integration-on-customer-created",
  name: "Shopify Integration - On Customer Created",
  version: "1.0.0",
  integrations: {
    shopify
  },
  trigger: shopify.on("customers/create"),
  run: async (payload, io, ctx) => {
    const pre = await io.shopify.rest.Product.count("count-products");

    const firstName = payload.first_name;

    // Create a customized product
    const productOne = await io.shopify.rest.Product.save("create-product-one", {
      fromData: {
        title: `${firstName}'s Teapot`,
      },
    });

    // ..and another one
    const productTwo = await io.shopify.rest.Product.save("create-product-two", {
      fromData: {
        title: `${firstName}'s Mug`,
      },
    });

    const post = await io.shopify.rest.Product.count("count-products-again");

    await io.logger.info(`Created products: ${post.count - pre.count}`)

    // Use our fancy pagination helper
    const allProducts = await io.shopify.rest.Product.all("get-all-products", {
      limit: 1,
      autoPaginate: true,
    });


    const productNames = allProducts.data.map(p => p.title).join(", ")
  
    await io.logger.info(`All product names: ${productNames}`)

    const foundOne = await io.shopify.rest.Product.find("find-product", {
      id: productOne.id,
    });

    if (foundOne) {
      // Get those variants, because we can
      await io.shopify.rest.Variant.all("get-all-variants", {
        product_id: foundOne.id,
      });

      // Maybe that teapot was a bit too much
      await io.shopify.rest.Product.delete("delete-product", {
        id: foundOne.id,
      });
    }

    return {
      message: `Hi, ${firstName}! Bet you'll love this item: ${productTwo.title}`
    }
  },
});

Using the underlying Shopify API Client

You can access the Shopify API Client instance by using the runTask method on the integration:

import "@shopify/shopify-api/adapters/node";
import { Shopify } from "@trigger.dev/shopify";

const shopify = new Shopify({
  id: "shopify",
});

client.defineJob({
  id: "shopify-example-1",
  name: "Shopify Example 1",
  version: "0.1.0",
  trigger: eventTrigger({
    name: "shopify.example",
  }),
  integrations: {
    shopify,
  },
  run: async (payload, io, ctx) => {
    const newProduct = await io.shopify.runTask(
      "create-product",
      async (client, task, io, session) => {
        // We create a session for you to pass to the client
        const product = new client.rest.Product({ session });

        product.title = "Rick's Amazing Teapot";
        product.body_html = "<strong>What a great teapot!</strong>";
        product.vendor = "Astley Inc.";
        product.product_type = "Teapot";
        product.status = "active";

        // This will create the product and update the object
        await product.save({ update: true });

        return product;
      }
    );

    return {
      status: 418,
      statusText: `I'm ${newProduct.title}`,
    };
  },
});