{
  "schema_version": 2,
  "id": "develop/clients/hiredis",
  "title": "hiredis guide (C)",
  "url": "https://redis.io/docs/latest/develop/clients/hiredis/",
  "summary": "Connect your C application to a Redis database.",
  "aliases": [
    "/connect/clients/hiredis/",
    "/clients/hiredis/",
    "/develop/clients/c"
  ],
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-09T11:06:49+01:00",
  "children": [
    {
      "id": "develop/clients/hiredis/connect",
      "summary": "Connect to the server with `hiredis`.",
      "title": "Connect",
      "url": "https://redis.io/docs/latest/develop/clients/hiredis/connect/"
    },
    {
      "id": "develop/clients/hiredis/issue-commands",
      "summary": "Construct commands and send them to the Redis server.",
      "title": "Issue commands",
      "url": "https://redis.io/docs/latest/develop/clients/hiredis/issue-commands/"
    },
    {
      "id": "develop/clients/hiredis/handle-replies",
      "summary": "Handle command replies with `hiredis`.",
      "title": "Handle command replies",
      "url": "https://redis.io/docs/latest/develop/clients/hiredis/handle-replies/"
    },
    {
      "id": "develop/clients/hiredis/transpipe",
      "summary": "Learn how to use Redis pipelines and transactions",
      "title": "Pipelines and transactions",
      "url": "https://redis.io/docs/latest/develop/clients/hiredis/transpipe/"
    },
    {
      "id": "develop/clients/hiredis/int-examples",
      "summary": "Integrate hiredis with C++ and external frameworks.",
      "title": "Integration guides",
      "url": "https://redis.io/docs/latest/develop/clients/hiredis/int-examples/"
    }
  ],
  "page_type": "content",
  "content_hash": "c07c16c615d2e385874aeee0601a52fa945195662f493c7eb3286ec1ad5b6c0b",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "[`hiredis`](https://github.com/redis/hiredis) is the\n[C language](https://en.wikipedia.org/wiki/C_(programming_language))\nclient for Redis.\nThe sections below explain how to install `hiredis` and connect your application\nto a Redis database.\n\n`hiredis` requires a running Redis or [Redis Stack](https://redis.io/docs/latest/operate/oss_and_stack/install/install-stack) server. See [Getting started](https://redis.io/docs/latest/operate/oss_and_stack/install) for Redis installation instructions."
    },
    {
      "id": "build-and-install",
      "title": "Build and install",
      "role": "content",
      "text": "Clone or download the `hiredis` source from the [Github repository](https://github.com/redis/hiredis).\nThen, in a terminal, go into the `hiredis` folder and run the `make` command to build\nthe dynamically-loaded library for `hiredis` (this has the name `libhiredis.dylib` on\nMacOS and `libhiredis.so` on Linux). You can copy this library to your\nproject folder or run `sudo make install` to install it to `/usr/local/lib`."
    },
    {
      "id": "connect-and-test",
      "title": "Connect and test",
      "role": "content",
      "text": "The code in the example below connects to the server, stores and retrieves\na string key using [`SET`](https://redis.io/docs/latest/commands/set) and\n[`GET`](https://redis.io/docs/latest/commands/get), and then finally closes the\nconnection. An explanation of the code follows the example.\n\nFoundational: Connect to a Redis server, set and retrieve string values using SET and GET, then close the connection\n\n[code example]\n\n\n\nFor a real project, you would build your code with a makefile, but for\nthis simple test, you can just place it in a file called `main.c` and\nbuild it with the following command. (If you didn't install `hiredis`\nusing `make install`, then you should also use the `-I` option to\nspecify the folder that contains the `hiredis` headers.)\n\n[code example]\n\nThe default executable filename is `a.out`. If you run this file from\nthe terminal, you should see the following output:\n\n[code example]\n\nThe code first uses `redisConnect()` to open the connection for\nall subsequent commands to use. See\n[Connect](https://redis.io/docs/latest/develop/clients/hiredis/connect) for\nmore information about connecting to Redis.\n\nThe `redisCommand()` function\nissues commands to the server, each of which returns a\n`redisReply` pointer. Here, the reply is a string, which you can\naccess using the `str` field of the reply. The `redisCommand()`\ncall allocates memory for the reply, so you should free this\nwith `freeReplyObject()` when you have finished using it.\nSee [Issue commands](https://redis.io/docs/latest/develop/clients/hiredis/issue-commands)\nand [Handle replies](https://redis.io/docs/latest/develop/clients/hiredis/handle-replies)\nfor more information.\n\nFinally, you should close the connection to Redis with a\ncall to `redisFree()`. This is not strictly necessary\nfor this short test program, but real-world code will typically\nopen and use many connections. You must free them after using them\nto prevent errors."
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "The [`hiredis`](https://github.com/redis/hiredis) Github repository contains\nexamples and details that may be useful if you are using `hiredis` to\nimplement a higher-level client for another programming language. There are\nalso examples showing how to use `hiredis` adapter headers to integrate with\nvarious event handling frameworks.\n\nSee the other pages in this section for more information and examples."
    }
  ],
  "examples": [
    {
      "id": "connect-and-test-ex0",
      "language": "c",
      "code": "// The following comment is required to make the example interactive.\n//%cflags:-lhiredis\n#include <stdio.h>\n#include <stdlib.h>\n\n#include <hiredis/hiredis.h>\n\n\nint main() {\n    // The `redisContext` type represents the connection\n    // to the Redis server. Here, we connect to the\n    // default host and port.\n    redisContext *c = redisConnect(\"127.0.0.1\", 6379);\n\n    // Check if the context is null or if a specific\n    // error occurred.\n    if (c == NULL || c->err) {\n        if (c != NULL) {\n            printf(\"Error: %s\\n\", c->errstr);\n            // handle error\n        } else {\n            printf(\"Can't allocate redis context\\n\");\n        }\n\n        exit(1);\n    }\n\n    // Set a string key.\n    redisReply *reply = redisCommand(c, \"SET foo bar\");\n    printf(\"Reply: %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    // Get the key we have just stored.\n    reply = redisCommand(c, \"GET foo\");\n    printf(\"Reply: %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    // Close the connection.\n    redisFree(c);\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex1",
      "language": "bash",
      "code": "cc main.c -L/usr/local/lib -lhiredis",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex2",
      "language": "plaintext",
      "code": "% ./a.out                             \nReply: OK\nReply: bar",
      "section_id": "connect-and-test"
    }
  ]
}
