{
  "id": "user_functions",
  "title": "User functions",
  "url": "https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/triggers-and-functions/concepts/triggers/user_functions/",
  "summary": "Execute JavaScript functions via `TFCALL` or `TFCALLASYNC`",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-04-01T08:10:08-05:00",
  "page_type": "content",
  "content_hash": "99935aca0f69441ece2ed8145e0019b15e0a335232ce925e7313d7c191567c8d",
  "sections": [
    {
      "id": "content",
      "title": "Content",
      "role": "content",
      "text": "All `TFCALL` command arguments that follow the function name are passed to the function callback. The following example shows how to implement a simple function that returns the value of a key of type string or hash:\n\n[code example]\n\nExample of running the preceding function:\n\n[code example]\n\nIt is also possible to get all the function arguments as a `JS` array. This is how we can extend the above example to accept multiple keys and return their values:\n\n[code example]\n\nRun example:\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "content-ex0",
      "language": "js",
      "code": "#!js api_version=1.0 name=lib\n\nredis.registerFunction('my_get', function(client, key_name){\n    if (client.call('type', key_name) == 'string') {\n        return client.call('get', key_name);\n    }\n    if (client.call('type', key_name) == 'hash') {\n        return client.call('hgetall', key_name);\n    }\n    throw \"Unsupported type\";\n});",
      "section_id": "content"
    },
    {
      "id": "content-ex1",
      "language": "bash",
      "code": "127.0.0.1:6379> set x 1\nOK\n127.0.0.1:6379> TFCALL lib.my_get 1 x\n\"1\"\n127.0.0.1:6379> hset h a b x y\n(integer) 2\n127.0.0.1:6379> TFCALL lib.my_get 1 h\n1) \"a\"\n2) \"b\"\n3) \"x\"\n4) \"y\"",
      "section_id": "content"
    },
    {
      "id": "content-ex2",
      "language": "js",
      "code": "#!js api_version=1.0 name=lib\n\nredis.registerFunction('my_get', function(client, ...keys){\n    var results = [];\n    keys.forEach((key_name)=> {\n            if (client.call('type', key_name) == 'string') {\n                results.push(client.call('get', key_name));\n                return;\n            }\n            if (client.call('type', key_name) == 'hash') {\n                results.push(client.call('hgetall', key_name));\n                return;\n            }\n            results.push(\"Unsupported type\");\n        }\n    );\n    return results;\n\n});",
      "section_id": "content"
    },
    {
      "id": "content-ex3",
      "language": "bash",
      "code": "127.0.0.1:6379> TFCALL foo.my_get 2 x h\n1) \"1\"\n2) 1) \"a\"\n   2) \"b\"\n   3) \"x\"\n   4) \"y\"",
      "section_id": "content"
    }
  ]
}
