This is a step by step guide on how to receive webhooks from QStash in your Golang application running on fly.io.
0. Prerequisites#
- flyctl - The fly.io CLI
1. Create a new project#
Let's create a new folder called flyio-go and initialize a new project.
2. Creating the main function#
In this example we will show how to receive a webhook from QStash and verify the signature using the popular golang-jwt/jwt library.
First, let's import everything we need:
Next we create main.go. Ignore the verify function for now. We will add that
next. In the handler we will prepare all necessary variables that we need for
verification. This includes the signature and the signing keys. Then we try to
verify the request using the current signing key and if that fails we will try
the next one. If the signature could be verified, we can start processing the
request.
The verify function will handle verification of the JWT,
that includes claims about the request. See
here.
You can find the complete file here.
That's it, now we can deploy our API and test it.
3. Create app on fly.io#
Login with flyctl and
then flyctl launch the new app. This will create the necessary fly.toml for
us. It will ask you a bunch of questions. I chose all defaults here except for
the last question. We do not want to deploy just yet.
4. Set Environment Variables#
Get your current and next signing key from the Upstash Console
Then set them using flyctl secrets set ...
5. Deploy the app#
Fly.io made this step really simple. Just flyctl deploy and enjoy.
6. Publish a message#
Now you can publish a message to QStash. Note the destination url is basically your app name, if you are not sure what it is, you can go to fly.io/dashboard and find out. In my case the app is named "winter-cherry-9545" and the public url is "https://winter-cherry-9545.fly.dev".
Next Steps#
That's it, you have successfully created a Go API hosted on fly.io, that receives and verifies incoming webhooks from qstash.
Learn more about publishing a message to qstash here