How to register webhook via API ?

Solved
Highlighted
Shopify Partner
14 0 1

I would like my app to save specific data when an order is created. I know that this can be done via webhooks from Shopify.

This page says what cannot be done when creating a webhook via API, bit says nothing about how to actulally do it.

https://help.shopify.com/en/api/getting-started/webhooks#configure-through-api

Is there a reference that explains how to reguster webhooks from within my app?

Thanks.

BTW, I am using GraphQL

 

0 Likes
Highlighted
Shopify Partner
1866 181 748

This is an accepted solution.

That's well documented for GraphQL in webhookSubscriptionCreate.

// mutation query
mutation webhookSubscriptionCreate($topic: WebhookSubscriptionTopic!, $webhookSubscription: WebhookSubscriptionInput!) {
  webhookSubscriptionCreate(topic: $topic, webhookSubscription: $webhookSubscription) {
    userErrors {
      field
      message
    }
    webhookSubscription {
      id
    }
  }
}

// query variables
{
	"topic": "ORDERS_CREATE",
	"webhookSubscription": {
		"callbackUrl": "https://whatever.requestcatcher.com/",
		"format": "JSON"
	}
}

Hope this helps!

Liked this post? You might also like our fantastic upsell apps Candy Rack and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
0 Likes
Highlighted
Shopify Partner
14 0 1

Karl,

Thanks again.  Just starting out and still learning where things are located. The docs search field does not pull up these article(s)? Now that I know where they are I sould be good.

0 Likes
Highlighted
Shopify Partner
1866 181 748

No problem. I generally prefer to search via google using "shopify api" followed by whatever I need or "shopify graphql" followed by what I need as well as "shopify liquid" again followed by what I need. That search giant sure does a better job at finding stuff ;-)

Liked this post? You might also like our fantastic upsell apps Candy Rack and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
Highlighted
New Member
11 0 0

Can you post PHP code for the same ?

0 Likes
Highlighted
Shopify Partner
1 0 1

This is possible also by making a post request to

 

/admin/api/#{api_version}/webhooks.json

 

with a payload containing the topic and your target url , like:

 

{
  "webhook": {
    "topic": "orders/create",
    "address": "https://wh.example.com/",
    "format": "json"
  }
}

 

 Check the list of topics here:

https://help.shopify.com/en/api/reference/events/webhook

 

and for more information search for Create a new webhook subscription at same page 

Highlighted
Explorer
48 3 5

If you are using php shopify, here's a code snippet to subscribe ORDERS_CREATE webhook 

 

$graphQL = 'mutation webhookSubscriptionCreate($topic: WebhookSubscriptionTopic!, $webhookSubscription: WebhookSubscriptionInput!) {
        webhookSubscriptionCreate(topic: $topic, webhookSubscription: $webhookSubscription) {
            userErrors {
                field
                message
            }
            webhookSubscription {
                id
            }
        }
    }';

$variables = [
    "topic" => "ORDERS_CREATE",
    "webhookSubscription" => [
        "callbackUrl" => "https://example.com/webhook-receiver/",
        "format" => "JSON"
    ]
];

$cfgSdk = array(
    'ShopUrl' => $_SESSION['shopOrigin'],
    'AccessToken' => $_SESSION['shopAccessToken']
);

$shopify = new \PHPShopify\ShopifySDK($cfgSdk);
$shopify->GraphQL->post($graphQL, null, null, $variables);

 

 

 
https://cuedd.com
0 Likes