Google Cloud Pub/Sub webhook delivery

Tracking traffic from Shopify's platform can be overwhelming, especially as you grow your app. Integrating with Google Cloud Pub/Sub allows your app to consume events at its own pace and handle large volumes of webhook traffic.

This tutorial shows you how to use Google Cloud Pub/Sub to manage webhook events for your app.

Requirements

  • You've created a public or custom app. Private apps can't integrate with Google Cloud Pub/Sub.
  • You've set up a Google Cloud Pub/Sub project.
  • You've created at least one topic in the project.

For more information on creating projects and topics, refer to Google's Pub/Sub quickstart guide.

Step 1: Integrate your app with Google Cloud Pub/Sub

To integrate your app with Google Cloud Pub/Sub, you need to get the Shopify service account address from the Partner Dashboard, and then add that address to a Google Cloud Pub/Sub topic.

Get the Shopify service account address

  1. Go to your Partner Dashboard.
  2. Go to Apps, and then click the name of your app.
  3. Click App setup.
  4. In the Event Subscriptions section, under Google Cloud Platform - Pub/Sub, copy the Shopify service account address.

Add the service account address to Google Cloud Pub/Sub

  1. In the Google Cloud Console, go to Pub/Sub > Topics.
  2. Next to a topic, click and then click View permissions.
  3. Click ADD MEMBER.
  4. Paste the Shopify service account address into the New members text box.
  5. In the Role drop-down menu, select Pub/Sub Publisher.
  6. Click Save.

You can now add webhook subscriptions to the Google Cloud Pub/Sub topic using Shopify's APIs.

Step 2: Subscribe to webhooks using GraphQL and Google Cloud Pub/Sub

After you've integrated your app with Google Cloud Pub/Sub, you can use GraphQL mutations to subscribe to, update, and delete webhook subscriptions.

Create a new webhook subscription using GraphQL

To create a new webhook subscription using GraphQL and Google Cloud Pub/Sub, you can use the pubSubWebhookSubscriptionCreate mutation, as shown in the following example. When using this mutation, set pubSubProject to the ID of your Google Cloud Platform project, and set pubSubTopic to the ID of the topic you set up in Google Cloud Pub/Sub.

POST /admin/api/2021-07/graphql.json

Update webhook subscriptions using GraphQL

To update a Google Cloud Pub/Sub webhook subscription using GraphQL, you can use the pubSubWebhookSubscriptionUpdate mutation, as shown in the following example. When using this mutation, set pubSubProject to the ID of your Google Cloud Platform project, and set pubSubTopic to the ID of the topic you set up in Google Cloud Pub/Sub.

POST /admin/api/2021-07/graphql.json

Delete webhook subscriptions using GraphQL

You can delete webhook subscriptions for Google Cloud Pub/Sub using the webhookSubscriptionDelete mutation.

Step 3: Subscribe to webhooks using REST and Google Cloud Pub/Sub

After you've integrated your app with Google Pub/Sub, you can use REST requests to subscribe to, update, and delete webhook subscriptions.

Create a new webhook subscription using REST

Subscribing to a webhook using the REST API and Pub/Sub is similar to subscribing to an HTTP webhook, but you set the address field to pubsub://{project-id}:{topic-id}, where {project-id} is the ID of your Google Cloud Platform project, and {topic-id} is the ID of the topic you set up in Google Cloud Pub/Sub.

POST https://{shop_id}.myshopify.com/admin/api/2021-07/webhooks.json

Update or delete webhook subscriptions using REST

You can update webhook subscriptions and delete them by ID using the Webhook resource.

Google Cloud Pub/Sub payloads

When you're using Google Cloud Pub/Sub to manage your webhooks, you receive a JSON payload for topics as described in the Webhook resource documentation. However, unlike with an HTTP webhook, you don't need to perform HMAC verification.

Refer to the Google Cloud Pub/Sub documentation for payload size limits.

Next steps