How to use Sponsorship Policy webhook
Webhook allows you to receive real-time notifications when a sponsorship policy is triggered. You can use webhooks to approve or reject sponsoring userops. Start by going to the sponsorship policies page on the Pimlico dashboard, clicking on the existing policy and clicking on the "Edit button".
Request is sent with POST, where body is a JSON object with the following structure:
const body = {
type: 'sponsorshipPolicy.webhook',
data: {
object: {
userOperation,
entryPoint,
chainId,
sponsorshipPolicyId: sponsorshipPolicy.id
}
}
};
The returned value should be a JSON with the following structure:
{
"sponsor": true // Boolean
}
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.
Installation
pnpm install @pimlico/webhook
Usage
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)
)
// console.log(webhookEvent.data.object.userOperation)
return res.status(200).json({
sponsor: true
})
}