# HSET

> Set one or more fields of a hash, creating the hash if it does not exist.

URL: https://docs.hotkv.com/commands/hset/

```
HSET key field value [field value ...]
```

- Group: Hash
- Complexity: O(1) for each field/value pair given
- HotKV Cloud: all plans
- On-Premise: no license needed

`HSET` writes each `field` and `value` pair into the hash stored at `key`. A
missing key is created as an empty hash first. A field that already exists
gets the new value, and any time to live set on that field with
[HEXPIRE](https://docs.hotkv.com/commands/hexpire/index.md) or a related command is removed. To keep field
TTLs while writing, use [HSETEX](https://docs.hotkv.com/commands/hsetex/index.md) with `KEEPTTL`.

Fields are written left to right, so if the same field appears twice the last
value wins.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Success | Integer: the number of fields that were newly created (updated fields are not counted) | Same |

Errors:

- `WRONGTYPE` if `key` holds a value that is not a hash. Nothing is written.
- ``ERR wrong number of arguments for 'hset' command`` if no pair is given or
  a field has no value.

## Examples

```
127.0.0.1:6379> HSET user:1 name "Ada" lang "en"
(integer) 2
127.0.0.1:6379> HSET user:1 lang "fr" city "Paris"
(integer) 1
127.0.0.1:6379> HGET user:1 lang
"fr"
```

## Compatibility

Same arguments and replies as Redis 7.4 and Valkey 8.

## See also

[HGET](https://docs.hotkv.com/commands/hget/index.md), [HSETNX](https://docs.hotkv.com/commands/hsetnx/index.md), [HSETEX](https://docs.hotkv.com/commands/hsetex/index.md),
[HMSET](https://docs.hotkv.com/commands/hmset/index.md), [HDEL](https://docs.hotkv.com/commands/hdel/index.md), [HGETALL](https://docs.hotkv.com/commands/hgetall/index.md)