What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

WEBHOOK KEEPS GETTING REMOVED EVEN THO I VERIFY IT!

WEBHOOK KEEPS GETTING REMOVED EVEN THO I VERIFY IT!

I_KNOW_NOTHING
Shopify Partner
45 0 11

I created a webhook via the Notifications in Shopify Admin, then after a couple of days it is getting removed even tho i follow the step to verify it. Here's my code.

<?php
    define("CLIENT_SECRET",  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
   
    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");
    $verified = verify_webhook($data, $hmac_header);
   
    if ($verified) {

        http_response_code(200);
        $data = json_decode($data, true);
        $handle = $data["handle"];
        error_log("X_SHOPIFY_WEBHOOK_ID: " . $_SERVER["HTTP_X_SHOPIFY_WEBHOOK_ID"]);
        error_log("-----");

    }
    else {
        http_response_code(401);
        error_log("Error 401 Stopped working!");
    }
?>

Also just want to add note that the webhook has receiving duplicate, I wonder if this is related. If so, how do I solve it?

I_KNOW_NOTHING_0-1689963514516.png

Any help is appreciated. Thank you!



Replies 7 (7)

SBD_
Shopify Staff
1831 273 421

Hey @I_KNOW_NOTHING 

 

Code is looking good - your logs indicate a 200 is returned.

 

Is the endpoint always online/available? If the webhook can't be delivered 19 times (over 48 hours) it will be removed. More info.

 

Are you able to create the webhook using an app instead of in admin? This will give you access to webhook delivery metrics to troubleshoot delivery failures and get information on performance.

Scott | Developer Advocate @ Shopify 

I_KNOW_NOTHING
Shopify Partner
45 0 11

Hey @SBD_ ,

Yea, the endpoint is always online/available as i was using a cpanel webhost rather than a heroku. 

Are you able to create the webhook using an app instead of in admin? - Regarding this one, do you have a simpler version of this instead of using the "npm init @shopify/app@latest" as this seems like overkill just for my simple webhook function.


SBD_
Shopify Staff
1831 273 421

For sure. You can create a custom app in the Shop's admin to get an API key and then hit the webhooks endpoint directly. 

Scott | Developer Advocate @ Shopify 

I_KNOW_NOTHING
Shopify Partner
45 0 11

Hey @SBD_ 

I tried what you recommend and create a private app but still following the verification code i sent above. I encountered this issue.


I change the CLIENT_SECRET with the value of the custom apps API Secret Key

I_KNOW_NOTHING_1-1690448712660.png


But it isn't getting verified at all (unlike the initial webhooks I had)

I_KNOW_NOTHING_2-1690448782132.png


I actually tried using the value of Admin API access token as well as API key (all of them returned not verified)

SBD_
Shopify Staff
1831 273 421

Hey @I_KNOW_NOTHING 

 

You're correct in using the API secret key. Confirming the app has subscribed to these webhooks notifications (instead of via admin)?

Scott | Developer Advocate @ Shopify 

I_KNOW_NOTHING
Shopify Partner
45 0 11

Hi @SBD_
Sorry if I took too long to reply but yes i am subscribed via the Notifications tab.

SBD_
Shopify Staff
1831 273 421

Hey @I_KNOW_NOTHING 

 

To use the webhook delivery metrics, you'll need to subscribe to the webhooks through the API (REST / GraphQL) and verify them with your app's key.

 

Subscriptions created through the admin need to be verified using the key in the admin and wont show on the delivery metrics.

Scott | Developer Advocate @ Shopify