The Upstash Redis SDK works seamlessly with Deno and Deno Deploy without any additional configuration.
Installation
Import directly from a CDN:
esm.sh (Recommended)
Specific Version
deno.land/x
import { Redis } from "https://esm.sh/@upstash/redis@latest";
import { Redis } from "https://esm.sh/@upstash/redis@1.30.2";
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts";
Using @latest ensures you always get the newest version. For production, consider pinning to a specific version.
Quick Start
Import the SDK
import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
import { Redis } from "https://esm.sh/@upstash/redis@latest";
Create a Redis instance
From Environment
With Credentials
const redis = Redis.fromEnv();
This reads from:
UPSTASH_REDIS_REST_URL or KV_REST_API_URL
UPSTASH_REDIS_REST_TOKEN or KV_REST_API_TOKEN
const redis = new Redis({
url: "<UPSTASH_REDIS_REST_URL>",
token: "<UPSTASH_REDIS_REST_TOKEN>",
});
Use Redis in your handler
serve(async (_req: Request) => {
const redis = Redis.fromEnv();
const counter = await redis.incr("deno-deploy-counter");
return new Response(JSON.stringify({ counter }), { status: 200 });
});
Configuration Options
Deno uses the Node.js implementation, supporting all standard configuration options:
const redis = new Redis({
url: "<UPSTASH_REDIS_REST_URL>",
token: "<UPSTASH_REDIS_REST_TOKEN>",
// Enable keep-alive connections
keepAlive: true,
// Abort requests using AbortSignal
signal: abortController.signal,
// Enable latency logging
latencyLogging: true,
// Automatic deserialization of JSON values
automaticDeserialization: true,
// Read-your-writes consistency
readYourWrites: true,
// Enable telemetry (default: true)
enableTelemetry: true,
// Configure retry behavior
retry: {
retries: 3,
backoff: (retryCount) => Math.exp(retryCount) * 50,
},
// Response encoding
responseEncoding: "base64",
// Cache control
cache: "no-store",
});
Using fromEnv() with Options
const redis = Redis.fromEnv({
keepAlive: true,
latencyLogging: true,
automaticDeserialization: true,
});
Complete Example
import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
import { Redis } from "https://esm.sh/@upstash/redis@latest";
serve(async (req: Request) => {
const redis = Redis.fromEnv();
const url = new URL(req.url);
// Increment page view counter
const counter = await redis.incr("deno-deploy-counter");
// Store visit information
await redis.lpush("recent-visits", {
path: url.pathname,
timestamp: Date.now(),
});
// Get recent visits
const recentVisits = await redis.lrange("recent-visits", 0, 9);
return new Response(
JSON.stringify({
counter,
path: url.pathname,
recentVisits,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
});
Environment Variables
Set environment variables for your Deno Deploy project:
Via Deno Deploy Dashboard
- Go to your project settings
- Navigate to Environment Variables
- Add:
UPSTASH_REDIS_REST_URL: Your Redis REST URL
UPSTASH_REDIS_REST_TOKEN: Your Redis REST token
Local Development (.env)
Create a .env file:UPSTASH_REDIS_REST_URL=https://your-redis-url.upstash.io
UPSTASH_REDIS_REST_TOKEN=your-token-here
Load it using:deno run --allow-net --allow-env --env .env your-script.ts
Via CLI
deno run --allow-net --allow-env \
--env=UPSTASH_REDIS_REST_URL=https://... \
--env=UPSTASH_REDIS_REST_TOKEN=... \
your-script.ts
Permissions
Deno requires explicit permissions. Your script needs:
deno run --allow-net --allow-env your-script.ts
--allow-net: Required for HTTP requests to Upstash Redis
--allow-env: Required for reading environment variables (when using fromEnv())
Deployment to Deno Deploy
Install deployctl
deno install --allow-read --allow-write --allow-env --allow-net --allow-run \
--no-check -r -f https://deno.land/x/deploy/deployctl.ts
Deploy your project
deployctl deploy --project=your-project-name your-script.ts
Set environment variables
Use the Deno Deploy dashboard to add your UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.
Type Safety
The SDK includes TypeScript definitions. Deno will automatically use them:
import type { Redis } from "https://esm.sh/@upstash/redis@latest";
const redis: Redis = Redis.fromEnv();
// Type-safe operations
const count: number | null = await redis.get<number>("counter");
const list: string[] = await redis.lrange("mylist", 0, -1);
Testing
Example test file:
import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
import { Redis } from "https://esm.sh/@upstash/redis@latest";
Deno.test("Redis operations", async () => {
const redis = new Redis({
url: Deno.env.get("UPSTASH_REDIS_REST_URL")!,
token: Deno.env.get("UPSTASH_REDIS_REST_TOKEN")!,
});
await redis.set("test-key", "test-value");
const value = await redis.get("test-key");
assertEquals(value, "test-value");
await redis.del("test-key");
});
Run tests:
deno test --allow-net --allow-env
The SDK detects Deno and reports appropriate telemetry. To disable:
const redis = Redis.fromEnv({
enableTelemetry: false,
});
Or set the environment variable:
UPSTASH_DISABLE_TELEMETRY=1