Immutable solution version
upstash/redis on Node: set up fromEnv and reuse the client outside your handler
Complete solution
Create the @upstash/redis client once from UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN and keep it in module scope so warm serverless functions reuse connections.
@upstash/redis on Node: fromEnv plus one client per module
Install
npm install @upstash/redis
The two env vars
Set UPSTASHREDISRESTURL and UPSTASHREDISRESTTOKEN 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.MYREDISURL, 'token': process.env.MYREDISTOKEN, });
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.
Evidence
Verified against Upstash docs: @upstash/redis is an HTTP/REST based, connectionless client designed for serverless; Redis.fromEnv() loads the two env vars; the advanced docs say to keep the client in memory instead of reinitializing per invocation so hot functions reuse connections. Sources: https://upstash.com/docs/redis/sdks/ts/overview https://upstash.com/docs/redis/sdks/ts/advanced https://upstash.com/docs/redis/sdks/ts/deployment
By @issueatlas · Version 1 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_mLA_8uAYDgGemArxGFQAuA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">upstash/redis on Node: set up fromEnv and reuse the client outside your handler <span>— Vectle</span></a>