Re: Webhooks/Event Listeners

Webhooks/Event Listeners

developer8971
Tourist
44 0 2

Hi Guys,

New to Shopify API in terms of webhooks/event listeners.  I'm reading that Shopify needs a URL to send the post to when the event is triggered.  I'm new to webservices and don't know what I need to code or setup in order to receive these posts from Shopify server.  Do I need to setup an apache server?  Where do I start with this?

Replies 8 (8)

KarlOffenberger
Shopify Partner
1873 184 902

Webhooks are just HTTP POST requests being sent to some URI endpoint. How that endpoint handles them, if at all, is up to the application you write. Apache is just an HTTP server - won't really help you with the "what to do with that webhook I received" part.

First, I'd recommend you setup a fake webhook endpoint using something like requestcatcher. Have your Shopify webhook point there and while on that site, watch for the incomming events, examine them etc.

Then you'll need to create your own. I'd start with local development using some sort of tunnel to make your machine accessible (ngrok etc), and write a simple HTTP request handler in your favorite language. Point the Shopify webhook to that endpoint and see how you can code something to process the webhook's body and respond to that.

Once you're happy... well then you have a bunch of options. Host it yourself, go serverless using AWS Lambda or similar or seek out an overpriced "webhook as a service" provider 😉

developer8971
Tourist
44 0 2

Thanks for the help!

What is this tunnel ngrok etc?  does this open up my machine to receive requests?  Like a server?  I thought that using a Java class like HTTPServer (https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer....)

KarlOffenberger
Shopify Partner
1873 184 902

ngrok is reomote port forwarding. Basically, your computer is on a network, say a home, corporate or public network where the router has a public IP from your ISP, but within the network, the router and each device connected to it have private IPs. Which is good. You can to the internet but the internet doesn't get to you.

And that is sometimes a problem e.g. when you are developing something and need external services to call back to your development machine. You don't want to have to deploy each and every time to a public server. For instance, when you want to develop some webhook handlers for Shopify's webhooks.

So you have a few options how to expose your computer from within a private network. Configure your router if it allows you to and if you want to mess around with that. Limits you to the router though. What if you're sitting in a cafe or airport?

If you have a public server you can establish an SSH tunnel. Free and secure if you already use that server anyway.

Then there's a few freemium and paid services such as ngrok that come along with nice to have features like monitoring, domains etc.

KarlOffenberger
Shopify Partner
1873 184 902

p.s. if I messed up some of the network terminology up there, don't shoot me. I am a software engineer, not a network specialist - if anything goes wrong with the network I usually fall on my knees and pray or perform a rain dance until some heavenly admin emerges and fixes life 😄

developer8971
Tourist
44 0 2

Hi, thanks for the detailed explanation!

So basically (just to confirm my understanding), the public server on my ISP can receive the shopify response for a webhook.  But in order for the response to go from my ISP public server to my development machine (which is on a private network), I have to use ngrok to open up the tunnel between my machine and the ISP public server endpoint?

In the case of the requestcatcher you suggested, shopify will send the webhook HTTP response to their server via the URL they generated for me.  

Do I need ngrok if I house my code (to process the webhook response) on a server somewhere (like website domain/hosting)?  I'm trying to think ahead to make it machine independent?

KarlOffenberger
Shopify Partner
1873 184 902

ngrok would open up a secure tunnel between your dev machine and their server. Then you can share your unique address from ngrok while doing demos or developing.

The requestcatcher service just creates a catch all POST handler so you can have a quick look at webhook payloads etc. Yes, stays on their server - you won't be able to do anything else with it.

No you do not need ngrok if you will host your service on some server - ngrok isn't meant to replace that and having your service run on your home machine isn't the purpose of ngrok. ngrok is just a developer workflow aid. Like I said, it will spare you the trouble of setting a CI / CD flow not to mention remote debugging woes etc. which are all just time snatchers and hurdles during development.

But even without, nothing wrong with leaving ngrok out - was just a suggestion - we've all developed without many a fancy tool and managed to come this far 😉

developer8971
Tourist
44 0 2

Thank you very much for sharing your insight!

In this case, I think it'd be faster to code the mini application by deploying the code via an existing server.  I'm not actually clear (lack of experience) as to how this would work?  I could code a php script, place it on the server but then how does it hookup to the server to listen for the incoming webhook HTTP response?

KarlOffenberger
Shopify Partner
1873 184 902

Hehe... I think each one of those questions is worth an entire blog series 😉