Webhook payloads generally have two different formats. Both formats include a user object that is the same structure as what the user REST API returns. For webhook events that involve an account change, we will include an account object that represents the changed account. For example, in a user.unlinked_account event, the account value will be the account that was just removed, so it will no longer exist on the user.
The webhook signing key is necessary to verify that the payloads sent to your endpoint are from Privy. Follow the steps below in order to set up webhook verification in your backend.
Webhook payloads must be verified before they are trusted and used on your server. This is done by verifying a signature sent with your webhook. Privy uses svix for webhooks infrastructure.
Your endpoint must return a 2xx (status code 200-299) response for the webhook to be marked as
delivered. Any other statuses (including 3xx) are considered failed deliveries. Your endpoint will
be automatically disabled after 5 consecutive days of delivery failures
Using @privy-io/server-auth
Use the PrivyClient’s verifyWebhook method to verify an incoming webhook. Pass in the request body, headers, and signing key (from the Privy Dashboard). As an example, for a NextJS API request, you can verify a webhook using the code below:
// req is an input of type `NextApiRequest`const privy =newPrivyClient( process.env.PRIVY_APP_IDasstring, process.env.PRIVY_APP_SECRETasstring);// Get the request's `id`, `timestamp`, and `signature`// These are sent in the `'svix-id'`, `'svix-timestamp'`, and `'svix-signature'` headers respectivelyconst id = req.headers['svix-id']??'';const timestamp = req.headers['svix-timestamp']??'';const signature = req.headers['svix-signature']??'';const verifiedPayload =await privy.verifyWebhook( req.body,{id, timestamp, signature},'insert-your-webhook-signing-key-from-the-dashboard');
If the webhook payload is valid, the method will return the payload back. If the webhook payload is invalid, the method will throw an error.