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 app/api/hello.js
with the template below to test the Redis client:
import { Redis } from "@upstash/redis";
import type { NextApiRequest, NextApiResponse } from "next";
const redis = Redis.fromEnv();
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
): Promise<void> {
const count = await redis.incr("counter");
res.status(200).json({ count });
}
Configure the Credentials
We’ll place the credentials in the .env
file as below:
UPSTASH_REDIS_REST_URL=<YOUR_URL>
UPSTASH_REDIS_REST_TOKEN=<YOUR_TOKEN>
Run & Deploy
You can run the app locally: npm run dev
and check
http://localhost:3000/api/hello
Deploy your app with vercel deploy
You can also integrate your Vercel projects with Upstash using Vercel Integration module. Check this article.
Was this page helpful?