Happening now! Shopify Community AMA: Building a Marketing Budget with 2H Media | Ask your marketing budget questions now!

How to identify what triggers a webhook: API or store client?

How to identify what triggers a webhook: API or store client?

Phelumie
Shopify Partner
3 0 4

I have integrated shopify with my app using the rest api and I have also configured webhooks so anytime an action is done i  get events,Awesome.. Now I want to identify what triggered the webhook, is it the api or from the store client itself.. If its the api I want to return 200 immediately.. How do I detect webhook events triggered by my api?? Please any help will be highly appreciated.. Thanks 

Replies 2 (2)

okur90
Shopify Partner
126 20 19

You can identify what triggered the webhook by including a unique identifier in the metadata of the objects you manipulate via the REST API. When you receive a webhook event, you can check the metadata to determine if it was triggered by your app or by another source.

 

When creating or updating a resource (e.g., an order or a product) using the REST API, include a custom metadata field that contains a unique identifier for your app. For example:

 

{
  "product": {
    "title": "My Product",
    "metafields": [
      {
        "key": "app_id",
        "value": "your_unique_app_id",
        "value_type": "string",
        "namespace": "your_app_namespace"
      }
    ]
  }
}

 

When your app receives a webhook event, check the metadata of the resource included in the webhook payload. If the `app_id` metadata field matches your app's unique identifier, this means the webhook was triggered by your app.

 

Based on the metadata check, you can decide whether to return a 200 status code immediately or process the webhook event as needed.

 

Keep in mind that not all webhook events include the entire resource data in their payload. In some cases, you may need to make an additional API call to fetch the resource and its metadata to determine if your app triggered the webhook event.

 

I hope this helps.

Code Slingin, Pixel Wranglin - Laugh it up at onlinex.com.au
Phelumie
Shopify Partner
3 0 4

Thanks for replying but this solution can't help me because there are some endpoint like making a put or delete request i can't attach a metafield  .. What do you think is the best.. Shouldnt be a header to indicate if I want a webhook triggered for this request or webhook header to detect if the webhook was triggered by my api.. Thanks