2023/11/01
 
1 Nov, 2023 | 3 min read

Integrating Shopify with Slack for Quick Order Processing

  • Shania Smith
  • Senior Marketing Officer - Content - WSO2

In today’s fast-paced e-commerce world, staying informed about new orders and providing excellent customer service are crucial for maintaining customer satisfaction and driving sales. Integrating Shopify with Slack and email notifications can streamline these processes and enhance the overall customer experience.

How It Works:

Order Notifications: When a new order is placed on your Shopify store or an existing order is canceled, an automated notification can be sent to a designated Slack channel and to a given email address. This immediate alert allows your team to quickly acknowledge and process orders, reducing the risk of delays or oversights.

In this tutorial, we will learn how to create an integration that will send a notification to a Slack channel along with an email for order creations and cancellations in your Shopify store to make the order handling process efficient. For the integration, we will create a development store, an app in the development store, set up permissions for the app, obtain API secrets, and create webhooks for the triggers Order Create and Order Cancel.

Prerequisites: 

  1. Shopify Partner Account. (You can create one here).
  2. Slack subscription.
  3. Ballerina (Version 2201.7.1 is preferred)

Step 1: Setting Up Shopify:

1.1 Creating a Development Store

To start the integration, let's create a development store in Shopify using your Shopify Partner account.

Figure 1.1: Creating a development store

Navigate to the newly created development store and log in with the same credentials you used for setting up the Shopify Partner account.

1.2 Creating a Developer App

To create a trigger for order creation and order deletion, we need a Shopify App. In your development store, navigate to Settings -> Apps and sales channels -> Develop Apps to create a new app. Let’s name this app, order-manager.

Figure 1.2:Creating a Shopify App

1.3 Obtaining App Secrets

Now our app is ready. To add triggers for Order Create and Order Cancel, we need to set up permissions for the app. Navigate to the newly created app and click on “Configure Admin API Scopes” to set up permissions.

Figure 1.3: App overview

We will need the “read_orders” permission. Select “read_orders'' from the list. Before saving the configuration, we need to be mindful about the “Event version” under “Webhook subscriptions” as we are going to create a webhook for this trigger. This version states the API version of the Shopify Webhook API. Let’s go with the Latest stable version 2023-07.

Figure 1.4: Webhook event version selection

After saving the configurations, our app is ready to be installed. Click on “Install app” and confirm the prompted message.

Once the app is installed, it will reveal the API credentials. Make sure to copy the “Admin API access token” and the “API secret key” as these are needed for the Slack Webhook creation and implementation of our integration.

We are all set to move to the next step of setting up the Slack channel. We’ll re-visit Shopify to create webhooks.

Step 2: Setting Up Slack

We need a Slack workspace, a channel to post updates on orders and an app associated with the workspace with relevant permissions. To interact with the app we need to obtain an OAuth token with required permissions.

2.1 Creating an App with Relevant Permissions 

I have a “Shopify Slack” Slack workspace. Let’s create an app for this. Go to Slack API and create a new app from the “Your Apps” section. Name the app, select the workspace, and create the app.

Figure 1.5: Creating a Slack App

To obtain the OAuth token, navigate to the “OAuth & Permissions” section. 

Figure 1.6 OAuth & Permissions

Let’s set up scopes and get the Bot OAuth token. We’re going to need the “channels:write”, “channels:read”, “chat:write”, and “chat:write.public” Bot Token scopes. Now install the OAuth Token to the workspace using the “Install to Workspace” button. Copy the Bot User OAuth Token to use it in the integration.

Step 3: Implementing Shopify-Slack Connection Using Ballerina Shopify Trigger: 

Let’s start integrating. We are going to write the integration using Ballerina. To get started with a project, let’s run the command below in the terminal.

This will generate the main.bal file and the Ballerina.toml file. The main.bal file is the file in which we write our integration logic. The Ballerina.toml identifies the directory as a Ballerina project (package). It contains all the meta information that is needed to build your package. We can remove all the content from the main.bal file and start coding.

3.1 Integrating Shopify Listener

First, let’s implement the Shopify trigger. Starting with importing the ballerinax/trigger.shopify module.

Next, let’s create a Listener using the Listener config.

I’m using port 8090 here. This can be changed as per the requirement. 


To receive Order Create and Order Cancel notifications, we need to implement the OrdersService.

The OrdersService has multiple built-in triggers for order related actions. Let’s implement only the “onOrdersCreate” and “onOrdersCancelled” methods for this tutorial. Let’s revisit the implementation details after we integrate the Slack Client.

3.2 Integrating Slack Client

To integrate the Slack Client we need import the “ballerinax/slack” module.

Let’s create a configurable to add the OAuth token we generated for the Slack App. This OAuth token will be used as the token in the Slack ConnectionConfig to configure the Slack client.

