I trust that you found this blog post to be enjoyable. If you are interested in having my team handle your eCommerce setup and marketing for you, Contact Us Click here.

How to Create Shopify Webhooks With the Shopify API

How to Create Shopify Webhooks With the Shopify API

Shopify webhooks are invaluable for building integrations with external applications that need to respond in real-time to events happening in a Shopify store. These webhooks can be created and managed using the Shopify admin dashboard, which offers a user-friendly interface. However, while the admin dashboard is straightforward, it has its limitations. Specifically, it doesn't support automated tasks for managing webhooks. This means developers need to manually log into the admin interface to handle webhook-related tasks, which can be time-consuming and less efficient for automated processes. In this article, I'll show you how to set up authentication using the Shopify API and then utilise the Shopify webhooks API to generate and view webhooks.

Step 1: Generate API credentials from the Shopify admin

  • Log in to your Shopify admin
  • Go to settings – Apps and sales channels

 

  • Click on Develop Apps
  • After then you will see this screen then click on Allow custom app development.

 

  • And if you have already created a custom app, you will see another screen. I am attaching both screens.
  • In the "App Details" section, enter a name for your app and provide an emergency developer email.
  • After filling out the details section, you need to click on "Create App." Then, you will see a new screen to configure the Admin API scope.

 

  • In the Admin API section, select the areas of your store that you want the app to access.
  • After making your selections, click on "Save" to set the scope you require.
  • After then install your app

 

  • After installing the app, you will see a new API credential screen. Then, reveal the token and save it.you’ll use these credentials to make authorized request

 

Step 2: then you have create a shopify api using php at this end url:

https://xxx.com/shopify/{shopify url}/xxx.php

 

<?php

  ini_set('display_errors', 1);

  ini_set('display_startup_errors', 1);

  error_reporting(E_ALL);

 

  define('CLIENT_SECRET',         'd9307d8fb2eca15880067eedc3f4a5441faf0xxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

 function verify_webhook($data, $hmac_header)

 {

 $calculated_hmac = base64_encode(hash_hmac('sha256', $data,     CLIENT_SECRET, true));

 return hash_equals($calculated_hmac, $hmac_header);

 

 }

  $hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];

  $data = file_get_contents('php://input');

  $calculated_hmac = base64_encode(hash_hmac('sha256', $data,     CLIENT_SECRET, true));

 $verified = verify_webhook($data, $hmac_header);

 

 error_log('Webhook verified: '.var_export($verified, true)); 

 if ($verified) {

 $response = $data;

 $log'order.json';

 $order_data = json_decode($response, true);

 $pro_title = $order_data['line_items'][0]['title'];

 $customer_tag = $order_data['customer']['tags'];

 // $customer_id = $order_data['customer']['id'];

 $unsub_title = 'unsubscribe to:'.$pro_title;

 

 

 // $finally_data = array($unsub_title, $customer_tag);

 $inputString = $customer_tag;

 

 

// Split the input string by commas

 $items = explode(', ', $inputString);

 

// Initialize an empty associative array

 $unsubscribeArray = [];

 

 // Loop through the items

 foreach ($items as $item) {

   // Remove the "unsubscribe to:" prefix and trim any extra spaces

   $item = trim(str_replace('unsubscribe to:', '', $item));

 

   // Add the item to the associative array

   $unsubscribeArray[] = 'unsubscribe to:'.$item;

 

 }

 $old_tags = $unsubscribeArray;

 if($unsub_title === $old_tags ){

 $ b_data = 1;

 file_put_contents($log, $b_data );

 }

 else{

 

 $ b_data = 0;

 file_put_contents($log, $b_data );

 }

 die();

 } else {

 

  http_response_code(401);

  }

 ?>

 

 Step 3: Create webhooks in admin

First got to settings > notifications > click on webhooks 

 

 

After that, click on "Create Webhook" to open a popup. In this popup, you can select the event type you want to create or update, such as order creation, product updates, or customer information changes. Additionally, you need to enter the URL of your server where the Shopify API code will handle the webhook requests. This URL is the endpoint that will receive the webhook payloads from Shopify, ensuring your server processes the incoming data appropriately.

 

After creating the webhooks it will generate a client secret key and it will show like this

 

 

Step 4: Authentication credentials in  URL

  • You can use the Admin API access token you generated for your request’s basic HTTP  authorization.
  • https://{shop}.myshopify.com/admin/api/{api-version}/webhooks.json
  • {shop} – The name of your development store.
  • {api-version} – The supported API version that you want to use.

Log in to your Postman and create a new workspace.Paste the URL above and set the header. Example - Then you find the order that you created

 

 

Then you have to add configuration details for a webhook on body to create order of the postman like this 

 

{

   "webhook":{

       "topic":"orders/create",

       "address":"{Add your webhook_url here}",

       "format":"json"

   }

}

 

Now, press the Send button to create your new webhook. You should get a successful response, as you can see below:

 

Back to blog