Use this file to discover all available pages before exploring further.
This guide covers the fundamental Redis operations using the Upstash Redis SDK. Learn how to work with strings, hashes, lists, sets, sorted sets, and JSON data.
// Set with expiration in secondsawait redis.set('key', 'value', { ex: 60 })// Set with expiration in millisecondsawait redis.set('key', 'value', { px: 60000 })// Alternative: setex commandawait redis.setex('key', 60, 'value')
// Increment a counterconst count = await redis.incr('counter')console.log('Counter:', count)// Increment by a specific amountawait redis.incrby('counter', 5)// Decrementawait redis.decr('counter')await redis.decrby('counter', 3)
// Set hash fieldsawait redis.hset('user:1000', { name: 'John Doe', email: 'john@example.com', age: '30'})// Get a single fieldconst name = await redis.hget('user:1000', 'name')console.log(name) // 'John Doe'// Get all fields and valuesconst user = await redis.hgetall('user:1000')console.log(user) // { name: 'John Doe', email: 'john@example.com', age: '30' }
// Increment a numeric fieldawait redis.hincrby('user:1000', 'age', 1)// Get all field namesconst keys = await redis.hkeys('user:1000')console.log(keys) // ['name', 'email', 'age']// Get all valuesconst values = await redis.hvals('user:1000')// Check if field existsconst exists = await redis.hexists('user:1000', 'name')// Delete fieldsawait redis.hdel('user:1000', 'age')
// Push elements to the left (beginning) of the listawait redis.lpush('mylist', 'first')await redis.lpush('mylist', 'second', 'third')// Push elements to the right (end) of the listawait redis.rpush('mylist', 'last')// Pop from the leftconst first = await redis.lpop('mylist')// Pop from the rightconst last = await redis.rpop('mylist')
// Get elements in a range (0-based index)const elements = await redis.lrange('mylist', 0, -1) // Get all elementsconsole.log(elements)// Get specific rangeconst subset = await redis.lrange('mylist', 0, 2) // First 3 elements// Get list lengthconst length = await redis.llen('mylist')
// Get element at indexconst element = await redis.lindex('mylist', 0)// Set element at indexawait redis.lset('mylist', 0, 'new-value')// Trim list to specified rangeawait redis.ltrim('mylist', 0, 99) // Keep first 100 elements
// Add members to a setawait redis.sadd('tags', 'redis', 'database', 'nosql')// Check if member existsconst isMember = await redis.sismember('tags', 'redis')console.log(isMember) // 1 (true)// Get all membersconst members = await redis.smembers('tags')console.log(members) // ['redis', 'database', 'nosql']// Remove membersawait redis.srem('tags', 'nosql')
// Get range by rank (0-based, lowest to highest score)const topPlayers = await redis.zrange('leaderboard', 0, 2)console.log(topPlayers) // ['player2', 'player3', 'player1']// Get range with scoresconst withScores = await redis.zrange('leaderboard', 0, -1, { withScores: true})console.log(withScores) // ['player2', 85, 'player3', 95, 'player1', 100]// Get range in reverse order (highest to lowest)const reversed = await redis.zrange('leaderboard', 0, -1, { rev: true})
// Append to arrayawait redis.json.arrappend('user:2000', '$.tags', 'early-adopter')// Get array lengthconst length = await redis.json.arrlen('user:2000', '$.tags')// Get array element by indexconst tags = await redis.json.get('user:2000', '$.tags')
// Set expiration in secondsawait redis.expire('mykey', 60)// Set expiration in millisecondsawait redis.pexpire('mykey', 60000)// Set expiration at specific timestampconst timestamp = Math.floor(Date.now() / 1000) + 3600await redis.expireat('mykey', timestamp)// Get TTL (time to live)const ttl = await redis.ttl('mykey')console.log(ttl) // seconds until expiration