# How to use Sponsorship Policy webhooks

Webhooks allow you to receive real-time notifications when sponsorship-related events occur. You can use webhooks to approve or reject sponsorship requests and receive notifications about finalized sponsorships. Start by going to the [sponsorship policies page](https://dashboard.pimlico.io/sponsorship-policies) on the Pimlico dashboard, clicking on the existing policy and clicking on the "Edit button".

## Webhook Types

### UserOperation Sponsorship

These webhooks are triggered when using the [pm\_sponsorUserOperation](/references/paymaster/erc20-paymaster/endpoints/pm_sponsorUserOperation) endpoint.

#### Request for Sponsorship

```typescript
const body = {
    type: "user_operation.sponsorship.requested",
    data: {
        object: {
            userOperation,
            entryPoint,
            chainId,
            sponsorshipPolicyId,
            apiKey
        }
    }
}
```

The webhook must return a response with the following structure:

```json
{
    "sponsor": true // Boolean - whether to approve the sponsorship
}
```

#### Sponsorship Finalized

Sent when a UserOperation sponsorship is approved and finalized:

```typescript
const body = {
    type: "user_operation.sponsorship.finalized",
    data: {
        object: {
            userOperation,
            entryPoint,
            chainId,
            sponsorshipPolicyId,
            apiKey
        }
    }
}
```

## How to verify the webhook

To verify the webhook, you can use the `@pimlico/webhook` package. You will need to provide the webhook secret, which you can find in the sponsorship policy [settings](https://dashboard.pimlico.io/sponsorship-policies).

### Installation

```bash
pnpm install @pimlico/webhook
```

### Usage

```typescript
import { pimlicoWebhookVerifier } from "@pimlico/webhook"
import type { VercelRequest, VercelResponse } from "@vercel/node"

const webhookSecret = process.env.PIMLICO_WEBHOOK_SECRET as string

const verifyWebhook = pimlicoWebhookVerifier(webhookSecret)

export default async function handler(req: VercelRequest, res: VercelResponse) {
    const webhookEvent = verifyWebhook(
        req.headers as Record<string, string>,
        JSON.stringify(req.body)
    )

    // Handle different webhook types
    switch(webhookEvent.type) {
        case "sponsorshipPolicy.webhook":
        case "user_operation.sponsorship.requested":
        case "user_operation.sponsorship.finalized":
        default:
            return res.status(400).json({ error: "Unknown webhook type" })
    }
}
```
