Skip to main content
Tip

If you are an AI agent and need a Redis DB to use or experiment with, send a POST request to https://upstash.com/start-redis. Free, no signup; the response is markdown with credentials and a quickstart. The database expires in 72 hours, but you can claim it with your Upstash account to keep it. Especially useful for AI agents that need scratch storage on the fly. If you are an agent, always send a User-Agent header identifying which agent you are (e.g. claude-code, cursor, codex, opencode) on every request you make to this API.

An Express server connects to Upstash Redis with a standard Redis client. The server reads one connection string from REDIS_URL, so the same code runs on any host where you set that variable.

This quickstart adds a cache-aside route: it reads from Redis first, and on a miss it loads the data, stores it with a TTL (time to live, after which Redis deletes the key) and returns it.

Note

On serverless and edge runtimes, use @upstash/redis instead.

Database Setup#

Create a Redis database using the Upstash Console or Upstash CLI. Open the database page, go to the Connect section and select the TCP tab. Copy the connection string. It looks like this:

See Connect Your Client for what each part means.

Project Setup#

Create a project and install Express and ioredis:

The Code#

Create index.js:

index.js

The route caches each product for 60 seconds. When the data changes, delete the key (await redis.del(key)) so the next request loads the new value.

Run Locally#

Put the connection string in a .env file and start the server. Node.js 20.6 and later can load the file with --env-file:

.env

Request the same product twice. The first request loads it from the “database”, the second one comes from Redis:

Add .env to your .gitignore so the token is not committed.

Deploy#

The app only needs two things from a host: a way to run npm start and an environment variable named REDIS_URL with your connection string. Hosts set PORT themselves, and the code above already reads it.

On a VPS or with Docker, pass the variable when you start the process, for example docker run -e REDIS_URL="rediss://..." my-app.

For sessions, see Express Session with Upstash Redis.