FROM CACHE - de_header

Shopify dynamic web hook

Gelöst

Shopify dynamic web hook

appstarter123
Shopify Partner
13 0 0

shopify offers a webhook api for listen to events maybe a product is updated or deleted. I want to build an app where people can connect their shopify store and then I import all products in my database. But I also have to create a webhook for each user that has connected their shop. How can I use dynamic webhooks now ?

I have seen a lot of business that you can connect your shop/multiple shops and you get everytime notifications when a event happend. So its possible but the question is how I create now a webhook for every use that has connected their shop ?

I know how can I create a webhook hardcoded but not dynamically

1 AKZEPTIERTE LÖSUNG

Gabe
Shopify Staff
18440 2890 4254

Erfolg.

Hey @appstarter123 

 

Thanks for your question and I can see one of our top experts answered your question in the other thread in the EN community. You have reached the German community here.

 

It would also be a small gesture of courtesy that when you ask a question and another person answers your question that you at least acknowledge to the other person that you got their response.

 

Regarding your question, creating dynamic webhooks for each user in Shopify involves several steps, and it's typically done by using Shopify's API. Here are some options:

  1. API authentication: When a user connects their Shopify store to your app, you'll need to authenticate with Shopify's API. This step gives you an access token that you can use for further API calls. You should save this access token in your database, linked with the user's account.

  2. Webhook creation: When a user connects their store, you'll want to create a webhook for the relevant events you want to listen to. This is done with a POST request to the /admin/api/2023-04/webhooks.json endpoint (or whatever the current version is). The body of the request should include the address of your endpoint (the URL that Shopify will send the webhook data to), the topic of the webhook (the event you want to listen to, like products/update), and the format (usually json).

Here's an example of what that could look like using the requests library:

 

import requests

shop_url = "https://{}/admin/api/2023-04/webhooks.json".format(user_shop_domain)
headers = {
    "X-Shopify-Access-Token": user_access_token,
    "Content-Type": "application/json",
}
data = {
    "webhook": {
        "topic": "products/update",
        "address": "https://yourapp.com/webhook_endpoint",
        "format": "json",
    }
}

response = requests.post(shop_url, headers=headers, json=data)

 

Shopify will send a POST request to the address you specified whenever the event occurs. Your app needs to be able to handle these requests, parse the JSON data, and do something with it (like updating the product in your database). For security, you should verify that incoming webhooks are actually from Shopify. Shopify includes a X-Shopify-Hmac-SHA256 header in the request, which is a HMAC of the body content, using your app's shared secret as the key. You should compute the HMAC on your side and compare it to the header to verify the request.

 

If a user disconnects their store from your app, you should delete the associated webhooks. This is done with a DELETE request to the /admin/api/2023-04/webhooks/{webhook_id}.json endpoint. You should store the access token and associated shop domain of each user securely in your database when they connect their shop. This way, you can make authenticated requests to the Shopify API on behalf of each individual user. Also, ensure you handle rate limits and potential errors from the Shopify API to make your app more reliable.

Gabe | Social Care @ Shopify
 - War meine Antwort hilfreich? Klicke Like um es mich wissen zu lassen! 
 - Wurde deine Frage beantwortet? Markiere es als Akzeptierte Lösung 
 - Um mehr zu erfahren, besuche das Shopify Help Center oder den Shopify Blog

Lösung in ursprünglichem Beitrag anzeigen

1 ANTWORT 1

Gabe
Shopify Staff
18440 2890 4254

Erfolg.

Hey @appstarter123 

 

Thanks for your question and I can see one of our top experts answered your question in the other thread in the EN community. You have reached the German community here.

 

It would also be a small gesture of courtesy that when you ask a question and another person answers your question that you at least acknowledge to the other person that you got their response.

 

Regarding your question, creating dynamic webhooks for each user in Shopify involves several steps, and it's typically done by using Shopify's API. Here are some options:

  1. API authentication: When a user connects their Shopify store to your app, you'll need to authenticate with Shopify's API. This step gives you an access token that you can use for further API calls. You should save this access token in your database, linked with the user's account.

  2. Webhook creation: When a user connects their store, you'll want to create a webhook for the relevant events you want to listen to. This is done with a POST request to the /admin/api/2023-04/webhooks.json endpoint (or whatever the current version is). The body of the request should include the address of your endpoint (the URL that Shopify will send the webhook data to), the topic of the webhook (the event you want to listen to, like products/update), and the format (usually json).

Here's an example of what that could look like using the requests library:

 

import requests

shop_url = "https://{}/admin/api/2023-04/webhooks.json".format(user_shop_domain)
headers = {
    "X-Shopify-Access-Token": user_access_token,
    "Content-Type": "application/json",
}
data = {
    "webhook": {
        "topic": "products/update",
        "address": "https://yourapp.com/webhook_endpoint",
        "format": "json",
    }
}

response = requests.post(shop_url, headers=headers, json=data)

 

Shopify will send a POST request to the address you specified whenever the event occurs. Your app needs to be able to handle these requests, parse the JSON data, and do something with it (like updating the product in your database). For security, you should verify that incoming webhooks are actually from Shopify. Shopify includes a X-Shopify-Hmac-SHA256 header in the request, which is a HMAC of the body content, using your app's shared secret as the key. You should compute the HMAC on your side and compare it to the header to verify the request.

 

If a user disconnects their store from your app, you should delete the associated webhooks. This is done with a DELETE request to the /admin/api/2023-04/webhooks/{webhook_id}.json endpoint. You should store the access token and associated shop domain of each user securely in your database when they connect their shop. This way, you can make authenticated requests to the Shopify API on behalf of each individual user. Also, ensure you handle rate limits and potential errors from the Shopify API to make your app more reliable.

Gabe | Social Care @ Shopify
 - War meine Antwort hilfreich? Klicke Like um es mich wissen zu lassen! 
 - Wurde deine Frage beantwortet? Markiere es als Akzeptierte Lösung 
 - Um mehr zu erfahren, besuche das Shopify Help Center oder den Shopify Blog