---
name: redis-sveltekit
description: "Provisions a Redis database (Upstash-compatible, REST and TCP) for a SvelteKit app with `npx extraorbital add redis` (no signup, no card) and wires it into SvelteKit with `@upstash/redis`. Use when a SvelteKit project needs Redis, a cache, rate limiting, sessions, a job queue, Upstash, Valkey or REDIS_URL, or when UPSTASH_REDIS_REST_URL is missing or unset in .env."
---

# Redis in SvelteKit

<!-- Generated from ExtraOrbital's service registry by `pnpm skills:build` in platform/app. Edit the generator, not this file. -->

ExtraOrbital provisions a Redis database (Upstash-compatible, REST and TCP) in one command and writes the credentials into the project's env file. For anything this recipe does not cover, use the `extraorbital` skill or https://extraorbital.dev/llms.txt.

## 1. Provision

```bash
npx extraorbital add redis
```

Run it from the project root. It is idempotent (a second run returns the same resource) and writes to `.env.local` if the project has one, otherwise `.env`, keeping that file out of git. Add `--json` to parse the result; credential values are redacted there, and you should never print them either.

| Variable | |
| --- | --- |
| `UPSTASH_REDIS_REST_URL` | REST endpoint, for serverless and edge runtimes |
| `UPSTASH_REDIS_REST_TOKEN` | Its token |
| `REDIS_URL` | `rediss://` URL for any Redis-protocol client |

Free on the Prototype plan: 256 MB, 500K commands/mo.

## 2. Install the client

```bash
npm install @upstash/redis
```

## 3. Connect

SvelteKit reads `.env` and `.env.local` into `$env/dynamic/private` in dev; in production the host's environment fills it.

**`src/lib/server/redis.ts`**

```ts
import { Redis } from "@upstash/redis";
import { env } from "$env/dynamic/private";

// Over REST: every command is an HTTP request, so there is no connection to manage.
export const redis = new Redis({
  url: env.UPSTASH_REDIS_REST_URL!,
  token: env.UPSTASH_REDIS_REST_TOKEN!,
});
```

## 4. Use it

**`src/routes/api/redis/+server.ts`**

```ts
import { json } from "@sveltejs/kit";
import { redis } from "$lib/server/redis";

export async function GET() {
  const hits = await redis.incr("hits");
  return json({ hits });
}
```

## 5. Verify

```bash
npx extraorbital list        # the resource is listed as active
npm run dev
curl http://localhost:5173/api/redis
```

A JSON answer means the credentials, the client and the route all work. Delete the route afterwards if the app does not need it.

## Pitfalls

- A long-lived worker (BullMQ, `ioredis`, `node-redis`) connects with `REDIS_URL` instead; it is the same database. BullMQ needs `maxRetriesPerRequest: null` on its connection.
- Set an expiry on cache keys (`ex`/`EX`): the Prototype allowance is 256 MB and 500K commands a month, across the account.
- Read credentials through `$env/dynamic/private`, not `process.env`: Vite does not put `.env` into `process.env` in dev, and `$lib/server/` keeps the module out of the browser bundle.
- `$env/dynamic/private` is empty while a page is prerendered: do not call the client from a prerendered route.
- Only `PUBLIC_*` variables reach the browser. Pass anything public a page needs from a `+page.server.ts` load function instead.
- Exit code 3 means the account is past its free allowance: show the human the link the command printed and wait. Do not retry or work around it.

Docs: https://extraorbital.dev/docs/resources/redis.md
