{"id":13217,"library":"generate-api-key","title":"API Key Generator","description":"The `generate-api-key` library provides a flexible solution for creating random API keys or access tokens within Node.js backend services. It currently stands at version 1.0.2 and appears to have a moderate release cadence, with recent updates addressing bug fixes and a significant refactor to TypeScript in version 1.0.1. Key differentiators include support for various generation methods such as `string`, `bytes`, `base32`, `base62`, `uuidv4`, and `uuidv5`, allowing developers to choose the most suitable method for their security and compatibility requirements. The library is designed to be straightforward to install and integrate, enabling quick deployment of API key generation capabilities. It also offers customizable options for key length, character pools, and prefixes, providing granular control over the generated keys.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/pauldenver/generate-api-key","tags":["javascript","REST","RESTful","API","access","token","key","typescript"],"install":[{"cmd":"npm install generate-api-key","lang":"bash","label":"npm"},{"cmd":"yarn add generate-api-key","lang":"bash","label":"yarn"},{"cmd":"pnpm add generate-api-key","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"While a default import `import generateApiKey from 'generate-api-key';` is shown in the README, the library primarily exposes a named export for its main function in TypeScript/ESM contexts, which is the safer and more explicit pattern. Both work for the main function due to interoperability.","wrong":"import generateApiKey from 'generate-api-key';","symbol":"generateApiKey","correct":"import { generateApiKey } from 'generate-api-key';"},{"note":"This is the documented default import pattern for ES Modules, often used for simplicity. For CommonJS, `require('generate-api-key').default` is the equivalent, though `require('generate-api-key')` often returns the function directly due to dual-package setup.","wrong":"const generateApiKey = require('generate-api-key');","symbol":"generateApiKey (default export)","correct":"import generateApiKey from 'generate-api-key';"},{"note":"For CommonJS, `require('generate-api-key')` in Node.js >=14 often returns an object where `generateApiKey` is a property. Using destructuring `const { generateApiKey } = require('generate-api-key');` is the explicit way to get the named export. `require('generate-api-key').default` can also retrieve the primary function.","wrong":"const generateApiKey = require('generate-api-key');","symbol":"generateApiKey (CommonJS)","correct":"const { generateApiKey } = require('generate-api-key');"}],"quickstart":{"code":"import generateApiKey from 'generate-api-key';\n\nasync function generateKeys() {\n  // Generate a default API key (string method, random length between 16 and 32)\n  const defaultKey = generateApiKey();\n  console.log('Default API Key:', defaultKey);\n\n  // Generate an API key with a specific length\n  const fixedLengthKey = generateApiKey({ length: 24 });\n  console.log('Fixed Length API Key:', fixedLengthKey);\n\n  // Generate a Base62 encoded API key with a prefix\n  const base62Key = generateApiKey({\n    method: 'base62',\n    prefix: 'org_abc',\n    length: 32 // Length applies to the UUID before encoding\n  });\n  console.log('Base62 API Key with Prefix:', base62Key);\n\n  // Generate a batch of UUIDv4 keys\n  const uuidv4Keys = generateApiKey({\n    method: 'uuidv4',\n    batch: 3\n  });\n  console.log('Batch of UUIDv4 Keys:', uuidv4Keys);\n}\n\ngenerateKeys().catch(console.error);","lang":"typescript","description":"This quickstart demonstrates how to generate API keys using the default method, specify key length, use different generation methods like Base62 with prefixes, and generate multiple keys in a batch."},"warnings":[{"fix":"Ensure your Node.js environment is version 14 or higher. Update Node.js if necessary, or revert to an older version of `generate-api-key` if sticking to Node.js <14 is unavoidable (not recommended).","message":"Version 1.0.1 introduced a breaking change by updating the minimum supported Node.js engine version to 14 or greater.","severity":"breaking","affected_versions":">=1.0.1"},{"fix":"Consult the documentation for each specific generation method to understand how the `length` or equivalent options are applied. For `base32` and `base62`, the length typically applies to the source UUID or bytes before encoding.","message":"When using methods like `base32`, `base62`, `uuidv4`, or `uuidv5`, the `length` option might behave differently than expected compared to the `string` method. For UUID-based methods, the length refers to the underlying UUID generation or is inherent to the UUID format, not the final encoded string length directly.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"When you want a specific length for a `string` method API key, use only the `length` option. If you need a variable length within a range, use `min` and `max` and omit `length`.","message":"The `string` method's `min` and `max` length options are ignored if the `length` option is explicitly provided. This can lead to unexpected key lengths if both are specified.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Upgrade your Node.js environment to version 14 or higher using a tool like `nvm` or `fnm` (`nvm install 14 && nvm use 14`).","cause":"Attempting to run `generate-api-key` on an older Node.js version (e.g., Node.js 12 or 10).","error":"Error: generate-api-key requires Node.js version 14 or greater."},{"fix":"For ES Modules: `import { generateApiKey } from 'generate-api-key';` or `import generateApiKey from 'generate-api-key';`. For CommonJS: `const { generateApiKey } = require('generate-api-key');` or `const generateApiKey = require('generate-api-key').default;`.","cause":"Incorrect import statement or module resolution issue, especially when mixing CommonJS and ES Modules, or using a wrong path.","error":"TypeError: generateApiKey is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}