{"id":22489531,"url":"https://github.com/pauldenver/generate-api-key","last_synced_at":"2025-08-02T21:32:56.813Z","repository":{"id":43166761,"uuid":"329971588","full_name":"pauldenver/generate-api-key","owner":"pauldenver","description":"A Node.js library for generating random API key/access tokens.","archived":false,"fork":false,"pushed_at":"2022-09-22T13:05:29.000Z","size":31,"stargazers_count":50,"open_issues_count":0,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-08T05:41:56.969Z","etag":null,"topics":["access-token","api","api-rest","javascript","key","rest","restful","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pauldenver.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-15T16:56:07.000Z","updated_at":"2024-10-29T07:59:12.000Z","dependencies_parsed_at":"2022-08-24T16:31:35.968Z","dependency_job_id":null,"html_url":"https://github.com/pauldenver/generate-api-key","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldenver%2Fgenerate-api-key","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldenver%2Fgenerate-api-key/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldenver%2Fgenerate-api-key/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldenver%2Fgenerate-api-key/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pauldenver","download_url":"https://codeload.github.com/pauldenver/generate-api-key/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228500309,"owners_count":17930034,"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":["access-token","api","api-rest","javascript","key","rest","restful","typescript"],"created_at":"2024-12-06T17:20:03.214Z","updated_at":"2024-12-06T17:22:29.670Z","avatar_url":"https://github.com/pauldenver.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# generate-api-key  \n\n[![GitHub package.json version (branch)][version-image]][npm-url]\n[![unit tests][tests-image]][tests-url]\n[![Build Status][travis-image]][travis-url]\n[![coverage][coverage-image]][coverage-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n[![CodeFactor][codefactor-image]][codefactor-url]  \n\n\n`generate-api-key` is a library for generating random API (Application Programming Interface) \nkeys or access tokens. By using this library, a Node.js backend service can generate API keys \nor access tokens, then issue them to users and/or other services that require access to the \ncapabilities and resources provided by the API service.  \n\n\n## Table of contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Generation Methods](#generation-methods)\n- [Options](#options)\n  - [`string` Method](#string-method)\n  - [`bytes` Method](#bytes-method)\n  - [`base32` Method](#base32-method)\n  - [`base62` Method](#base62-method)\n  - [`uuidv4` Method](#uuidv4-method)\n  - [`uuidv5` Method](#uuidv5-method)\n- [Security](#security)\n- [Change Log](#change-log)\n- [License](#license)  \n\u003cbr /\u003e\n\n## Installation\n\nUsing NPM:\n\n```bash\n$ npm install generate-api-key\n```\n\nUsing Yarn:\n\n```bash\n$ yarn add generate-api-key\n```\n\u003cbr /\u003e\n\n## Usage\n\nThe `generate-api-key` library can generate API key/access tokens by utilizing several \ngeneration methods, such as `string`, `bytes`, `base32`, `base62`, `uuidv4`, and \n`uuidv5`. The `string` method is used by default.  \n\u003cbr /\u003e\n\nImporting:  \n\n```javascript\n// CommonJS Import\nconst { generateApiKey } = require('generate-api-key');\n// OR\nconst generateApiKey = require('generate-api-key').default;\n\n// ES6 Import\nimport { generateApiKey } from 'generate-api-key';\n// OR\nimport generateApiKey from 'generate-api-key';\n```\n\nExample:\n\n```javascript\nimport generateApiKey from 'generate-api-key';\n\n// Generate the API key.\ngenerateApiKey(); // ⇨ 'q_EaTiX+xbBXLyO05.+zDXjI+Qi_X0v'\n```\n\u003cbr /\u003e\n\n### Generation Methods\n\n| Method   |  Description                                                           |\n| -------- | ---------------------------------------------------------------------- | \n| `string` | Creates an API key/access token using random string generation         |\n| `bytes`  | Creates an API key/access token using random bytes                     |\n| `base32` | Creates an API key/access token using a random UUID and converting it\u003cbr /\u003einto a [Douglas Crockford Base32](https://en.wikipedia.org/wiki/Base32#Crockford's_Base32) encoded string  |\n| `base62` | Creates an API key using Base62 encoding                               |\n| `uuidv4` | Creates an API key/access token using random UUID Version 4 generation |\n| `uuidv5` | Creates an API key/access token using random UUID Version 5 generation |\n\u003cbr /\u003e\n\n## Options\n\n### `string` Method  \n\nCreates an API key/access token using random string generation.  \n\n| Name     | Default Value |  Description                                                      |\n| ---------| ------------- | ----------------------------------------------------------------- | \n| `method` | `string`      | To use the `string` generation method                             |\n| `min`    | `16`          | The minimum length of the API key (ignored if `length` is given)  |\n| `max`    | `32`          | The maximum length of the API key (ignored if `length` is given)  |\n| `length` | `undefined`   | The length of the API key                                         |\n| `pool`   | `abcdefghijklmnopqrstuvwxyz`\u003cbr /\u003e`ABCDEFGHIJKLMNOPQRSTUVWXYZ`\u003cbr /\u003e`0123456789-._~+/` | The characters used for the API key generation | \n| `prefix` | `undefined`   | A string prefix for the API key, followed by a period (`.`)       |  \n| `batch`  | `undefined`   | The number of API keys to generate                                |\n\u003cbr /\u003e\n\nExamples:  \n\n```javascript\nimport generateApiKey from 'generate-api-key';\n\n// Generate the API key. The 'string' method is used by default.\ngenerateApiKey(); // ⇨ 'q_EaTiX+xbBXLyO05.+zDXjI+Qi_X0v'\n\n// Provide the generation method.\ngenerateApiKey({ method: 'string' }); // ⇨ 'Zt1HbMcLKxk6~nnW'\n\n// Create an API key with a certain length.\ngenerateApiKey({ method: 'string', length: 8 }); // ⇨ 'TNJ6-Lo4'\n\n// Create an API key with a length between a certain range.\ngenerateApiKey({ method: 'string', min: 10, max: 20 }); // ⇨ 'ENwiOFdP8cWj'\n\n// Create an API key with a certain pool of characters.\ngenerateApiKey({\n  method: 'string',\n  pool: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'\n}); // ⇨ 'QFLSGIDLOUAELQZTQXMHQNJ'\n\n// Create an API key with a prefix.\ngenerateApiKey({ method: 'string', prefix: 'test_app' }); // ⇨ 'test_app.aTd34Rli0nir70/8'\n\n// Create a batch (certain amount) of API keys.\ngenerateApiKey({ method: 'string', batch: 5 }); // ⇨ \n  // [\n  //   'w05KkI9AWhKxzvPFtXotUva-',\n  //   'YFL0ICl4PtLD8Y/oQ20iyAE',\n  //   'vJFbfeP_cpMYsH9l5BVHY23Ss',\n  //   '29~LIlSjDYFr5OrhU3f',\n  //   'UQc8Tp1d9elWAh7KDIMkjz2moFs'\n  // ]\n```\n\u003cbr /\u003e\n\n### `bytes` Method  \n\nCreates an API key/access token using random bytes.  \n\n| Name     | Default Value |  Description                                                      |\n| ---------| ------------- | ----------------------------------------------------------------- | \n| `method` | `bytes`       | To use the `bytes` generation method                              |\n| `min`    | `16`          | The minimum length of the API key (ignored if `length` is given)  |\n| `max`    | `32`          | The maximum length of the API key (ignored if `length` is given)  |\n| `length` | `undefined`   | The length of the API key                                         |\n| `prefix` | `undefined`   | A string prefix for the API key, followed by a period (`.`)       |  \n| `batch`  | `undefined`   | The number of API keys to generate                                |\n\u003cbr /\u003e\n\nExamples:  \n\n```javascript\nimport generateApiKey from 'generate-api-key';\n\n// Provide the generation method.\ngenerateApiKey({ method: 'bytes' }); // ⇨ '6f31bfc3717d63e7bd21'\n\n// Create an API key with a certain length.\ngenerateApiKey({ method: 'bytes', length: 12 }); // ⇨ '47a8dcbc79f6'\n\n// Create an API key with a length between a certain range.\ngenerateApiKey({ method: 'bytes', min: 12, max: 25 }); // ⇨ 'fae27c801b5092bc'\n\n// Create an API key with a prefix.\ngenerateApiKey({ method: 'bytes', prefix: 'test_app' }); // ⇨ 'test_app.8daaa6b26c79030db1a1448261'\n\n// Create a batch (certain amount) of API keys.\ngenerateApiKey({ method: 'bytes', batch: 5 }); // ⇨ \n  // [\n  //   '0d5a87f007aae092a6',\n  //   '96a62b4438d82645506b',\n  //   'abd4e4199311fb1e2a818a4a',\n  //   'ddbb04b2375ba050cb506e89df',\n  //   '2ee3db86329865d8'\n  // ]\n```\n\u003cbr /\u003e\n\n### `base32` Method  \n\nCreates an API key/access token using a random UUID and converting it into a [Douglas Crockford Base32](https://en.wikipedia.org/wiki/Base32#Crockford's_Base32) encoded string.  \n\n| Name     | Default Value |  Description                                                      |\n| ---------| ------------- | ----------------------------------------------------------------- | \n| `method` | `base32`      | To use the `base32` generation method                             |\n| `dashes` | `true`        | Add dashes (`-`) to the API key or not                            |\n| `prefix` | `undefined`   | A string prefix for the API key, followed by a period (`.`)       |  \n| `batch`  | `undefined`   | The number of API keys to generate                                |\n\u003cbr /\u003e\n\nExamples:  \n\n```javascript\nimport generateApiKey from 'generate-api-key';\n\n// Provide the generation method.\ngenerateApiKey({ method: 'base32' }); // ⇨ '2NOLH5I-43EEK7A-R6YRK3I-BRCIQNQ'\n\n// Create an API key without the dashes.\ngenerateApiKey({ method: 'base32', dashes: false }); // ⇨ 'MPZ6G2QTAXUGBYTCLKEGYZG6UXAY'\n\n// Create an API key with a prefix.\ngenerateApiKey({ method: 'base32', prefix: 'test_app' }); // ⇨ 'test_app.PMKC6DQ-2LZECPY-RVRF6YI-INGIEMQ'\n\n// Create a batch (certain amount) of API keys.\ngenerateApiKey({ method: 'base32', batch: 5 }); // ⇨ \n  // [\n  //   'EGIKGTI-PTLUW7I-QBLUBNA-FH4P2EI',\n  //   'MOHE7XQ-DRKEQ4I-RO6JVDQ-GIJRLKA',\n  //   'UDVJBFI-JFTUWYA-XE4YCBA-FUYXQBY',\n  //   '22EFRQI-2AWULDI-QE2PUCY-MXG36RI',\n  //   '42CXULQ-SDPU6ZA-RQ6QPBQ-4BMZCOA'\n  // ]\n```\n\u003cbr /\u003e\n\n### `base62` Method  \n\nCreates an API key using Base62 encoding.  \n\n| Name     | Default Value |  Description                                                      |\n| ---------| ------------- | ----------------------------------------------------------------- | \n| `method` | `base62`      | To use the `base62` generation method                             |\n| `prefix` | `undefined`   | A string prefix for the API key, followed by a period (`.`)       |  \n| `batch`  | `undefined`   | The number of API keys to generate                                |\n\u003cbr /\u003e\n\nExamples:  \n\n```javascript\nimport generateApiKey from 'generate-api-key';\n\n// Provide the generation method.\ngenerateApiKey({ method: 'base62' }); // ⇨ '2AEmXhHtNJkIAqL1S3So6G'\n\n// Create an API key with a prefix.\ngenerateApiKey({ method: 'base62', prefix: 'test_app' }); // ⇨ 'test_app.1aHVuwNwzITpzJWl40OPvx'\n\n// Create a batch (certain amount) of API keys.\ngenerateApiKey({ method: 'base62', batch: 5 }); // ⇨ \n  // [\n  //   '1kCFJsoNBpnX65k84Y8clk',\n  //   '5rAZLlhh9pQuV6weF2RxGg',\n  //   '4kBS1ciuLadz340qeXQGl6',\n  //   '3DXocnIyt236ymZAHWYOs6',\n  //   'oPv07XcusmLM1UsHniMZy'\n  // ]\n```\n\u003cbr /\u003e\n\n### `uuidv4` Method  \n\nCreates an API key/access token using random UUID Version 4 generation.  \n\n| Name     | Default Value |  Description                                                      |\n| ---------| ------------- | ----------------------------------------------------------------- | \n| `method` | `uuidv4`      | To use the `uuidv4` generation method                             |\n| `dashes` | `true`        | Add dashes (`-`) to the API key or not                            |\n| `prefix` | `undefined`   | A string prefix for the API key, followed by a period (`.`)       |  \n| `batch`  | `undefined`   | The number of API keys to generate                                |\n\u003cbr /\u003e\n\nExamples:  \n\n```javascript\nimport generateApiKey from 'generate-api-key';\n\n// Provide the generation method.\ngenerateApiKey({ method: 'uuidv4' }); // ⇨ 'c40c974f-307e-490e-8d4e-0c8f31f21df3'\n\n// Create an API key without the dashes.\ngenerateApiKey({ method: 'uuidv4', dashes: false }); // ⇨ 'be1748535cad474191b34a4aa4299a9d'\n\n// Create an API key with a prefix.\ngenerateApiKey({ method: 'uuidv4', prefix: 'test_app' }); // ⇨ 'test_app.1dfdf2c1-7365-4625-b7d9-d9db5210f18d'\n\n// Create a batch (certain amount) of API keys.\ngenerateApiKey({ method: 'uuidv4', batch: 5 }); // ⇨ \n  // [\n  //   'd822611a-0600-4edf-9697-21ba5c79e6d7',\n  //   '596ac0ae-c4a0-4803-b796-8f239c8431ba',\n  //   '0f3819f3-b417-4c4c-b674-853473800265',\n  //   '882332bf-4215-41ce-a573-a3c8f5d47a24',\n  //   'f269c1f6-77d5-464d-8229-769c0b3a21f7'\n  // ]\n```\n\u003cbr /\u003e\n\n### `uuidv5` Method  \n\nCreates an API key/access token using random UUID Version 5 generation.  \n\n| Name         | Default Value |  Description                                                      |\n| -------------| ------------- | ----------------------------------------------------------------- | \n| `method`     | `uuidv5`      | To use the `uuidv5` generation method                             |\n| `name`       | `undefined`   | A unique name to use for the generation                           |\n| `namespace`  | `undefined`   | The UUID to use for the generation (ignored if `batch` is given)  |\n| `dashes`     | `true`        | Add dashes (`-`) to the API key or not                            |\n| `prefix`     | `undefined`   | A string prefix for the API key, followed by a period (`.`)       |  \n| `batch`      | `undefined`   | The number of API keys to generate                                |\n\u003cbr /\u003e\n\nExamples:  \n\n```javascript\nimport generateApiKey from 'generate-api-key';\n\n// Provide the generation method with the name and namespace.\ngenerateApiKey({\n  method: 'uuidv5',\n  name: 'production app',\n  namespace: '1dfdf2c1-7365-4625-b7d9-d9db5210f18d'\n}); // ⇨ 'd683c168-377f-528d-8d9a-b7f1551ecb44'\n\n// Create an API key without the dashes.\ngenerateApiKey({\n  method: 'uuidv5',\n  name: 'production app',\n  namespace: '596ac0ae-c4a0-4803-b796-8f239c8431ba',\n  dashes: false\n}); // ⇨ 'b1bc2cda0c1f5eb594495088a37339b8'\n\n// Create an API key with a prefix.\ngenerateApiKey({\n  method: 'uuidv5',\n  name: 'production app',\n  namespace: '0f3819f3-b417-4c4c-b674-853473800265',\n  prefix: 'prod_app'\n}); // ⇨ 'prod_app.3f7e5d98-3aa9-5dcb-82e3-10d9a2fc412a'\n\n/* \n * Create a batch (certain amount) of API keys. When creating a\n * batch of 'uuidv5' API keys, a namespace is generated for each\n * key. The provided namespace is not used.\n */\ngenerateApiKey({\n  method: 'uuidv5',\n  name: 'production app',\n  batch: 5\n}); // ⇨ \n  // [\n  //   '1e37088a-4dbf-5126-a255-071095e3a53b',\n  //   '23c3cfce-2cf1-5b80-856c-12cf6b5f4e88',\n  //   '46ec193e-237e-517b-a02e-7679510215d8',\n  //   'cf646907-7bc7-5953-bf76-b7527d70b234',\n  //   '5ead0c39-0eac-57e8-831d-250932aeb1e0'\n  // ]\n```\n\u003cbr /\u003e\n\n## Security  \n\nWhen generating and storing API keys and access tokens please be mindful of secure \ndatabase storage best practices. The reason API keys or access tokens are stored is to \nconfirm the key/token that is provided (ex. HTTP request) is valid and issued by your \norganization or application (the same as a password). Just like a password, an API key \nor access token can provide direct access to data or services that require authentication.\n\nTo authenticate an API key or access token, it is not necessary to know the raw \nkey/token value, the key/token just needs to validated to be correct. API keys and \naccess tokens should not be stored in plain text in your database, they should be \nstored as a hashed value. Consider using database storage concepts such as salting \nor peppering during the hashing process. \n\nLastly, if you suspect the API credentials for your organization or application have \nbeen compromised, please revoke the keys and regenerate new keys.  \n\u003cbr /\u003e\n\n## Change Log\n\nThe [CHANGELOG](./CHANGELOG.md) contains descriptions of notable changes.  \n\u003cbr /\u003e\n\n## License\n\nThis software is licensed under the [Apache 2 license](./LICENSE).  \n\n[npm-url]: https://www.npmjs.com/package/generate-api-key\n[version-image]: https://img.shields.io/github/package-json/v/pauldenver/generate-api-key/main?label=version\u0026style=flat-square\n[tests-url]: https://github.com/pauldenver/generate-api-key/actions/workflows/test.yml\n[tests-image]: https://github.com/pauldenver/generate-api-key/actions/workflows/test.yml/badge.svg?branch=main\n[coverage-url]: https://github.com/pauldenver/generate-api-key/actions/workflows/coverage.yml\n[coverage-image]: https://github.com/pauldenver/generate-api-key/actions/workflows/coverage.yml/badge.svg?branch=main\n[travis-url]: https://travis-ci.com/pauldenver/generate-api-key\n[travis-image]: https://travis-ci.com/pauldenver/generate-api-key.svg?branch=main\n[coveralls-url]: https://coveralls.io/github/pauldenver/generate-api-key\n[coveralls-image]: https://coveralls.io/repos/github/pauldenver/generate-api-key/badge.svg?branch=main\n[codefactor-url]: https://www.codefactor.io/repository/github/pauldenver/generate-api-key/overview/main\n[codefactor-image]: https://www.codefactor.io/repository/github/pauldenver/generate-api-key/badge/main","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpauldenver%2Fgenerate-api-key","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpauldenver%2Fgenerate-api-key","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpauldenver%2Fgenerate-api-key/lists"}