Introduction
When you create a webhook, you specify a URL and subscribe to event types. When an event that your webhook is subscribed to occurs, GitHub will send an HTTP request with data about the event to the URL that you specified. If your server is set up to listen for webhook deliveries at that URL, it can take action when it receives one.
This article describes how to write code to let your server listen for and respond to webhook deliveries. You will test your code by using your computer or codespace as a local server.
Setup
In order to test your webhook locally, you can use a webhook proxy URL to forward webhooks from GitHub to your computer or codespace. This article uses smee.io to provide a webhook proxy URL and forward webhooks.
Get a webhook proxy URL
- In your browser, navigate to https://smee.io/.
- Click Start a new channel.
- Copy the full URL under "Webhook Proxy URL". You will use this URL in the following setup steps.
Forward webhooks
-
If you don't already have smee-client installed, run the following command in your terminal:
Shell npm install --global smee-client
npm install --global smee-client -
To receive forwarded webhooks from smee.io, run the following command in your terminal. Replace
WEBHOOK_PROXY_URLwith your webhook proxy URL from earlier.Shell smee --url WEBHOOK_PROXY_URL --path /webhook --port 3000
smee --url WEBHOOK_PROXY_URL --path /webhook --port 3000You should see output that looks like this, where
WEBHOOK_PROXY_URLis your webhook proxy URL:Shell Forwarding WEBHOOK_PROXY_URL to http://127.0.0.1:3000/webhook Connected WEBHOOK_PROXY_URL
Forwarding WEBHOOK_PROXY_URL to http://127.0.0.1:3000/webhook Connected WEBHOOK_PROXY_URLNote that the path is
/webhookand the port is3000. You will use these values later when you write code to handle webhook deliveries. -
Keep this running while you test out your webhook. When you want to stop forwarding webhooks, enter Ctrl+C .
Create a webhook
-
Create a webhook with the following settings. For more information, see Creating webhooks.
- For the URL, use your webhook proxy URL from earlier.
- If you have an option to choose the content type, use JSON.
Write code to handle webhook deliveries
In order to handle webhook deliveries, you need to write code that will:
- Initialize your server to listen for requests to your webhook URL
- Read the HTTP headers and body from the request
- Take the desired action in response to the request
You can use any programming language that you can run on your server.
The following examples print a message when a webhook delivery is received. However, you can modify the code to take another action, such as making a request to the GitHub API or sending a Slack message.
Ruby example
This example uses the Ruby gem, Sinatra, to define routes and handle HTTP requests. For more information, see the Sinatra README.
Ruby example: Install dependencies
To use this example, you must install the sinatra gem in your Ruby project. For example, you can do this with Bundler:
-
If you don't already have Bundler installed, run the following command in your terminal:
Shell gem install bundler
gem install bundler -
If you don't already have a Gemfile for your app, run the following command in your terminal:
Shell bundle init
bundle init -
If you don't already have a Gemfile.lock for your app, run the following command in your terminal: