> ## Documentation Index
> Fetch the complete documentation index at: https://docs.plerion.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Webhook

> Integrate Plerion with custom webhooks to deliver alerts directly to your external web servers for flexible automation and workflows.

With the Webhook integration, you can automatically send Plerion alerts to an external web server. A request is triggered whenever an alert is created, updated, or resolved, allowing you to connect Plerion to custom workflows and automation.

<Warning>
  **Note:** This is a one-way outbound integration. Alerts flow from Plerion to your webhook endpoint, but responses from your server will not update Plerion.
</Warning>

## Steps to integrate Webhook with Plerion

<Steps>
  <Step title="On the Plerion dashboard, go to Settings > Integrations">
    <Frame>
      <img src="https://mintcdn.com/pleriondocs/KcogPGsTmyqY5j98/images/integrations/webhook/integration-menu.png?fit=max&auto=format&n=KcogPGsTmyqY5j98&q=85&s=1974e53e6b1b18607f35b5f3a3287465" alt="Plerion dashboard with Settings expanded and Integrations selected" width="404" height="602" data-path="images/integrations/webhook/integration-menu.png" />
    </Frame>
  </Step>

  <Step title="Find Webhook and click the + button">
    <Frame>
      <img src="https://mintcdn.com/pleriondocs/KcogPGsTmyqY5j98/images/integrations/webhook/add-webhook.png?fit=max&auto=format&n=KcogPGsTmyqY5j98&q=85&s=96855870ccbc702ef154cf11ba413459" alt="Integrations page with Webhook option and plus button" width="736" height="840" data-path="images/integrations/webhook/add-webhook.png" />
    </Frame>
  </Step>

  <Step title="On the Connect Webhook page, enter your integration details">
    * **Integration name:** Enter a descriptive name.
    * **Webhook URL:** Provide the endpoint that will receive alerts.
    * **Secret (optional):** Configure a secret token for request signing.
    * **Additional headers (optional):** Add any extra headers to send with the request.

    After entering the details, click `Send test message` to confirm your server is receiving requests correctly.

    <Frame>
      <img src="https://mintcdn.com/pleriondocs/KcogPGsTmyqY5j98/images/integrations/webhook/test-webhook.png?fit=max&auto=format&n=KcogPGsTmyqY5j98&q=85&s=0a7b529f2210c4a73960556a72e8c0ba" alt="Connect Webhook page showing fields for Integration name, URL, Secret, and Headers" width="1816" height="856" data-path="images/integrations/webhook/test-webhook.png" />
    </Frame>
  </Step>

  <Step title="Finalize the setup">
    Click `Add` to complete the Webhook integration.
  </Step>
</Steps>

## Secret token and Signature

If you configure a **Secret**, Plerion will generate a hash signature for each payload and include it in the `plerion-signature` request header.

* The signature uses an HMAC SHA-256 hex digest.
* The header value is formatted as `sha256=<signature>`.
* The key is your secret token, and the payload body is the signed content.

This allows your server to verify that requests originate from Plerion.

### Validating payloads using secret token

To validate requests, compute the HMAC of the received payload using your stored secret and compare it to the `plerion-signature` header.\
We recommend setting the secret as an environment variable, not hardcoding it in your app.

<Tabs>
  <Tab title="TypeScript">
    ```javascript TypeScript theme={"system"}
       import * as crypto from "crypto";

       const WEBHOOK_SECRET: string = process.env.WEBHOOK_SECRET;

       const verify_signature = (req: Request) => {
       const signature = crypto
          .createHmac("sha256", WEBHOOK_SECRET)
          .update(JSON.stringify(req.body))
          .digest("hex");
       return `sha256=${signature}` === req.headers.get("plerion-signature");
       };

       const handleWebhook = (req: Request, res: Response) => {
       if (!verify_signature(req)) {
          res.status(401).send("Unauthorized");
          return;
       }
       // The rest of your logic here
       };

    ```
  </Tab>

  <Tab title="Ruby">
    ```rb Ruby theme={"system"}
       #Define a verify_signature function
       def verify_signature(payload_body)
       signature = 'sha256=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), ENV['SECRET_TOKEN'], payload_body)
       return halt 500, "Signatures didn't match!" unless Rack::Utils.secure_compare(signature, request.env['HTTP_PLERION_SIGNATURE'])
       end

       #Then you can call it when you receive a webhook payload
       post '/payload' do
       request.body.rewind
       payload_body = request.body.read
       verify_signature(payload_body)
       push = JSON.parse(payload_body)
       "I got some JSON: #{push.inspect}"
       end
    ```
  </Tab>
</Tabs>

### Payload Format

<Tabs>
  <Tab title="Alert Payload">
    When an alert is created, updated, or resolved, Plerion sends an HTTP POST request to your webhook with the following structure:

    ```json JSON theme={"system"}
         {
            "data": {
               "tenant": "<tenant_name>",
               "integration": "<integration_name>",
               "integrationUrl": "<integration_url>",
               "workflow": "<workflow_name>",
               "workflowUrl": "<workflow_url>",
               "alerts": [
                  {
                     "id": "<alert-id>",
                     "title": "<alert_title>",
                     "description": {
                        "resource": "<resource_name>",
                        "alertUrl": "<plerion_alert_url>",
                        "resourceType": "<resource_type>",
                        "alertSummary": "<array_alert_summary>",
                        "status": "OPEN or RESOLVED"
                     },
                     "operation": "CREATED, UPDATED or RESOLVED"
                  }
               ]
            }
         }
    ```

    * `tenant`: Name of the Tenant
    * `integration`: Name of the Integration
    * `integrationUrl`: URL for the Integration.
    * `workflow`: Name of the Workflow
    * `workflowUrl`: URL for the Workflow.
    * `alerts`: Array of the alerts that were created/updated/resolved. Properties inside `alerts` array are described below:
      * `id`: Contains the unique identifier of the Alert
      * `title`: Contains the title of the Alert
      * `resource`: Contains name of the resource or N/A if no asset is linked to the alert. \[Unavailable if status/operation is `RESOLVED`]
      * `alertUrl`: URL to the alert on Plerion
      * `resourceType`: Contains type of the resource for `OPEN` status. \[Unavailable if status/operation is `RESOLVED`]
      * `alertSummary`: An array containing summary of the generated Alert. \[Unavailable if status/operation is `RESOLVED`]
      * `status`: Status of the alert. Can be `OPEN` or `RESOLVED`
      * `operation`: Signifies the operation of the alert. Can be `CREATED` or `UPDATED` or `RESOLVED`.
  </Tab>

  <Tab title="Testing Integration Payload">
    When you click on **Send a Test Message**, Plerion sends a request with this structure:

    ```json JSON theme={"system"}
    "data": {
       "title": "Plerion Test Message",
       "message": "Tested by: <email_of_the_user>",
       "integration": "<name_of_integration>",
    }
    ```
  </Tab>
</Tabs>
