Use a random number generator designed for cryptography. Then base-64 encode the number.

This is a C# example:

var key = new byte[32];
using (var generator = RandomNumberGenerator.Create())
    generator.GetBytes(key);
string apiKey = Convert.ToBase64String(key);
Answer from Edward Brey on Stack Overflow
🌐
StrongDM
strongdm.com › tools › api-key-generator
API Key Generator: Random, Online, Free
3. Copy: Use the “Copy API Keys” button to grab and use them securely. 2. Select Key Length: Pick your desired key strength, such as 256-bit. 3. Choose Character Type: Use letters, numbers, or a mix of both for added randomness. 4. Add a Prefix (Optional): Include a custom prefix to help identify your keys. 5. Set a Separator: Choose how to separate multiple keys—new line, comma, etc. 6. Generate: Click “Generate API Keys” to instantly create your keys.
🌐
CodePen
codepen.io › corenominal › pen › rxOmMJ
API Key Generator
* See: http://stackoverflow.co... /** * Generate new key and insert into input value */ $( '#keygen' ).on('click',function() { $( '#apikey' ).val( generateUUID() ); });...
🌐
Generate Random
generate-random.org › home › api keys
Random API Key Generator | Generate API Keys Online | React, Node.js, ...
20 hours ago - Our API key generator creates cryptographically secure tokens used for authenticating applications and services. Generate random API keys in multiple formats including alphanumeric, hexadecimal, Base64, Base64 URL-safe, UUID v4, and numeric formats. Perfect for REST API authentication, webhook secrets, service-to-service auth, and client application IDs.
Top answer
1 of 12
94

Use a random number generator designed for cryptography. Then base-64 encode the number.

This is a C# example:

var key = new byte[32];
using (var generator = RandomNumberGenerator.Create())
    generator.GetBytes(key);
string apiKey = Convert.ToBase64String(key);
2 of 12
40

API keys need to have the properties that they:

  • uniquely identify an authorized API user -- the "key" part of "API key"
  • authenticate that user -- cannot be guessed/forged
  • can be revoked if a user misbehaves -- typically they key into a database that can have a record deleted.

Typically you will have thousands or millions of API keys not billions, so they do not need to:

  • Reliably store information about the API user because that can be stored in your database.

As such, one way to generate an API key is to take two pieces of information:

  1. a serial number to guarantee uniqueness
  2. enough random bits to pad out the key

and sign them using a private secret.

The counter guarantees that they uniquely identify the user, and the signing prevents forgery. Revocability requires checking that the key is still valid in the database before doing anything that requires API-key authorization.

A good GUID generator is a pretty good approximation of an incremented counter if you need to generate keys from multiple data centers or don't have otherwise a good distributed way to assign serial numbers.


or a hash of a random string

Hashing doesn't prevent forgery. Signing is what guarantees that the key came from you.

🌐
Stripe
docs.stripe.com › keys
API keys | Stripe Documentation
If you’re logged in to Stripe, our documentation populates code examples with your test API keys. Only you can see these values. If you’re not logged in, our code examples include randomly generated API keys that you can replace with your test keys.
🌐
Carbon Design System
carbondesignsystem.com › community › patterns › generate-an-api-key
Generate an API key
Options should be provided in the modal before the user clicks generate. To avoid scrolling in the modal, a multi-step modal can be used, with a progress indicator. Many components can be implemented in the modal content space. Some examples of restricted API key generation.
🌐
GitHub
github.com › tspence › api-key-generator
GitHub - tspence/api-key-generator: A DotNet library for generating and validating API keys
A DotNet library for generating and validating API keys - tspence/api-key-generator
Starred by 10 users
Forked by 4 users
Languages   C#
Find elsewhere
🌐
Nordic APIs
nordicapis.com › 5-random-api-key-generators-and-why-you-might-use-them
5 Random API Key Generators (And Why You Might Use Them) | Nordic APIs |
May 21, 2025 - We compare five free random API key generators to quickly create test keys for validation, mocking, or educational use.
🌐
npm
npmjs.com › package › generate-api-key
generate-api-key - npm
The 'string' method is used by default. generateApiKey(); // ⇨ 'q_EaTiX+xbBXLyO05.+zDXjI+Qi_X0v' // Provide the generation method. generateApiKey({ method: 'string' }); // ⇨ 'Zt1HbMcLKxk6~nnW' // Create an API key with a certain length. generateApiKey({ method: 'string', length: 8 }); // ⇨ 'TNJ6-Lo4' // Create an API key with a length between a certain range. generateApiKey({ method: 'string', min: 10, max: 20 }); // ⇨ 'ENwiOFdP8cWj' // Create an API key with a certain pool of characters. generateApiKey({ method: 'string', pool: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' }); // ⇨ 'QFLSGIDLOUAELQZT
      » npm install generate-api-key
    
Published   Sep 22, 2022
Version   1.0.2
Author   Paul Proctor
🌐
JWT Secrets
jwtsecrets.com › tools › api-key-generator
API Key Generator | Secure API Key Creation Tool
Generate secure API keys with customizable formats, prefixes, and suffixes for your applications and services.
🌐
Random Generator
randomgenerate.io › random api key generator
Random API Key Generator
Generate secure API keys with our Random API Key Generator. Easily create unique API keys and tokens online. Enhance your application's security. Try it now!
🌐
Qlik
qlik.dev › authenticate › api-key › generate-your-first-api-key
Generate your first API key | Qlik Developer Portal
Provide a description and set an expiration time for the API key. Click Generate to create the key.
🌐
Qodex
qodex.ai › all-tools › api-key-generator
Free API Key Generator – Secure Test Tokens | Qodex
Generate secure, random API keys with custom rules. Include uppercase, numbers, or special characters. Perfect for app testing, auth flows, and sandboxes.
🌐
Ultrance
useultrance.com › tools › api-key-generator
API Key Generator - Ultrance
Our free API key generator allows you to create unique API keys for your applications. To generate a new key, simply click the "Generate" button. The tool will create a random 32-character hexadecimal string that you can use to authenticate your requests. The key is generated using a ...
🌐
Password Generator
getmypassword.com › api-key
Random API Key Generator: Secure Your Software Projects!
December 29, 2024 - Create secure random API keys with our generator. It's essential for software development and protecting your projects. Great for kids learning about programming and cybersecurity basics.
🌐
GitHub
github.com › kevinkabatra › PHP-API-Key-Generator
GitHub - kevinkabatra/PHP-API-Key-Generator: Example for an API Key generator written in PHP.
Running example at: http://kevinkabatra.ignorelist.com/examples/api key generator/example_api_key_generator.php
Starred by 11 users
Forked by 9 users
Languages   PHP
🌐
Nettoolkit
nettoolkit.com › test-api-key
Free Test API Key
You need JavaScript enabled to interact with this site
🌐
Google AI
ai.google.dev › gemini api › using gemini api keys
Using Gemini API keys | Google AI for Developers
This page outlines how to create and manage your keys in Google AI Studio as well as how to set up your environment to use them in your code. ... You can create and manage all your Gemini API Keys from the Google AI Studio API Keys page.
🌐
OV DSS
ovdss.com › home › security › api key generator - create secure 🔑 keys (1-5000 keys)
API Key Generator - Create Secure 🔑 Keys (1-5000 Keys)
Generate secure API keys online with customizable length and complexity. Choose from 1-5000 keys and get started instantly with our API Key Generator tool.