Skip to main content

Upstash Redis in Vercel Functions

This is a quickstart for Vercel Serverless Functions. For Vercel Edge

Functions, check this article. :::

Database Setup

Create a Redis database using Upstash Console or Upstash CLI. Select the same region with your Vercel Serverless Function to minimize the latency. Copy the UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN for the next steps.

Project Setup

We will create a Next.js application and deploy to Vercel.

npx create-next-app@latest
cd my-app

Install @upstash/redis:

npm install @upstash/redis

The Code

Update pages/api/hello.js and replace REST_URL and REST_TOKEN below:

import { Redis } from '@upstash/redis'
import type { NextApiRequest, NextApiResponse } from 'next'

const redis = new Redis({
url: 'UPSTASH_REDIS_REST_URL',
token: 'UPSTASH_REDIS_REST_TOKEN',
})

export default async function handler(req: NextApiRequest, res: NextApiResponse): Promise<void> {
const count = await redis.incr("counter")
res.status(200).json({count})
}

Run & Deploy

You can run the app locally: npm run dev and check http://localhost:3000/api/hello

Deploy your app with vercel deploy

Integration module. Check this article. :::