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

# Webhooks

> Learn how to use webhooks to get notified when Tensorlake Jobs finishes.

You can use webhooks to get notified about the status of Document Ingestion Jobs.

Webhooks are configured on a per-project basis. The project associated with the API key that is being used
to configure the webhook is the one for which the webhook will be triggered.

*Our webhooks events are managed by Svix, please note none of your data is sent to Svix, only event statuses.*

## Create and Configure Your Webhook

To use Webhooks with Tensorlake, make sure you have created a project on [Tensorlake Cloud](https://cloud.tensorlake.ai).

<Note>It is important to disable CSRF protection for the endpoint if the framework you use enables them by default.</Note>

<Steps>
  <Step title="Create a webhook">
    Go to the project that you want to create the webhook for, and click into the Webhooks tab.

    <img src="https://mintcdn.com/tensorlake-35e9e726/fVE8-oNRlpqs-U2A/images/webhooks/create_webhook.png?fit=max&auto=format&n=fVE8-oNRlpqs-U2A&q=85&s=59e0050ebc52c03a6818c2eb61af1e5d" alt="Create and list webhooks" width="2664" height="1790" data-path="images/webhooks/create_webhook.png" />

    <Warning>Only admins for a project can create webhooks. However, members of a project can view webhooks.</Warning>
  </Step>

  <Step title="Set up your webhook">
    Give the webhook a name, and provide a URL that you control.

    <img src="https://mintcdn.com/tensorlake-35e9e726/fVE8-oNRlpqs-U2A/images/webhooks/webhook_settings_1.png?fit=max&auto=format&n=fVE8-oNRlpqs-U2A&q=85&s=1db3a5db8c6c264a00e5ad26d7cbfd3e" alt="Webhook creation" width="1724" height="1713" data-path="images/webhooks/webhook_settings_1.png" />
  </Step>

  <Step title="Configure access for your webhook">
    Configure your webhook by selecting the event types that you want to listen to.

    <img src="https://mintcdn.com/tensorlake-35e9e726/fVE8-oNRlpqs-U2A/images/webhooks/webhook_settings_2.png?fit=max&auto=format&n=fVE8-oNRlpqs-U2A&q=85&s=7704465bac9495902eccdceb2ce1f44d" alt="Webhook creation" width="1707" height="1736" data-path="images/webhooks/webhook_settings_2.png" />

    The three event types are:

    1. `tensorlake.document_ingestion.job.created`: Triggered when Tensorlake receives the request to parse a document and kicks off the parsing.
    2. `tensorlake.document_ingestion.job.failed`: Triggered when a parsing job fails.
    3. `tensorlake.document_ingestion.job.completed`: Triggered when a parsing job succeeds.
  </Step>
</Steps>

## Understand the Webhook Payload

The payload of the webhook will depend on the event type received. The payload will be a JSON object with the following structure:

<Tabs>
  <Tab title="Job Created">
    ```json theme={null}
    {
        "job_id": "parse_XXX",
        "status": "pending",
        "created_at": "2023-10-01T12:00:00Z",
    }
    ```
  </Tab>

  <Tab title="Job Failed">
    ```json theme={null}
    {
        "job_id": "parse_XXX",
        "status": "failure",
        "error": "Error message describing the failure",
        "created_at": "2023-10-01T12:00:00Z",
        "finished_at": "2023-10-01T12:05:00Z"
    }
    ```
  </Tab>

  <Tab title="Job Completed">
    ```json theme={null}
    {
        "job_id": "parse_XXX",
        "status": "successful",
        "created_at": "2023-10-01T12:00:00Z",
        "finished_at": "2023-10-01T12:05:00Z",
        "usage": {
          "pages_parsed": 10,
          "extraction_input_tokens_used": 0,
          "extraction_output_tokens_used": 0,
          "ocr_input_tokens_used": 0,
          "ocr_output_tokens_used": 0,
          "signature_detected_pages": 0,
          "strikethrough_detected_pages": 0,
          "summarization_input_tokens_used": 0,
          "summarization_output_tokens_used": 0
        }
    }
    ```
  </Tab>
</Tabs>

### Workflow Payloads

When all functions of an application finish running for a given input:

```json theme={null}
{
    "workflow_name": "workflow_XXXX",
    "invocation_id": "invocation_XXXX",
    "fn_status": {
        "fn_A": "success",
        "fn_B": "failure"
    }
}
```

Possible statuses for each function:

* `success` — The function finished running successfully.
* `failure` — The function failed to finish running.

## Test Your Webhooks in Tensorlake Cloud

We have a UI to test webhooks. After creating a webhook, select on the event type you want to test.

<img src="https://mintcdn.com/tensorlake-35e9e726/fVE8-oNRlpqs-U2A/images/webhooks/test_webhook.png?fit=max&auto=format&n=fVE8-oNRlpqs-U2A&q=85&s=2e164b112e957fe6db4a02b57f327b9d" alt="Webhook creation" width="1252" height="1254" data-path="images/webhooks/test_webhook.png" />

## Secure your Endpoints with Signature Verification

<Warning>
  Without signature verification, anyone could send forged requests to your
  endpoint. Each webhook endpoint has a unique secret to verify the authenticity
  of incoming requests.
</Warning>

To get your webhook secret:

1. Navigate to your project's Webhooks tab in [Tensorlake Cloud](https://cloud.tensorlake.ai)
2. Click on your webhook to view its details
3. Copy the webhook secret displayed in the interface

<img src="https://mintcdn.com/tensorlake-35e9e726/fVE8-oNRlpqs-U2A/images/webhooks/webhook_secret.png?fit=max&auto=format&n=fVE8-oNRlpqs-U2A&q=85&s=1f8156a4f6c5451d6d1232d5dfdaaedf" alt="Webhook Secret Location" width="1252" height="1254" data-path="images/webhooks/webhook_secret.png" />

You can use the [**Svix libraries**](https://docs.svix.com/receiving/verifying-payloads/how) to handle secret verification automatically in your application code.

Alternatively, you can [**manually verify the signature**](https://docs.svix.com/receiving/verifying-payloads/how-manual) using the secret. The verification process involves:

* Extracting the signature from the `svix-signature` header
* Computing the expected signature using your webhook secret
* Comparing the computed signature with the received signature

For more details and other verification methods, refer to the following resources:

* [Receiving Webhooks with Bridge](https://docs.svix.com/receiving/verifying-payloads/receiving-with-bridge)
* [Additional Authentication](https://docs.svix.com/receiving/additional-authentication)
* [Static Source IP Addresses](https://docs.svix.com/receiving/source-ips)
