Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/api-keys/javascript$ _

Generate API Keys in JavaScript

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: JavaScript

What: Generate cryptographically secure API keys in JavaScript using the Web Crypto API for browsers or Node.js crypto module. Suitable for authentication tokens and service-to-service communication.

Try it: Use our interactive Api-keys generator or integrate this code into your JavaScript application.

Generate cryptographically secure API keys in JavaScript using the Web Crypto API for browsers or Node.js crypto module. Suitable for authentication tokens and service-to-service communication. Looking for other languages? Check our code examples in PHP , Python , Java , C# , C++ , Ruby and Go or use our interactive web generator.

JavaScript Code Example

// Node.js implementation
const crypto = require('crypto');

function generateApiKey(length = 32) {
    return crypto.randomBytes(length).toString('hex');
}

// Generate 256-bit (32 byte) API key
const apiKey = generateApiKey(32);
console.log(apiKey);
// Output: "a7f3e9c2d8b4f1a6e5c9d7b3f8a2e6c4d9b5f7a3e8c6d2b9f4a7e1c5d8b3f6a9"

// Base64 URL-safe encoding
function generateApiKeyBase64(length = 32) {
    return crypto.randomBytes(length)
        .toString('base64')
        .replace(/\+/g, '-')
        .replace(/\//g, '_')
        .replace(/=/g, '');
}

const apiKeyBase64 = generateApiKeyBase64(32);
console.log(apiKeyBase64);
// Output: "p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo"

// Browser implementation (Web Crypto API)
function generateApiKeyBrowser(length = 32) {
    const array = new Uint8Array(length);
    crypto.getRandomValues(array);
    return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
}

[EXPLANATION]

Node.js uses the built-in crypto.randomBytes() method which provides cryptographically secure random data. For browsers, crypto.getRandomValues() fills a typed array with random values. Both methods are suitable for generating API keys with sufficient entropy for production use.

Expected Output

Hexadecimal: a7f3e9c2d8b4f1a6e5c9d7b3f8a2e6c4d9b5f7a3e8c6d2b9f4a7e1c5d8b3f6a9
Base64 URL-safe: p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo
Browser: c4e8f2a9d6b7e3f1a8c5d9b2e6f4a7c1d8b3e9f6a2c7d5b1e4f8a3c9d6b7e2f5

Common Use Cases

  • Express.js API authentication
  • Next.js API routes
  • Serverless function authentication
  • Browser extensions with backend communication
  • Electron app API keys

Important Notes

  • crypto.randomBytes() available in Node.js by default
  • crypto.getRandomValues() available in all modern browsers
  • Never use Math.random() for API keys
  • Store API key hash on server, not plaintext
  • Consider adding timestamp/version prefix for key rotation

Try Our Interactive Generator

Don't want to write code? Use our free web-based Api-keys generator with instant results.

TRY API-KEYS GENERATOR →