> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# HGETEX

> Get hash fields with expiration support.

The `HGETEX` command returns the values of the specified fields and optionally sets their expiration time or TTL. This allows you to retrieve hash data while managing its lifetime in a single operation.

## Arguments

<ParamField body="key" type="string" required>
  The key of the hash.
</ParamField>

<ParamField body="options" type="object" required>
  Expiration options for the hash fields. The options are mutually exclusive - you can only use one at a time.

  <Expandable title="properties">
    <ParamField body="ex" type="number">
      Set expiration time in seconds. Cannot be used with other expiration options.
    </ParamField>

    <ParamField body="px" type="number">
      Set expiration time in milliseconds. Cannot be used with other expiration options.
    </ParamField>

    <ParamField body="exat" type="number">
      Set expiration as Unix timestamp in seconds. Cannot be used with other expiration options.
    </ParamField>

    <ParamField body="pxat" type="number">
      Set expiration as Unix timestamp in milliseconds. Cannot be used with other expiration options.
    </ParamField>

    <ParamField body="persist" type="true">
      Remove expiration from the hash. Must be set to `true`. Cannot be used with other expiration options.
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="fields" type="...string[]" required>
  One or more field names to get.
</ParamField>

## Response

<ResponseField type="Record<string, TValue | null> | null" required>
  An object containing the field names and their values in the format `{[fieldName: string]: TValue | null}`. Returns `null` for fields that do not exist. If the hash doesn't exist or all fields are non-existent, `null` is returned.
</ResponseField>

<RequestExample>
  ```ts With EX (seconds) theme={"system"}
  await redis.hset("user:123", { name: "John", email: "john@example.com" });

  // Get fields and set expiration to 60 seconds
  const result = await redis.hgetex("user:123", { ex: 60 }, "name", "email");
  console.log(result); // { name: "John", email: "john@example.com" }
  ```

  ```ts With PX (milliseconds) theme={"system"}
  await redis.hset("session:abc", { token: "xyz123", user: "john" });

  // Get fields and set expiration to 30000 milliseconds (30 seconds)
  const result = await redis.hgetex("session:abc", { px: 30000 }, "token", "user");
  console.log(result); // { token: "xyz123", user: "john" }
  ```

  ```ts With EXAT (Unix timestamp in seconds) theme={"system"}
  await redis.hset("cache:data", { value: "cached" });

  // Set expiration to specific timestamp (1 hour from now)
  const futureTime = Math.floor(Date.now() / 1000) + 3600;
  const result = await redis.hgetex("cache:data", { exat: futureTime }, "value");
  console.log(result); // { value: "cached" }
  ```

  ```ts With PXAT (Unix timestamp in milliseconds) theme={"system"}
  await redis.hset("temp:data", { info: "temporary" });

  // Set expiration to specific timestamp in milliseconds
  const futureTime = Date.now() + 60000; // 1 minute from now
  const result = await redis.hgetex("temp:data", { pxat: futureTime }, "info");
  console.log(result); // { info: "temporary" }
  ```

  ```ts With PERSIST theme={"system"}
  await redis.hset("user:456", { name: "Jane" });
  await redis.expire("user:456", 300); // Set 5 minute expiration

  // Get fields and remove expiration
  const result = await redis.hgetex("user:456", { persist: true }, "name");
  console.log(result); // { name: "Jane" }

  // Verify expiration was removed
  const ttl = await redis.ttl("user:456");
  console.log(ttl); // -1 (no expiration)
  ```

  ```ts Without Expiration Options theme={"system"}
  await redis.hset("data:xyz", { field1: "value1", field2: "value2" });

  // Just get fields without changing expiration
  // Pass an empty object or an object with no expiration properties
  const result = await redis.hgetex("data:xyz", {}, "field1", "field2");
  console.log(result); // { field1: "value1", field2: "value2" }
  ```

  ```ts Non-existent Fields theme={"system"}
  await redis.hset("user:789", { name: "Bob" });

  // Get fields including one that doesn't exist
  const result = await redis.hgetex("user:789", {}, "name", "email");
  console.log(result); // { name: "Bob", email: null }
  ```
</RequestExample>

## Use Cases

* **Session Management**: Retrieve session data and extend its lifetime
* **Cache Refresh**: Get cached data and update its TTL
* **Temporary Data**: Access temporary data while managing its expiration
* **Rate Limiting**: Fetch rate limit data and reset expiration
