> ## Documentation index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Ask it with GET https://upstash.com/docs/ask?q=<keywords or question>. Short keywords rank best.
> Use these to discover all available pages before exploring further.
> Feature requests: https://upstash.com/docs/common/help/feature-requests.md lists what users asked for and how to vote for a request or add one.
> Page rating: `GET https://upstash.com/docs/feedback?path=/docs/redis/quickstarts/nextjs-app-router&helpful={true|false}&comment={comment}` records whether this page helped. The `comment` (URL-encoded, at most 500 characters) says what helped, or what was missing or wrong; a `helpful=false` rating needs it. It is anonymous and needs no key. Comments hold no code, secrets or personal data.

# Upstash Redis with Next.js (App Router)

> Use Upstash Redis in a Next.js App Router project with @upstash/redis: add your credentials, create a client, and build a simple counter.

---

## Quickstart: Upstash Redis in Next.js 16

<Frame>
  <iframe
    src="https://player.mux.com/gPYmP1M00800UgLr4fa9O8CeN6M36E2iY6Bww8Ir5Qn4U?thumbnail-time=5&metadata-video-title=Upstash+Redis+x+Nextjs+Quickstart&video-title=Upstash+Redis+x+Nextjs+Quickstart"
    allow="accelerometer; gyroscope; autoplay; encrypted-media; fullscreen;"
    allowfullscreen
    className="w-full aspect-video"
  ></iframe>
</Frame>

---

## 1. Install package

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

```bash
npm install @upstash/redis
```

---

## 2. Connect to Redis

1. Grab your Redis credentials from the Upstash dashboard

   <Frame>
     <img src="/img/next-quickstart/credentials.png" alt="Redis credentials for a Next.js application" />
   </Frame>

2. Paste them into your Next environment variables:

   ```bash title=".env.local"
   UPSTASH_REDIS_REST_URL=https://holy-kite-17499.upstash.io
   UPSTASH_REDIS_REST_TOKEN=AURbAAIncDEyYjM4M...
   ```

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

   ```typescript title="lib/redis.ts"
   import { Redis } from "@upstash/redis"

   // 👇 we can now import our redis client anywhere we need it
   export const redis = Redis.fromEnv()
   ```

---

## 3. Using our Redis Client

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

```typescript title="app/page.tsx"
import { redis } from "@/lib/redis"

// 👇 server component
const Page = async () => {
  const count = await redis.get<number>("count")
  return <p>count: {count}</p>
}

export default Page
```

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:

```typescript title="app/api/counter/route.ts"
import { redis } from "@/lib/redis"

export const POST = async () => {
  await redis.incr("count")

  return new Response("OK")
}
```

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

<Frame>
  <img src="/img/next-quickstart/curl.png" alt="Redis REST API request with curl" />
</Frame>

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

<Card title="GitHub Repository" icon="github" href="https://github.com/upstash/redis-js/tree/main/examples/nextjs-app-router" horizontal>
  You can find the project source code on GitHub.
</Card>

<Info>
  If you're already on Vercel, you can create Upstash projects directly through Vercel: [Read more](../howto/vercelintegration).
</Info>
