What are webhooks used for?
Okay, now that you’re familiar with webhooks, it’s time to go over what they’re typically used for, which includes:
- Real-time notifications – If you’re running an e-commerce platform, you can use webhooks to notify your inventory management system whenever a new order is placed.
- Data synchronization – Have two or more systems in place? No problem, because webhooks can push notifications from one system to the other whenever a change happens (e.g., a user updates their profile).
- Workflow automation – You can make a webhook trigger to send a notification in Slack whenever a new issue is created in a project management tool (e.g., Jira).
- Monitoring and alerts – If you have a monitoring system in place, you can trigger your webhook to send an alert (via email, sms, etc.) to the responsible person/team whenever a server goes down.
- API communication – Besides helping apps communicate, webhooks can also be middlemen in API communication. For instance, when a transaction is completed, a payment gateway can use webhooks to notify your application so your system can respond accordingly in time.
- Third-party webhook integration – As you’ll shortly see, you can integrate webhooks with various third-party software, such as CRM systems, project management tools, e-commerce platforms, etc.
Examples of webhooks
Now, as promised, let me show you some real-world uses and examples of webhooks.
Slack
Slack allows you to modify the platform to send a notification to a channel whenever a critical error occurs in your web application. For example, when the error occurs, the webhook will send a detailed message to a specific Slack channel, with the payload containing all the details and a link to the logs.
Here’s an example of a JSON payload:
{
"channel": "#alerts",
"username": "ErrorBot",
"text": "A critical error occurred in the web application:\n*Error Message:* Unexpected token in JSON\n*Timestamp:* 2024-07-25T14:30:00Z\n*Logs:* <http://example.com/logs/error-1234>",
"icon_emoji": ":warning:"
}
Copy
And here’s a code breakdown with principles you can apply to other examples as well:
"channel":
– Specifies the Slack channel where the message should be sent."username":
– Sets the username that appears next to the message in the Slack channel."text":
– The main content of the message that provides the information about the error."icon_emoji":
– Sets the emoji that appears next to the message.
Also, did you know that you can combine Slack, Zapier, and ESPs? My colleague Piotr talks about it in his Slack email integration article.
GitHub
With GitHub webhooks, you can make a webhook send a notification to a CI/CD service to trigger a build when a new push is made to a repository.
Here’s what you’d have to do:
- Go to your repository on GitHub
- Navigate to Settings → Webhooks → Add webhook
- Enter the Payload URL of your CI/CD service (e.g., Jenkins)
- Select application/json as the Content type
- Choose Just the push event for the event to trigger the webhook
- Click Add webhook
Then, you need to set up a new job to handle builds in your CI/CD service and configure it to listen for incoming webhooks from GitHub. And finally, parse the incoming JSON payload to extract relevant information.
JSON example: