Skip to main content

Quickstart: Upstash Redis in Next.js 16#


1. Install package#

In your Next.js app, install our @upstash/redis package:


2. Connect to Redis#

  1. Grab your Redis credentials from the Upstash dashboard

    Redis credentials for a Next.js application
  2. Paste them into your Next environment variables:

    .env.local
  3. Create a Redis instance, for example in lib/redis.ts

    lib/redis.ts

3. Using our Redis Client#

We can now connect to Upstash Redis from any server component or API route. For example:

app/page.tsx

Because this count doesn’t exist yet, let’s create a Next API route to populate it.


4. Storing data in Redis#

Let’s create a super simple API that, every time when called, increments an integer value we call count. This is the same value we display in our page above:

app/api/counter/route.ts

Perfect! Every time we now call this API, we increment the count in our Redis database:

Redis REST API request with curl

The server component fetches the most recent count at render-time and displays the up-to-date value automatically. For a video demo, check the video at the top of this article.


Examples#

GitHub Repository

You can find the project source code on GitHub.

Info

If you’re already on Vercel, you can create Upstash projects directly through Vercel: Read more.