> ## 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.

# SET

> Set a key to hold a string value.

## Arguments

<ParamField body="key" type="str" required>
  The key
</ParamField>

<ParamField body="value" type="TValue" required>
  The value, if this is not a string, we will use `JSON.stringify` to convert it
  to a string.
</ParamField>

<ParamField body="get" type="bool">
  Instead of returning `True`, this will cause the command to return the old
  value stored at key, or `None` when key did not exist.
</ParamField>

<ParamField body="ex" type="int">
  Sets an expiration (in seconds) to the key.
</ParamField>

<ParamField body="px" type="int">
  Sets an expiration (in milliseconds) to the key.
</ParamField>

<ParamField body="exat" type="int">
  Set the UNIX timestamp in seconds until the key expires.
</ParamField>

<ParamField body="pxat" type="int">
  Set the UNIX timestamp in milliseconds until the key expires.
</ParamField>

<ParamField body="keepttl" type="bool">
  Keeps the old expiration if the key already exists.
</ParamField>

<ParamField body="nx" type="bool">
  Only set the key if it does not already exist.
</ParamField>

<ParamField body="xx" type="bool">
  Only set the key if it already exists.
</ParamField>

## Response

<ResponseField required>
  `True` if the key was set.
  If `get` is specified, this will return the old value stored at key, or `None` when
  the key did not exist.
</ResponseField>

<RequestExample>
  ```py Basic theme={"system"}
  assert redis.set("key", "value") == True

  assert redis.get("key") == "value"
  ```

  ```py With nx and xx theme={"system"}
  # Only set the key if it does not already exist.
  assert redis.set("key", "value", nx=True) == False

  # Only set the key if it already exists.
  assert redis.set("key", "value", xx=True) == True
  ```

  ```py With expiration theme={"system"}
  # Set the key to expire in 10 seconds.
  assert redis.set("key", "value", ex=10) == True

  # Set the key to expire in 10000 milliseconds.
  assert redis.set("key", "value", px=10000) == True
  ```

  ```py With old value theme={"system"}
  # Get the old value stored at the key.
  assert redis.set("key", "new-value", get=True) == "old-value"
  ```
</RequestExample>
