{
  "schema_version": 2,
  "id": "operate/oss_and_stack/stack-with-enterprise/deprecated-features/triggers-and-functions/Quick_Start_CLI",
  "title": "Quick start using redis-cli",
  "url": "https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/triggers-and-functions/quick_start_cli/",
  "summary": "Get started with triggers and functions using redis-cli",
  "aliases": [
    "/develop/interact/programmability/triggers-and-functions/quick_start_ri/"
  ],
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-21T16:36:38+01:00",
  "page_type": "content",
  "content_hash": "bb4819cc0387c6588cdce2d568a84bed2141914e26bd27247709970a18df85e9",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Make sure that you have [Redis Stack installed](https://redis.io/docs/latest/operate/oss_and_stack/install/install-stack) and running. Alternatively, you can create a [free Redis Cloud account](https://redis.com/try-free/?utm_source=redisio&utm_medium=referral&utm_campaign=2023-09-try_free&utm_content=cu-redis_cloud_users). The triggers and functions preview is available in the fixed subscription plan for the Google Cloud Asia Pacific (Tokyo) and AWS Asia Pacific (Singapore) regions."
    },
    {
      "id": "connect-to-redis-stack",
      "title": "Connect to Redis Stack",
      "role": "content",
      "text": "[code example]"
    },
    {
      "id": "load-a-library",
      "title": "Load a library",
      "role": "content",
      "text": "Use the `TFUNCION LOAD` command to create a new library in your Redis instance.\n\n[code example]\n\nWhen the library is created successfully, an `OK` response is returned. Run the `TFUNCTION LIST` command to confirm your library was added to Redis.\n\n[code example]\n\nThe `TFCALL` command is used to execute the JavaScript Function. If the command fails, an error will be returned.\n\n[code example]\n\nTo update the library run the `TFUNCTION LOAD` command with the additional parameter `REPLACE`.\n\n[code example]"
    },
    {
      "id": "uploading-an-external-file",
      "title": "Uploading an external file",
      "role": "content",
      "text": "Use the `redis-cli` command to upload JavaScript from an external file. The file needs to contain the header, which contains the engine identifier, the API version, and the library name: `#!js api_version=1.0 name=myFirstLibrary`.\n\n[code example]\n\nUse the `redis-cli -x` option to send the file with the command and use the `TFUNCTION LOAD REPLACE` to replace the inline library with the one from the `main.js` file.\n\n[code example]"
    },
    {
      "id": "creating-triggers",
      "title": "Creating triggers",
      "role": "content",
      "text": "Functions within Redis can respond to events using keyspace triggers. While the majority of these events are initiated by command invocations, they also include events that occur when a key expires or is removed from the database.\n\nFor the full list of supported events, please refer to the [Redis keyspace notifications page](https://redis.io/docs/latest/develop/pubsub/keyspace-notifications#events-generated-by-different-commands/?utm_source=redis\\&utm_medium=app\\&utm_campaign=redisinsight_triggers_and_functions_guide).\n\nThe following code creates a new keyspace trigger that adds a new field to a new or updated hash with the latest update time. \n\nLoad the code in your database:\n\n[code example]\n\nAdd a new hash with the required prefix to trigger our function.\n\n[code example]\n\nCheck if the last updated time is added to the example.\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "connect-to-redis-stack-ex0",
      "language": "Shell",
      "code": "> redis-cli -h 127.0.0.1 -p 6379",
      "section_id": "connect-to-redis-stack"
    },
    {
      "id": "load-a-library-ex0",
      "language": "Shell",
      "code": "127.0.0.1:6379> TFUNCTION LOAD \"#!js api_version=1.0 name=myFirstLibrary\\n redis.registerFunction('hello', ()=>{ return 'Hello World'})\"\nOK",
      "section_id": "load-a-library"
    },
    {
      "id": "load-a-library-ex1",
      "language": "shell",
      "code": "> TFUNCTION LIST\n1) 1) \"api_version\"\n   2) \"1.0\"\n   3) \"cluster_functions\"\n   4) (empty list or set)\n   5) \"configuration\"\n   6) \"null\"\n   7) \"engine\"\n   8) \"js\"\n   9) \"functions\"\n   10) 1) \"hello\"\n   11) \"keyspace_triggers\"\n   12) (empty list or set)\n   13) \"name\"\n   14) \"myFirstLibrary\"\n   15) \"pending_async_calls\"\n   16) (empty list or set)\n   17) \"pending_jobs\"\n   18) \"0\"\n   19) \"stream_triggers\"\n   20) (empty list or set)\n   21) \"user\"\n   22) \"default\"",
      "section_id": "load-a-library"
    },
    {
      "id": "load-a-library-ex2",
      "language": "Shell",
      "code": "127.0.0.1:6379> TFCALL myFirstLibrary.hello 0\n\"Hello World\"",
      "section_id": "load-a-library"
    },
    {
      "id": "load-a-library-ex3",
      "language": "Shell",
      "code": "127.0.0.1:6379> TFUNCTION LOAD REPLACE \"#!js api_version=1.0 name=myFirstLibrary\\n redis.registerFunction('hello', ()=>{ return 'Hello World updated'})\"\nOK",
      "section_id": "load-a-library"
    },
    {
      "id": "uploading-an-external-file-ex0",
      "language": "JavaScript",
      "code": "#!js api_version=1.0 name=lib\n\nredis.registerFunction('hello', ()=> {\n  return 'Hello from an external file'\n})",
      "section_id": "uploading-an-external-file"
    },
    {
      "id": "uploading-an-external-file-ex1",
      "language": "Shell",
      "code": "redis-cli -x TFUNCTION LOAD REPLACE < ./main.js",
      "section_id": "uploading-an-external-file"
    },
    {
      "id": "creating-triggers-ex0",
      "language": "redis Load keyspace example",
      "code": "TFUNCTION LOAD REPLACE \"#!js name=myFirstLibrary api_version=1.0\\n \n    function addLastUpdatedField(client, data) {\n        if(data.event == 'hset') {\n            var currentDateTime = Date.now();\n            client.call('hset', data.key, 'last_updated', currentDateTime.toString());\n        }\n    } \n    redis.registerKeySpaceTrigger('addLastUpdated', 'fellowship:', addLastUpdatedField);\" // Register the KeySpaceTrigger 'AddLastUpdated' for keys with the prefix 'fellowship' with a callback to the function 'addLastUpdatedField'",
      "section_id": "creating-triggers"
    },
    {
      "id": "creating-triggers-ex1",
      "language": "Shell",
      "code": "127.0.0.1:6379> HSET fellowship:1 name \"Frodo Baggins\" title \"The One Ring Bearer\"",
      "section_id": "creating-triggers"
    },
    {
      "id": "creating-triggers-ex2",
      "language": "Shell",
      "code": "127.0.0.1:6379> HGETALL fellowship:1\n1) \"name\"\n2) \"Frodo Baggins\"\n3) \"title\"\n4) \"The One Ring Bearer\"\n5) \"last_updated\"\n6) \"1693238681822\"",
      "section_id": "creating-triggers"
    }
  ]
}