Now, create a Slack Client using the slackConfig we declared above.

Let's use this “slackClient” to send messages to our #order slack channel upon order creation and cancellation. We can implement the “onOrdersCreate” and “onOrdersCancelled” methods.

We have composed a message to send to the slack channel with the order number, total, and the currency.

3.3 Adding the Email Sender

To add the Choreo email sender to send an email for order creation and cancellation let’s import that sendemail module.

Next declare a configurable to provide the recipient email address and create the sendemail client.

Let’s implement the email notification logic for the onOrdersCreate and onOrdersCancelled methods.

Finally, your code should look like this.

I have imported the “ballerina/log” module to add some logs to each trigger as well. You can implement the other methods as per your requirement.

We are all set to deploy our integration in Choreo!

Step 4: Deploying in Choreo:

In order to deploy our integration in Choreo, the code needs to be in a GitHub or Bitbucket repository. I have already added my ballerina project to shopify-slack-order-notfication-service Github project

4.1 Deploy in Choreo

Let’s create an event-triggered integration component in Choreo to deploy our project.

Navigate to the Create a New Component page in your Choreo Project and select Event-Triggered Integration.

Figure 1.7: Create a New Component page

Provide a name and a description. Make sure to keep the Access Mode as External since we are going to use this to get notifications over the internet.

Figure 1.8: General details in event-triggered integration creation

Next, authorize the version control vendor. Since we are using Github here, let’s go ahead and authorize Github. Here we can give permission for Choreo to view and work with our git repositories.


After successfully authorizing, we can connect our repository to this component. Make sure to select the Ballerina build preset and set the access mode to External.

Fig 1.9 Repository connect in event-triggered integration creation

Next we can select the trigger type. Choreo has a collection of triggers we can use to get a head start in webhook integrations. However, since we have already developed our integration, let’s go with the custom option.

Fig 1.9 Repository connect in event-triggered integration creation

Next we can select the trigger type. Choreo has a collection of triggers we can use to get a head start in webhook integrations. However, since we have already developed our integration, let’s go with the custom option.

Figure 1.10: Trigger type selection in event-triggered integration creation

In the next page, we can decide the type of the trigger under the Trigger Category section. Since we are going to expose an endpoint to create a webhook to Shopify, let’s select the Push option.

Figure 1.11: Trigger category selection in event-triggered integration creation

Click on create.

After the event-triggered integration component is created, navigate to the Deploy page to deploy the integration. Click on “Configure & Deploy” to start the deployment process. We have to provide values to the configurations declared in the code.

Figure 1.12: Setting values for configurations

Add the “API secret key” obtained from the Shopify App as the value for the shopifyApiSecretKey, “OAuth token” obtained from the Slack App as the value for the slackOauthToken, and the recipient email address as the toEmail. Click “Deploy” to proceed with the deployment. Wait for a few seconds for the deployment to be successful. 

After the deployment is complete, we can see the “Invoke URL” for the component. Let’s copy this to use in the Shopify Webhook creation.

Figure 1.13: Invoke URL for the integration

4.2 Create Shopify Webhook


To create Shopify webhooks, we will use the Shopify Webhook API and send a POST request to it.. We can use CURL or any other tool to send the request. The sample curl command will look like the following.

Let’s use Postman to send the request. First, let’s create a webhook for the order/create topic.

Figure 1.14: Create webhook for the order topic

Example payload

Make sure to replace the {{developer-store-url}} with your developer store URL and {{invoke-url}} with the invoke URL obtained from Choreo. Add the “X-Shopify-Access-Token” header and set the “Admin API access token” obtained from the Shopify app as the value. 


Sending this request will create a webhook for orders/create topic. Similarly create another webhook for orders/cancelled using the same method. Sample payload for order cancellation webhook is shown below.

We can now test our integration.

Step 5: Testing the Integration: 

To test the integration, let’s create an order in the Shopify development store and observe our slack channel.

Figure 1.15: Order creation in the Shopify Store

You can now see the message in the slack channel.

Figure 1.16: Order creation notification received in the Slack channel

You can see the email notification in the inbox of the email you provided.

Figure 1.17: Order creation notification received via email

We can test order cancellation by canceling an order in the Shopify development store.

Figure 1.18: Order cancellation in the Shopify store

You can now see the message for the canceled order in the slack channel

Figure 1.19: Order cancellation notification received in the Slack channel

The email notification for cancellation is received.

Fig 1.20 Order cancellation notification received via email

References:

Ballerina trigger.shopify - https://lib.ballerina.io/ballerinax/trigger.shopify/latest

Ballerina Slack - https://central.ballerina.io/ballerinax/slack

Shopify Webhook API - https://shopify.dev/docs/api/admin-rest/2023-04/resources/webhook#post-webhooks

English