What will be build?
Let’s assume there is a 3rd party API that provides some data. One approach would be to just query the API whenever you or your users need it, however that might not work well if the API is slow, unavailable or rate limited. A better approach would be to continuously fetch fresh data from the API and store it in your database. Traditionally this would require a long running process, that would continuously call the API. With QStash you can do this inside your Next.js app and you don’t need to worry about maintaining anything. For the purpose of this recipe we will build a simple app, that scrapes the current Bitcoin price from a public API, stores it in redis and then displays a chart in the browser.Setup
If you don’t have one already, create a new Next.js project withnpx create-next-app@latest --ts
.
Then install the required packages
@upstash/redis
with any kind of database client you want.
Scraping the API
Create a new serverless function in/pages/api/cron.ts
Deploy to Vercel
That’s all we need to fetch fresh data. Let’s deploy our app to Vercel. You can either push your code to a git repository and deploy it to Vercel, or you can deploy it directly from your local machine using the vercel cli. For a more indepth tutorial on how to deploy to Vercel, check out this quickstart. After you have deployed your app, it is time to add your secrets to your environment variables.Secrets
Head over to QStash and copy theQSTASH_CURRENT_SIGNING_KEY
and QSTASH_NEXT_SIGNING_KEY
to vercel’s
environment variables. 
UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
to vercel.
In the near future we will update our
Vercel integration to do this for
you.
Redeploy
To use the environment variables, you need to redeploy your app. Either withnpx vercel --prod
or in the UI.
Create cron trigger in QStash
The last part is to add the trigger in QStash. Go to QStash and create a new schedule.