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

Published March 9, 2026 · Updated October 23, 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.552040
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_ + base62 body (classic keys: 24 chars)32 chars classic; newer keys are longer~143 bits for a 24-char body
OpenAIsk-proj- + random bodyNot published; developers report 130+ charsWell above 256 bits if the body is random
AWS Secret Access Key40 chars from A-Z, a-z, 0-9, + and /40 charsUp to ~240 bits
GitHub PATghp_ + 30 random base62 chars + 6-char checksum40 chars~178 bits (GitHub's figure)
Google Cloud API keyAIza + 35 chars from A-Z, a-z, 0-9, - and _39 charsUp to ~210 bits
Twilio Auth Token32 hex chars32 chars128 bits

Notice that every secret in this table carries at least 128 bits of entropy, and several go well past it. The AWS access key ID (the 20-character AKIA... value) is an identifier, not the secret; the 40-character secret access key is what protects the account. GitHub publishes its figure in its 2021 post on token formats; the other estimates come from the documented or commonly matched character sets.

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 1.1 x 10^19 years to try every possible key. That is about 780 million times the age of the universe (13.8 billion years). 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~214 daysBroken by a well-funded attacker
80 bits~38,000 yearsRisky for long-lived keys
96 bits~2.5 billion yearsBorderline acceptable
128 bits~1.1 x 10^19 yearsSecure
256 bits~3.7 x 10^57 yearsOverkill (in the best way)

These times are for trying every key at one trillion guesses per second; on average an attacker finds a key in half that time, and more hardware shortens it further. A 64-bit key falls within months. An 80-bit key holds out longer but offers little 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.

Related sites

Key Management

  • — API Security Education
  • keys.makeup — API Key Generator (this site)
  • keys.yachts — Enterprise Key Management
  • keys.surf — Browser Key Manager
  • — Developer Themes
  • — LGBTQ+ Privacy Security
  • — Motorcycle Key Guide