Why to use webhooks?
APIs send data when requested. But webhooks can send data when an event is triggered to the callback url. So the client doesn't need to check repeatedly, if new data is available.
How are webhooks different from APIs?
|Client||client requests the API url to get data||Webhooks call the client url to send|
|Server||API Clients dont need a running server. They can request data when they want||Webhook callback need active servers. They have to be active all the time accept connections.|
|Push||APIs cannot push data to other systems||Webhooks can push data to a given url|
|Invoke||On API Request||On Event Trigger|
|request||Comes into the server||Goes out to callback URL|
|New data||Check repeatedly||Pushed to callback url|
How to make Webhooks ?
Webhooks are similar to APIs with respect to providing data to the clients. They can be built within the same API server. The user needs to provide a callback url, where they need the data send.
Setting up webhook reciever
- Have a running server with a valid url publicly accessible
- The URL should accept a POST request
- URL should reply with a 201 response to let the webhook know that data has been sent correctly.
Registering the webhook url is a one time configuration activity that API user performs. The callback url from every user is different. So the API server can send data when available to different clients.
Steps to create webhook with API
- Create an endpoint to accept webhook callback url for each user
- Create a POST API request within the API server to callback url
- Check for valid response from callback url and save response in database