Skip to main content

Usage

Returns all fields and values of the hash stored at key.
await redis.hgetall(key);

Parameters

key
string
required
The key of the hash

Response

object
TData | null
An object containing all field-value pairs in the hash, or null if the hash does not exist or is empty.The return type is a record where keys are field names and values are the stored values. Values are automatically parsed from JSON when possible.

Examples

Get all hash fields

import { Redis } from '@upstash/redis';

const redis = new Redis({
  url: process.env.UPSTASH_REDIS_REST_URL!,
  token: process.env.UPSTASH_REDIS_REST_TOKEN!,
});

// Set hash fields
await redis.hset('user:1000', {
  name: 'Alice',
  email: '[email protected]',
  age: 30,
  city: 'New York'
});

// Get all fields and values
const user = await redis.hgetall('user:1000');
console.log(user);
// {
//   name: 'Alice',
//   email: '[email protected]',
//   age: 30,
//   city: 'New York'
// }

Handle non-existent keys

const result = await redis.hgetall('user:9999');
console.log(result); // null

Working with typed data

interface User {
  name: string;
  email: string;
  age: number;
  city: string;
}

const user = await redis.hgetall<User>('user:1000');

if (user) {
  console.log(`${user.name} is ${user.age} years old`);
}

Iterate over hash fields

const user = await redis.hgetall('user:1000');

if (user) {
  for (const [field, value] of Object.entries(user)) {
    console.log(`${field}: ${value}`);
  }
}

Store and retrieve complex data

// Store session data
await redis.hset('session:abc123', {
  userId: '1000',
  createdAt: Date.now().toString(),
  ipAddress: '192.168.1.1',
  userAgent: 'Mozilla/5.0...'
});

// Retrieve all session data
const session = await redis.hgetall('session:abc123');
console.log(session);

Notes

  • The command automatically parses numeric values that are safe integers
  • JSON values are parsed when possible
  • For very large hashes, consider using HSCAN to iterate incrementally

See Also

  • HGET - Get a single hash field value
  • HKEYS - Get all field names in a hash
  • HVALS - Get all values in a hash
  • HMGET - Get multiple hash field values

Build docs developers (and LLMs) love