The Redis class is the main entry point for interacting with Upstash Redis. It provides methods for all Redis commands and supports both direct instantiation and environment-based configuration.
Constructor
With Configuration Object
Create a new Redis client by providing connection credentials.
config
RedisConfigNodejs
required
Configuration object for the Redis client Automatically deserialize returned data using JSON.parse
Enable telemetry data collection. Can be disabled by setting UPSTASH_DISABLE_TELEMETRY environment variable
Enable automatic pipelining of commands for better performance
Enable latency logging for commands
When enabled, subsequent commands are guaranteed to observe effects of all earlier writes submitted by the same client
retry
RetryConfig
default: "5 retries with exponential backoff"
Configure retry behavior for network errors Number of retries to attempt before giving up
backoff
(retryCount: number) => number
Backoff function that returns milliseconds to wait before retrying. Default: Math.exp(retryCount) * 50
responseEncoding
false | 'base64'
default: "'base64'"
Encode responses as base64 before sending. Disable if you’re sure your data is valid UTF-8
cache
'default' | 'force-cache' | 'no-cache' | 'no-store' | 'only-if-cached' | 'reload'
default: "'no-store'"
Configure cache behavior for requests
HTTP(S) agent for connection pooling (Node.js only). Not supported in edge runtimes Enable HTTP keep-alive for connection reuse
With Custom Requester
Create a Redis client with a custom HTTP requester implementation.
Custom requester implementation request
<TResult>(req: UpstashRequest) => Promise<UpstashResponse<TResult>>
required
Function that handles HTTP requests to Upstash Redis REST API
Enable read-your-writes consistency
Sync token for maintaining consistency
Static Methods
fromEnv()
Create a Redis instance from environment variables.
This method automatically loads connection credentials from:
UPSTASH_REDIS_REST_URL or KV_REST_API_URL (fallback for Vercel KV)
UPSTASH_REDIS_REST_TOKEN or KV_REST_API_TOKEN (fallback for Vercel KV)
config
Omit<RedisConfigNodejs, 'url' | 'token'>
Optional configuration (all options except url and token)
A configured Redis instance
Properties
readYourWritesSyncToken
Get or set the sync token for read-your-writes consistency.
Instance Methods
pipeline()
Create a new pipeline to batch multiple commands.
A new Pipeline instance for batching commands
See Pipeline for full documentation.
multi()
Create a transaction (MULTI/EXEC) to execute commands atomically.
A new Pipeline instance configured for atomic execution
createScript()
Create a Lua script that can be executed efficiently using EVALSHA.
The Lua script to execute
Script options If true, returns a ScriptRO instance that uses EVALSHA_RO/EVAL_RO
script
Script<TResult> | ScriptRO<TResult>
A Script or ScriptRO instance depending on the readonly option
See Script for full documentation.
use()
Wrap middleware around the HTTP client for custom request/response handling.
middleware
(request: UpstashRequest, next: (req: UpstashRequest) => Promise<UpstashResponse<TResult>>) => Promise<UpstashResponse<TResult>>
required
Middleware function to wrap around requests
Command Methods
The Redis class provides methods for all Redis commands. Here are some commonly used examples:
String Commands
Hash Commands
List Commands
Set Commands
Sorted Set Commands
JSON Commands
Key Management
Complete Example
TypeScript Support
The Redis client is fully typed with TypeScript generics: