# Handle Deliveries

When you create a webhook, you specify a URL and when an event occurs, carbonregistry 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.

In this example we will go over how to setup the webhook and receive the data when an event happens. We will use expressJS for defining the webhook handler. Repository for the code is [here](https://github.com/Mojoflower-garden/webhook-handler/tree/main).

### Setup

In order to test your webhook locally, you can use a webhook proxy URL to forward webhooks from CarbonRegistry to your computer or codespace. This article uses [ngrok.com](https://ngrok.com/) to provide a webhook proxy URL and forward webhooks.

If you don't have ngrok installed, and want to use it, go install it [here](https://ngrok.com/download).&#x20;

Our express server will run on port `4040` so we run:

```bash
ngrok http 4040
```

Which sets up a tunnel so the carbonregistry `sandbox` can send the webhook event to our locally running server through a public facing url.

### Creating Webhook

Go to the "General" tab in your app's dashboard and insert the url where the webhook should deliver the payload and save it.

<figure><img src="/files/DxzgEMhurAHJ0IOB5P1q" alt=""><figcaption></figcaption></figure>

Now when an event happens that is connected to the app this endpoint will be called by CarbonRegistry. Let's try it by installing our app on an organization we own. You can go to the path:&#x20;

```bash
https://sandbox-app.carbonregistry.com/apps/{APP_NAME_ID}/installations/new
```

&#x20;to install the app on your organization.

After installing the app on an organization your an admin of we can see we get a webhook POST request from CarbonRegistry in the [ngrok dashboard](http://127.0.0.1:4040).&#x20;

<figure><img src="/files/t5NbFlG7Ujsgyqv0V6zd" alt=""><figcaption></figcaption></figure>

The code for handling this event:

```typescript
import express, { Request, Response } from "express";

const app = express();
app.use(express.json());
app.post("/webhook", (req: Request, res: Response) => {
  res.status(202).send("Accepted");
  const signature = req.header("x-icr-signature-256");
  
  //** Verify signature **//
  
  console.log("SIGNATURE:", signature);
  console.log("EVENT:", req.body.event);
  console.log("Installation:", req.body.installation);
  console.log("Organization:", req.body.installation.organization);
  console.log("Sender:", req.body.sender);
});

app.listen(4040, () => {
  console.log("Server is listening on port 4040");
});

```

Note: webhooks are currently not re-run on failure.&#x20;

Now, you should not blindly accepts calls to this endpoint as coming from CarbonRegistry. Therefore we support signatures using a shared secret. See "[Validate deliveries](/documentation/carbonregistry.com/api/apps/creating-icr-apps/registering-an-icr-app/webhooks/validate-deliveries.md)" for more info.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.carbonregistry.com/documentation/carbonregistry.com/api/apps/creating-icr-apps/registering-an-icr-app/webhooks/handle-deliveries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
