Veifying webhooks with the Svix libraries
First install the libraries if you haven't already:
npm install svix
// Or
yarn add svixpip install svixhttp = "1.0.0"
svix = "1.20.0"go get github.com/svix/svix-webhooks/goGradle: Add this dependency to your project's build file:
implementation "com.svix:svix:0.x.y"Maven: Add this dependency to your project's POM:
<dependency>
<groupId>com.svix</groupId>
<artifactId>svix</artifactId>
<version>0.x.y</version>
</dependency>Gradle: Add this dependency to your project's build file:
implementation "com.svix.kotlin:svix-kotlin:0.x.y"Maven: Add this dependency to your project's POM:
<dependency>
<groupId>com.svix.kotlin</groupId>
<artifactId>svix-kotlin</artifactId>
<version>0.x.y</version>
</dependency>Then verify webhooks using the code below. The payload is the raw (string) body of the request, and the headers are the headers passed in the request.
USE THE RAW REQUEST BODY
You need to use the raw request body when verifying webhooks, as the cryptographic signature is sensitive to even the slightest changes. You should watch out for frameworks that parse the request as JSON and then stringify it because this too will break the signature verification.
Framework specific examples
Here are examples on how to adjust the above examples to your favourite framework.
Python (Django)
from django.http import HttpResponse
from svix.webhooks import Webhook, WebhookVerificationError
secret = "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw"
@csrf_exempt
def webhook_handler(request):
headers = request.headers
payload = request.body
try:
wh = Webhook(secret)
msg = wh.verify(payload, headers)
except WebhookVerificationError as e:
return HttpResponse(status=400)
# Do something with the message...
return HttpResponse(status=204)Python (Flask)
Python (FastAPI)
Node.js (Next.js)
The svix-example repo contains an example of how to verify and use webhooks in a Next.js application.
Node.js (Next.js 13 App Router)
Node.js (Netlify Functions)
Node.js (Express)
Note: When integrating this example into a larger codebase, you will have to make sure not to apply the express.json() middleware to the webhook route, because the payload has to be passed to wh.verify without any prior parsing.
Node.js (NestJS)
Initialize the application with the rawBody flag set to true. See the (NestJS docs)[https://docs.nestjs.com/faq/raw-body#raw-body] for details.
Node.js (Nuxt)
Go (Standard lib)
Go (Gin)
Rust (axum)
Add the webhook_in route below to an axum router.
Ruby (Ruby on Rails)
Once you've set up your project add a route to your config/routes.rb file at the top of the Rails.application.routes.draw block:
The route above declares that POST /webhook requests are mapped to the index action of WebhookController.
To create WebhookController and its index action, we'll run the controller generator (with the --skip-routes option because we already have an appropriate route):
Rails will create several files for you:
Now we can add our verification logic to the newly created app/controllers/webhook_controller.rb file:
PHP (Laravel)
In your routes/api.php file add the following after the last use directive:
Last updated