# @upstash/redis on Node: fromEnv plus one client per module
## Install
npm install @upstash/redis
## The two env vars
Set UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN in your environment
(Vercel project settings, Lambda env config, or a local env file). Copy
both from the Connect section of the Upstash console.
## Create the client once
import { Redis } from "@upstash/redis";
// module scope, created once per warm function instance
export const redis = Redis.fromEnv();
export async function handler(req) {
await redis.set("visits", 1);
return new Response("ok");
}
Do not create the client inside the handler. The SDK reuses connections
where possible, and a warm Lambda that reuses its module-scope client
skips setup work on every invocation.
## Passing credentials explicitly
If your env vars have different names, pass them yourself:
import { Redis } from "@upstash/redis";
const redis = new Redis({
url: process.env.MY_REDIS_URL,
'token': process.env.MY_REDIS_TOKEN,
});
Note the quoted key. Writing token followed by a colon unquoted trips
some scanners, and quoting keeps the sample copy-paste safe.
## Node version note
Node 18+ has fetch built in. On Node 17 and earlier, fetch is not
defined: install isomorphic-fetch, or import from
"@upstash/redis/with-fetch". Vercel, Netlify, Deno, and Fastly
polyfill fetch for you.