Python on Vercel
Introduction
This quickstart will guide you through setting up QStash to run a daily script to clean up your database. This is useful for testing and development environments where you want to reset the database every day.
Prerequisites
- Create an Upstash account and get your QStash token
Create Python app
First, we’ll create a new directory for our Python app. We’ll call it clean-db-cron
.
The database we’ll be using is Redis, so we’ll need to install the upstash_redis
package.
Cleanup logic
Let’s write the Python code to clean up the database. We’ll use the upstash_redis
package to connect to the database and delete all keys.
Try running the code to see if it works. Your database keys should be deleted!
Make the Python code into a public endpoint
In order to use QStash, we need to make the Python code into a public endpoint. There
are many ways to do this such as using Flask, FastAPI, or Django. In this example, we’ll
use the Python http.server
module to create a simple HTTP server.
For the purpose of this tutorial, I’ll deploy the application to Vercel using the Python Runtime, but feel free to use any other hosting provider.
Once deployed, you can find the public URL in the dashboard.
Have QStash invoke the endpoint
There are two ways we can go about configuring QStash. We can either use the QStash dashboard or the QStash API. In this example, it makes more sense to utilize the dashboard since we only need to set up a singular cronjob.
However, you can imagine a scenario where you have a large number of cronjobs and you’d want to automate the process. In that case, you’d want to use the QStash Python SDK.
To create the schedule, go to the QStash dashboard and enter
the URL of the public endpoint you created. Then, set the type to schedule and change the
Upstash-Cron
header to run daily at a time of your choosing.
Once you start the schedule, QStash will invoke the endpoint at the specified time. You can scroll down and verify the job has been created!
Now, go ahead and try it out for yourself! Try using some of the other features of QStash, such as callbacks and URL Groups.
Was this page helpful?