Registry / auth-security / generate-api-key

generate-api-key

JSON →
library1.0.2jsnpmunverified

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.

npm install generate-api-key
INSTALL
IMPORT
SIG · GENERATE-API-KEY
G
generate-api-key
auth-securityjavascriptv1.0.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

generateApiKey
✓ import { generateApiKey } from 'generate-api-key';
✗ import generateApiKey from 'generate-api-key';
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.
generateApiKey (default export)
✓ import generateApiKey from 'generate-api-key';
✗ const generateApiKey = require('generate-api-key');
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.
generateApiKey (CommonJS)
✓ const { generateApiKey } = require('generate-api-key');
✗ const generateApiKey = require('generate-api-key');
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.

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.

import generateApiKey from 'generate-api-key'; async function generateKeys() { // Generate a default API key (string method, random length between 16 and 32) const defaultKey = generateApiKey(); console.log('Default API Key:', defaultKey); // Generate an API key with a specific length const fixedLengthKey = generateApiKey({ length: 24 }); console.log('Fixed Length API Key:', fixedLengthKey); // Generate a Base62 encoded API key with a prefix const base62Key = generateApiKey({ method: 'base62', prefix: 'org_abc', length: 32 // Length applies to the UUID before encoding }); console.log('Base62 API Key with Prefix:', base62Key); // Generate a batch of UUIDv4 keys const uuidv4Keys = generateApiKey({ method: 'uuidv4', batch: 3 }); console.log('Batch of UUIDv4 Keys:', uuidv4Keys); } generateKeys().catch(console.error);
Debug
Known issues
breakingVersion 1.0.1 introduced a breaking change by updating the minimum supported Node.js engine version to 14 or greater.
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).
affects: >=1.0.1
gotchaWhen 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.
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.
affects: >=1.0.0
gotchaThe `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.
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`.
affects: >=1.0.0
Errors
Common errors & fixes
Error: generate-api-key requires Node.js version 14 or greater.
Attempting to run `generate-api-key` on an older Node.js version (e.g., Node.js 12 or 10).
fix
Upgrade your Node.js environment to version 14 or higher using a tool like `nvm` or `fnm` (`nvm install 14 && nvm use 14`).
TypeError: generateApiKey is not a function
Incorrect import statement or module resolution issue, especially when mixing CommonJS and ES Modules, or using a wrong path.
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;`.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
16
Resources
generate-api-key — npm install generate-api-key · libregistry