Create a Redis instance, for example in lib/redis.ts
lib/redis.ts
Copy
Ask AI
import { Redis } from "@upstash/redis"// 👇 we can now import our redis client anywhere we need itexport const redis = new Redis({ url: process.env.UPSTASH_REDIS_REST_URL, token: process.env.UPSTASH_REDIS_REST_TOKEN,})
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
Copy
Ask AI
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:
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.