Solved

Webhook not registered

Pratham_Jani
Shopify Expert
42 1 1
/*webhook*/

$chh = curl_init();

$params = [
  'webhook' => [
    'topic' => 'app/uninstalled',
    'address' => 'https://example.herokuapp.com/delete.php',
    'format' => 'json'  
  ]
];

$json_string_params = json_encode($params);

$headers = array(
  'X-Shopify-Access-Token:'.$access_token,
  'Content-Type:application/json'
);

curl_setopt($chh, CURLOPT_URL, 'https://example.myshopify.com/admin/webhooks.json');
curl_setopt($chh, CURLOPT_HTTPHEADER, $headers);
curl_setopt($chh, CURLOPT_HEADER, false);
curl_setopt($chh, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($chh, CURLOPT_POSTFIELDS, $json_string_params);
curl_setopt($chh, CURLOPT_RETURNTRANSFER, true);

$resultt = curl_exec($chh);

$final = json_decode($resultt);

/*webhook*/

This is my code and. After firng this one i'm getting below response but still webhook not registered.

 

stdClass Object
(
    [webhook] => stdClass Object
        (
            [id] => 475440480367
            [address] => https://example.herokuapp.com/delete.php
            [topic] => app/uninstalled
            [created_at] => 2019-01-18T05:04:20-05:00
            [updated_at] => 2019-01-18T05:04:20-05:00
            [format] => json
            [fields] => Array
                (
                )
            [metafield_namespaces] => Array
                (
                )
        )
)

Can anyone review my code?

Accepted Solution (1)
Pratham_Jani
Shopify Expert
42 1 1

This is an accepted solution.

is this headers are correct?is this headers are correct?This is my request bulk.This is my request bulk.

View solution in original post

Replies 14 (14)

KarlOffenberger
Shopify Partner
1873 184 901

Hi,

 

Have you queried the webhooks endpoint to check i.e. GET /admin/webhooks.json ? Webhooks created by your app do not show up in admin UI as they're bound to your app, not the "admin app".

 

Sadly I cannot offer any review on PHP because I am not a PHP developer, though looking at the cURL it all looks okay. So above is just a suggestion to double check.

 

Best wishes!

Pratham_Jani
Shopify Expert
42 1 1

yes, i did queried as you specified. 

But its still not showing our registered webhooks in json format. It shows only from the admin side manually. 

 

Any possibilities?

KarlOffenberger
Shopify Partner
1873 184 901

Strange because the response you mention clearly shows the webhook ID so I'd assume it was created successfully. Could you try the same POST operation using a REST client such as Postman or Insomnia, using the same API credentials as your app and making sure you have cookies disabled in your request - just to narrow down the possibilities whether it's the API request itself, or maybe something in the PHP client request.

Pratham_Jani
Shopify Expert
42 1 1

I tried in Postman also. But result was same!

i don't know wthat to do now!

KarlOffenberger
Shopify Partner
1873 184 901

So again, just to confirm, POST /admin/webhooks.json with

{
  "webhook": {
    "topic": "app/uninstalled",
    "address": "https://test.requestcatcher.com/",
    "format": "json"
  }
}

via a REST client doesn't work?

 

I've just tested the endpoint and was able to create and confirm the webhook was registered.

KarlOffenberger
Shopify Partner
1873 184 901

If you're still having no luck with any of that, I suggest you post the X-Request-Id from your response header so that Shopify staff can look in to the details of the request.

Pratham_Jani
Shopify Expert
42 1 1

OK i'm getting you in REST. Can you post your flow with you've tried!. 

KarlOffenberger
Shopify Partner
1873 184 901

No special flow, just 1 single request like I posted above.

 

Pratham_Jani
Shopify Expert
42 1 1

i'm firing with this flow.

 

https://{username}:{password}@{shop}.myshopify.com/admin/webhooks.json

Is it correct?

KarlOffenberger
Shopify Partner
1873 184 901

 

curl --request POST \
  --url https://store.myshopify.com/admin/webhooks.json \
  --header 'authorization: Basic XXXXXXX' \
  --header 'content-type: application/json' \
  --data '{
  "webhook": {
    "topic": "app/uninstalled",
    "address": "https://test.requestcatcher.com/",
    "format": "json"
  }
}'
Pratham_Jani
Shopify Expert
42 1 1

This is an accepted solution.

is this headers are correct?is this headers are correct?This is my request bulk.This is my request bulk.

Pratham_Jani
Shopify Expert
42 1 1

@KarlOffenberger

 

Hey now it's working like a charm, I've checked with the request catcher and I got a response. But still, one thing is remaining that now I've to get that response in my app file e.g. /delete.php.  So for that, I'm finding receiving the code if you can help me with that it'd be Veeery appreciable for me. :))))

KarlOffenberger
Shopify Partner
1873 184 901

Hey again. Sorry, but I cannot help you out with PHP because it's simply not a language I know.

 

The process of creating a webhook handler on your side is pretty straightforward:

 

  1. Must be via HTTPS
  2. Must be a POST endpoint
  3. Should verify HMAC
  4. Should return 200 response
  5. Should return quickly

General advice and gotchas for webhooks are:

 

  • Don't process them immediately - receive > queue up > respond 200. Work through your queue separately.
  • Deduplicate - you will be getting dupes for a number of reasons known and unknown. Handle them gracefully.
  • Don't rely on webhooks being reliable. Assume they're not. Program defensively.
  • Don't rely on your app being reliable. It isn't. There isn't such a thing as bullet proof code. Handle re-registration and import.

During development use any of the free or paid localhost tunnelers. Makes life easier.

 

Good luck!

devsb399
Tourist
14 0 3

Hello @Pratham_Jani  & @KarlOffenberger,

 

We are having the same problem where webhook API is giving response with webhook id, But when we open let's say examplestore/admin/webhooks.json, At there, the webhook array is empty and our webhook is not registered. I have written about this issue in this forum https://community.shopify.com/c/Shopify-APIs-SDKs/Shopify-webhook-api-is-giving-response-but-not-reg....

 

If you have overcome the problem, It would be really kind of you to help us with this, Right now we are stuck in it and clueless.

 

Awaiting your response. Thanks