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

# ZADD

> Add members with scores.

Use `ZADD` to add members with a score to a sorted set, or to change the score of members that are already there.

Members are kept ordered by score, and members with equal scores are ordered lexicographically, which is what makes range queries by score or by member name possible. Scores are double precision floats and accept `+inf` and `-inf`. The key is created on first use, and the reply counts the members that were added.

`NX` only adds new members and never changes an existing score, while `XX` only updates members that already exist. `GT` and `LT` update a score only when the new one is greater or less than the current one, which is how you keep a running maximum or minimum, such as a high score or an earliest deadline, without reading the old value first. `CH` makes the reply count every member that changed, added or updated, instead of only the new ones. `INCR` treats the given score as an increment and returns the member's new score, behaving like [`ZINCRBY`](/docs/redis/commands/sorted-set/zincrby) for a single member and returning null when a condition prevented the change.

## Syntax

```redis theme={"system"}
ZADD <key> [NX | XX] [GT | LT] [CH] [INCR] <score> <member> [<score> <member> ...]
```

## Arguments

| Argument           | Required | Repeatable | Description                                                                                                                                                              |
| ------------------ | -------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `<key>`            | Yes      | No         | Redis key targeted by the command.                                                                                                                                       |
| `(NX \| XX)`       | No       | No         | Choose one form: `NX` (only add new members, never update an existing one); `XX` (only update members that already exist).                                               |
| `(GT \| LT)`       | No       | No         | Choose one form: `GT` (only update when the new score is greater than the current one); `LT` (only update when it is less). Neither blocks new members from being added. |
| `CH`               | No       | No         | Count changed members rather than only new members.                                                                                                                      |
| `INCR`             | No       | No         | Increment one member instead of adding normally.                                                                                                                         |
| `<score> <member>` | Yes      | Yes        | Score followed by the member it applies to. Repeat to add several members.                                                                                               |

## Important points

* `NX` cannot be combined with `XX`, `GT`, or `LT`, and `GT` and `LT` cannot be used together.
* RESP2 represents floating-point reply values as bulk strings; RESP3 may use native double replies. Client libraries commonly decode either form to a language number.

## Response

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

| Protocol | Reply                                                                        |
| -------- | ---------------------------------------------------------------------------- |
| RESP2    | Null bulk string or null array or Integer or Bulk string containing a number |
| RESP3    | Null or Integer or Double                                                    |

<Note>
  Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
</Note>

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. REST examples use `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`.

<AccordionGroup>
  <Accordion title="Redis CLI" icon="terminal">
    ```bash theme={"system"}
    ZADD my-key 1.5 member
    ```
  </Accordion>

  <Accordion title="@upstash/redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { Redis } from "@upstash/redis";

    const redis = Redis.fromEnv();

    await redis.zadd(
        "key",
        { score: 2, member: "member" },
        { score: 3, member: "member2"},
    );
    ```
  </Accordion>

  <Accordion title="upstash_redis" icon="python" iconType="brands">
    ```python theme={"system"}
    from upstash_redis import Redis

    redis = Redis.from_env()
    result = redis.zadd("my-key", {"member": 1.5})
    print(result)
    ```
  </Accordion>

  <Accordion title="ioredis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import Redis from "ioredis";

    const redis = new Redis(process.env.REDIS_URL!);
    const result = await redis.zadd("my-key", "1.5", "member");
    console.log(result);
    ```
  </Accordion>

  <Accordion title="node-redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { createClient } from "redis";

    const client = await createClient({ url: process.env.REDIS_URL })
      .on("error", console.error)
      .connect();
    const result = await client.zAdd("my-key", { score: 1.5, value: "member" });
    console.log(result);
    ```
  </Accordion>

  <Accordion title="redis-py" icon="python" iconType="brands">
    ```python theme={"system"}
    import os
    import redis

    client = redis.from_url(os.environ["REDIS_URL"])
    result = client.zadd("my-key", {"member": 1.5})
    print(result)
    ```
  </Accordion>

  <Accordion title="go-redis" icon="golang" iconType="brands">
    ```go theme={"system"}
    package main

    import (
        "context"
        "fmt"
        "os"

        "github.com/redis/go-redis/v9"
    )

    func main() {
        opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
        if err != nil {
            panic(err)
        }
        client := redis.NewClient(opts)
        result, err := client.ZAdd(context.Background(), "my-key", redis.Z{Score: 1.5, Member: "member"}).Result()
        if err != nil {
            panic(err)
        }
        fmt.Println(result)
    }
    ```
  </Accordion>

  <Accordion title="jedis" icon="java" iconType="brands">
    ```java theme={"system"}
    import java.net.URI;

    import redis.clients.jedis.Jedis;

    try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
      Object result = jedis.zadd("my-key", 1.5, "member");
      System.out.println(result);
    }
    ```
  </Accordion>

  <Accordion title="redis-rs" icon="rust" iconType="brands">
    ```rust theme={"system"}
    use redis::TypedCommands;

    fn main() -> redis::RedisResult<()> {
        let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
        let client = redis::Client::open(url)?;
        let mut connection = client.get_connection()?;

        let result = connection.zadd("my-key", "member", 1.5)?;
        println!("{result:?}");
        Ok(())
    }
    ```
  </Accordion>
</AccordionGroup>


## Related topics

- [ZADD](/docs/redis/sdks/ts/commands/zset/zadd.md)
- [ZUNIONSTORE](/docs/redis/commands/sorted-set/zunionstore.md)
- [ZINTERSTORE](/docs/redis/commands/sorted-set/zinterstore.md)
- [REST API](/docs/redis/features/restapi.md)
