{"id":17655830,"url":"https://github.com/sagi/workers-kv","last_synced_at":"2025-03-06T05:10:38.050Z","repository":{"id":35018584,"uuid":"195848107","full_name":"sagi/workers-kv","owner":"sagi","description":"Cloudflare Workers KV API for Node.js","archived":false,"fork":false,"pushed_at":"2024-09-03T19:49:30.000Z","size":306,"stargazers_count":53,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-27T04:12:41.751Z","etag":null,"topics":["api","cloudflare-workers","kv","node"],"latest_commit_sha":null,"homepage":"https://sagi.io","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sagi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-08T16:18:42.000Z","updated_at":"2025-02-02T13:57:14.000Z","dependencies_parsed_at":"2025-01-28T21:02:30.870Z","dependency_job_id":"681eca5d-93ca-4c9c-a0e6-727230202d69","html_url":"https://github.com/sagi/workers-kv","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagi%2Fworkers-kv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagi%2Fworkers-kv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagi%2Fworkers-kv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagi%2Fworkers-kv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sagi","download_url":"https://codeload.github.com/sagi/workers-kv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242150773,"owners_count":20080006,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","cloudflare-workers","kv","node"],"created_at":"2024-10-23T13:08:12.033Z","updated_at":"2025-03-06T05:10:38.028Z","avatar_url":"https://github.com/sagi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# workers-kv\n\n[`@sagi.io/workers-kv`](https://www.npmjs.com/package/@sagi.io/workers-kv) is a Cloudflare Workers KV API for Node.js.\n\n⭐ We use it at **[OpenSay](https://opensay.co/?s=workers-kv)** to efficiently cache data on Cloudflare Workers KV.\n\n[![CircleCI](https://circleci.com/gh/sagi/workers-kv.svg?style=svg\u0026circle-token=c5ae7a8993d47db9ca08a628614585ca45c75f33)](https://circleci.com/gh/sagi/workers-kv)\n[![MIT License](https://img.shields.io/npm/l/@sagi.io/workers-kv.svg?style=flat-square)](http://opensource.org/licenses/MIT)\n[![version](https://img.shields.io/npm/v/@sagi.io/workers-kv.svg?style=flat-square)](http://npm.im/@sagi.io/workers-kv)\n\n## Installation\n\n~~~\n$ npm i @sagi.io/workers-kv\n~~~\n\n## Quickstart\n\nFirst, instantiate a `WorkersKVREST` instance:\n\n~~~js\nconst WorkersKVREST = require('@sagi.io/workers-kv')\n\nconst cfAccountId = process.env.CLOUDFLARE_ACCOUNT_ID;\nconst cfAuthKey = process.env.CLOUDFLARE_AUTH_KEY;\nconst cfEmail = process.env.CLOUDFLARE_EMAIL;\n\nconst WorkersKV = new WorkersKVREST({ cfAccountId, cfAuthKey, cfEmail })\n~~~\n\nThen, access it's instance methods. For instance:\n\n~~~js\nconst namespaceId = '...'\n\nconst allKeys = await KV.listAllKeys({ namespaceId })\n~~~\n\n## API\n\nWe adhere to [Cloudflare's Workers KV REST API](https://api.cloudflare.com/#workers-kv-namespace-properties).\n\n### **`WorkersKVREST({ ... })`**\n\nInstantiates a `WorkersKV` object with the defined below methods.\n\nFunction definition:\n\n```js\nconst WorkersKVREST = function({\n  cfAccountId,\n  cfEmail,\n  cfAuthKey,\n  namespaceId = '',\n}){ ... }\n```\n\nWhere:\n\n  - **`cfAccountId`** *required* Your Cloudflare account id.\n  - **`cfEmail`** *optional|required* The email you registered with Cloudflare.\n  - **`cfAuthKey`** *optional|required* Your Cloudflare Auth Key.\n  - **`cfAuthToken`** *optional|required* Your Cloudflare Auth Token.\n  - **`namespaceId`** *optional* The `Workers KV` namespace id. This argument is *optional* - either provide it here, or via the methods below.\n\nUse `cfAuthToken` with a [Cloudflare auth token](https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys). You can also set `cfEmail` and `cfAuthKey` directly without using an auth token.\n\n### **`WorkersKV.listKeys({ ... })`**\n\nFunction definition:\n\n```js\nconst listKeys = async ({\n  namespaceId = '',\n  limit = MAX_KEYS_LIMIT,\n  cursor = undefined,\n  prefix = undefined,\n} = {}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`namespaceId`** *optional* The namespace id (can also be provided while instantiating `WorkersKV`).\n  - **`limit`** *optional* The number of keys to return. The cursor attribute may be used to iterate over the next batch of keys if there are more than the limit.\n  - **`cursor`** *optional* Opaque token indicating the position from which to continue when requesting the next set of records if the amount of list results was limited by the limit parameter. A valid value for the cursor can be obtained from the cursors object in the result_info structure.\n  - **`prefix`** *optional* A string prefix used to filter down which keys will be returned. Exact matches and any key names that begin with the prefix will be returned.\n\n### **`WorkersKV.listAllKeys({ ... })`**\n\nCursors through `listKeys` requests for you.\n\nFunction definition:\n\n```js\nconst listAllKeys = async ({\n  namespaceId = '',\n  prefix = undefined,\n  limit = MAX_KEYS_LIMIT,\n} = {}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`namespaceId`** *optional* The namespace id (can also be provided while instantiating `WorkersKV`).\n  - **`cursor`** *optional* Opaque token indicating the position from which to continue when requesting the next set of records if the amount of list results was limited by the limit parameter. A valid value for the cursor can be obtained from the cursors object in the result_info structure.\n  - **`prefix`** *optional* A string prefix used to filter down which keys will be returned. Exact matches and any key names that begin with the prefix will be returned.\n\n### **`listNamespaces({ ... })`**\n\nFunction definition:\n\n```js\nconst listNamespaces = async ({\n  page = 1,\n  per_page = 50,\n} = {}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`page`** *optional* Page number of paginated results.\n  - **`per_page`** *optional* Maximum number of results per page.\n\n### **`readKey({ ... })`**\n\nFunction definition:\n\n```js\nconst readKey = async ({\n  key,\n  namespaceId = '',\n}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`key`** *required* the key name.\n  - **`namespaceId`** *optional* The namespace id (can also be provided while instantiating `WorkersKV`).\n\n### **`WorkersKV.deleteKey({ ... })`**\n\nFunction definition:\n\n```js\nconst deleteKey= async ({\n  key,\n  namespaceId = '',\n}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`key`** *required* the key name.\n  - **`namespaceId`** *optional* The namespace id (can also be provided while instantiating `WorkersKV`).\n\n### **`WorkersKV.writeKey({ ... })`**\n\nFunction definition:\n\n```js\nconst writeKey=\u003e async ({\n  key,\n  value,\n  namespaceId = '',\n  expiration = undefined,\n  expiration_ttl = undefined,\n}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`key`** *required* A key's name. The name may be at most 512 bytes. All printable, non-whitespace characters are valid.\n  - **`value`** *required* A UTF-8 encoded string to be stored, up to 10 MB in length.\n  - **`namespaceId`** *optional* Is the namespace id (can also be provided while instantiating `WorkersKV`).\n  - **`expiration`** *optional* The time, measured in number of seconds since the UNIX epoch, at which the key should expire.\n  - **`expiration_ttl`** *optional* The number of seconds for which the key should be visible before it expires. At least 60.\n\n### **`WorkersKV.writeMultipleKeys({ ... })`**\n\nFunction definition:\n\n```js\nconst writeMultipleKeys =\u003e async ({\n  keyValueMap,\n  namespaceId = '',\n  expiration = undefined,\n  expiration_ttl = undefined,\n  base64 = false,\n}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`keyValueMap`** *required* Is an object with string keys and values. e.g  `{ keyName1: 'keyValue1', keyName2: 'keyValue2' }`\n  - **`namespaceId`** *optional* Is the namespace id (can also be provided while instantiating `WorkersKV`).\n  - **`expiration`** *optional* The time, measured in number of seconds since the UNIX epoch, at which the key should expire.\n  - **`expiration_ttl`** *optional* The number of seconds for which the key should be visible before it expires. At least 60.\n  - **`base64`** *optional* Whether or not the server should base64 decode the value before storing it. Useful for writing values that wouldn't otherwise be valid JSON strings, such as images. Default: false.\n\n### **`WorkersKV.deleteMultipleKeys({ ... })`**\n\nFunction definition:\n\n```js\nconst deleteMultipleKeys = async ({\n  keys,\n  namespaceId = '',\n}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`keys`** *required* An array of keys to be deleted.\n  - **`namespaceId`** *optional* The namespace id (can also be provided while instantiating `WorkersKV`).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagi%2Fworkers-kv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsagi%2Fworkers-kv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagi%2Fworkers-kv/lists"}