# SETEX

> Set a string value with a time to live in seconds.

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

```
SETEX key seconds value
```

- Group: String
- Complexity: O(1)
- HotKV Cloud: all plans
- On-Premise: no license needed

`SETEX` stores `value` at `key` and makes it expire after `seconds`, in one
step. It is the same as `SET key value EX seconds`. Any existing value, of any
type, is overwritten.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Always | Simple string `OK` | Simple string `OK` |

Errors:

- `ERR value is not an integer or out of range` when `seconds` is not an
  integer.
- `ERR invalid expire time in 'setex' command` when `seconds` is zero,
  negative or too large.

## Examples

```
127.0.0.1:6379> SETEX session:9 600 "user:9"
OK
127.0.0.1:6379> TTL session:9
(integer) 600
127.0.0.1:6379> SETEX session:9 0 "user:9"
(error) ERR invalid expire time in 'setex' command
```

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[SET](https://docs.hotkv.com/commands/set/index.md), [PSETEX](https://docs.hotkv.com/commands/psetex/index.md),
[EXPIRE](https://docs.hotkv.com/commands/expire/index.md), [MSETEX](https://docs.hotkv.com/commands/msetex/index.md)