Skip to main content

Usage

Returns the value associated with field in the hash stored at key.
await redis.hget(key, field);

Parameters

key
string
required
The key of the hash
field
string
required
The field in the hash to get

Response

value
TData | null
The value associated with the field, or null when the field is not present in the hash or the key does not exist.

Examples

Get a hash field value

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

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

// Set a hash field
await redis.hset('user:1000', { name: 'Alice', email: '[email protected]' });

// Get a specific field
const name = await redis.hget('user:1000', 'name');
console.log(name); // 'Alice'

// Get a non-existent field
const age = await redis.hget('user:1000', 'age');
console.log(age); // null

Working with typed data

interface User {
  name: string;
  email: string;
}

// Get with type inference
const email = await redis.hget<string>('user:1000', 'email');
console.log(email); // '[email protected]'

See Also

  • HSET - Set hash field values
  • HGETALL - Get all fields and values in a hash
  • HMGET - Get multiple hash field values

Build docs developers (and LLMs) love