API Key Length: How Long Should Your API Key Be?

Published March 9, 2026 · 8 min read

The length of an API key is not arbitrary. It directly determines the key's entropy, which defines how resistant it is to brute-force attacks. But "length" alone is misleading — the encoding format matters just as much. A 32-character hex key and a 32-character base64 key have very different security properties.

This guide gives you a definitive answer: how long your API key should be, why, and how to calculate the entropy for any format.

The Short Answer

For most APIs, use 32 bytes of random data (256 bits of entropy), encoded as a 64-character hex string or a 43-character base64url string. This matches AES-256 security strength and is resistant to all known brute-force attacks.

If you want a minimum viable key, 16 bytes (128 bits / 32 hex characters) is the floor. Below 128 bits, your keys may be vulnerable to well-resourced attackers within the key's lifetime.

Key Length vs Entropy

The security of a key is measured in bits of entropy, not in the number of characters. Entropy depends on both the number of characters and the size of the character set (alphabet).

The formula is: entropy = length x log2(alphabet_size)

EncodingAlphabet SizeBits per CharChars for 128-bitChars for 256-bit
Hex (0-9, a-f)1643264
Base646462243
Alphanumeric (a-z, A-Z, 0-9)625.952243
Base62 + symbols946.552039
Lowercase hex (0-9, a-f)1643264

This table reveals a critical insight: a 32-character base64 key has 192 bits of entropy (32 x 6), while a 32-character hex key has only 128 bits (32 x 4). Same length, 50% more security. Encoding matters.

What the Industry Uses

Examining the key lengths of major API providers reveals a strong consensus around 128-256 bits of entropy:

ProviderKey FormatTotal LengthEstimated Entropy
Stripesk_live_ + 24 base62 chars32 chars~143 bits
OpenAIsk-proj- + 48 base62 chars56 chars~285 bits
AWS Access Key20 alphanumeric chars20 chars~119 bits
GitHub PATghp_ + 36 base62 chars40 chars~214 bits
Google Cloud39 alphanumeric chars39 chars~232 bits
Twilio32 hex chars32 chars128 bits

Notice that every provider delivers at least 119 bits of entropy, and most target 128-256 bits. No serious API uses keys shorter than 20 characters.

Minimum Length by Security Requirement

128 bits (minimum for production)

At 128 bits of entropy, an attacker performing one trillion (10^12) guesses per second would need approximately 5.4 x 10^15 years to try every possible key. This exceeds the age of the universe by a factor of about 400,000. For most APIs with rate limiting and monitoring, 128 bits is sufficient.

192 bits (comfortable margin)

192 bits provides a safety margin against future advances in computing. It is the entropy level used by AES-192 and is commonly used for long-lived keys that may be active for years.

256 bits (recommended for production)

256 bits matches AES-256 and is considered secure against quantum computing attacks (Grover's algorithm reduces effective entropy to 128 bits, which remains unbreakable). This is the recommended length for any key that protects sensitive data or financial transactions.

The Prefix Question

Modern APIs include a prefix like sk_live_ or ghp_ before the random portion. The prefix does not contribute to entropy — it is a fixed, predictable string. When calculating your key's security, only count the random characters after the prefix.

// Total length: 40 characters
// Prefix: "sk_live_" (8 chars, 0 bits entropy)
// Random: 32 alphanumeric chars (~190 bits entropy)
// Effective entropy: ~190 bits

sk_live_EXAMPLE_DO_NOT_USE_1a2b...

The prefix is valuable for identification, routing, and secret scanning — but it adds zero security. Design your key length around the random portion, then add the prefix on top.

When Keys Are Too Long

There is such a thing as too long. Keys beyond 256 bits of entropy provide no additional practical security (the heat death of the universe arrives long before a 256-bit key is brute-forced). Extra length just creates usability problems:

The sweet spot is 32-64 characters of random data (128-256 bits), plus an optional prefix of 4-12 characters.

When Keys Are Too Short

Keys under 128 bits of entropy are dangerous. Here is what an attacker can do with modern hardware:

EntropyBrute-force Time (1T guesses/sec)Verdict
64 bits~5 hoursBroken trivially
80 bits~38 yearsRisky for long-lived keys
96 bits~2.5 million yearsBorderline acceptable
128 bits~5.4 x 10^15 yearsSecure
256 bits~3.7 x 10^54 yearsOverkill (in the best way)

A 64-bit key can be cracked in hours with a GPU cluster. An 80-bit key might survive decades but offers no margin for error. Start at 128 bits minimum.

Practical Recommendations

  1. Default to 32 bytes (256 bits) encoded as hex (64 chars) or base64url (43 chars)
  2. Add a prefix of 3-8 characters (e.g., sk_, pk_, api_) to aid identification
  3. Never go below 16 bytes (128 bits) for any key used in production
  4. Use base64url over hex if you want shorter keys with the same entropy
  5. Document your key format in your API docs so users know what to expect
  6. Generate keys server-side with a CSPRNG, never client-side for authentication

For quick key generation with any length, try our free API key generator which lets you set the exact character count and format.

Recommended Resources

To understand why key length maps to security strength, Real-World Cryptography covers entropy, AES key sizes, and brute-force resistance in depth. For securing keys in web applications, The Web Application Hacker's Handbook shows how attackers exploit short keys.

SPUNK LLC Network

API Sites

Key Management

More from SPUNK LLC