ClockTools Blog

Developer Tools

How Long Should an API Key Be?

Use at least 128 bits of unpredictable randomness for an opaque API key; 256 bits is a strong default. Encoding determines how many characters you see.

By , Developer and Publisher | | Reviewed under the ClockTools editorial policy

Secure API key represented by luminous random blocks progressing from 128 to 256 bits
Table of contents

For a new opaque API key, use at least 128 bits of unpredictable randomness; 256 random bits is a strong default when the receiving system accepts it. That does not mean every key must be 256 characters long. The same 256-bit value is 64 hexadecimal characters, 43 unpadded Base64URL characters, 44 padded Base64 characters, or 52 unpadded Base32 characters.

Use the ClockTools API Key Generator to choose the randomness target first, then the format. The tool calculates the required bytes and characters before it generates anything, so you can meet a field limit without mistaking visual length for security.

ClockTools API Key Generator set to a 256-bit Base64URL key with 43 characters and 32 random bytes
ClockTools API Key Generator set to a 256-bit Base64URL key with 43 characters and 32 random bytes

What is the right API key length?

There is no universal character count because a character is not a fixed security unit. A hex character carries 4 bits. A uniformly random Base64URL character carries close to 6 bits, apart from the final partial group. A decimal digit carries about 3.32 bits. The alphabet, generation method, and number of independent choices determine the search space.

For practical planning, start here:

Randomness targetHexBase64URL, no paddingTypical use
128 bits / 16 bytes32 characters22 charactersHigh brute-force resistance when generated uniformly and protected well
256 bits / 32 bytes64 characters43 charactersComfortable default for new opaque API keys and tokens

The Python secrets documentation describes 32 bytes, or 256 bits, as sufficient for the typical use cases of that module, while warning that adequate randomness changes with computing capability and threat. That makes 256 bits a sensible default, not a magic compliance rule.

The service that accepts the credential still controls the real format. If an existing API specifies 32 hex characters, a fixed prefix, or a provider-issued token, follow that contract. Do not silently lengthen, truncate, or re-encode a provider key.

Why do entropy and character count differ?

Entropy answers a better question than appearance: how many equally likely secrets could the generator have produced? A uniformly random 128-bit value has 2^128 possibilities. A 256-bit value has 2^256 possibilities. Adding more printed characters helps only if those characters add independent, unpredictable choices.

RFC 4086 illustrates why the distinction matters. A long output generated from a small or predictable seed can contain far less real information than its width suggests. For example, a 128-bit-looking value produced from only an 8-bit seed still exposes only 256 seed possibilities. Length cannot rescue weak randomness.

This is also why Math.random(), timestamps, sequential IDs, usernames, or concatenated device data are unsuitable foundations for API secrets. In a browser, MDN documents crypto.getRandomValues() as the method for cryptographically strong random values. ClockTools requires that API and does not fall back to Math.random().

For character alphabets, the theoretical calculation is:

entropy bits = character count × log2(number of distinct characters)

A 62-character letters-and-digits alphabet needs 22 independent characters to cross 128 bits and 43 to cross 256 bits. Digits alone need 39 and 78 characters respectively. A narrow alphabet is not automatically weak; it simply needs more characters to reach the same target.

How long is a 256-bit API key in each encoding?

Encoding changes representation, not the underlying random bytes. The live ClockTools tool and its regression tests use 32 random bytes for a 256-bit target. Those bytes expand as follows:

EncodingCharacter set and paddingCharacters from 32 bytes
Hexadecimal0–9, a–f64
Base64URLURL-safe alphabet, padding removed43
Base64Standard alphabet with = padding44
Base32A–Z, 2–7, padding removed52
The same 256 random bits shown as 64 hex, 43 Base64URL, 44 Base64, and 52 Base32 characters
The same 256 random bits shown as 64 hex, 43 Base64URL, 44 Base64, and 52 Base32 characters

The counts follow the encoding rules in RFC 4648. Hex is easiest to inspect and widely accepted, but it needs two characters for every byte. Base64URL is more compact and avoids the + and / characters that can be awkward in URLs and filenames. Ordinary Base64 can include those characters and uses padding. Base32 is case-insensitive in many workflows but is longer.

Choose the format that the receiving application can parse reliably. Do not remove padding unless the protocol allows it, and do not assume a URL-safe alphabet means the value is safe to expose in a URL. API secrets can leak through browser history, referrer data, reverse-proxy logs, and analytics.

When is 128 bits enough?

A uniformly random 128-bit opaque secret presents an enormous online guessing space. In many systems, rate limits, monitoring, credential isolation, and the difficulty of testing guesses make exhaustive search impractical. Increasing the random value to 256 bits creates a larger margin and costs little for most new server-side designs.

The choice should still follow a threat model and system contract. Consider 256 bits when you control the credential format, storage cost is negligible, and the key protects valuable or long-lived access. A well-generated 128-bit key may be appropriate when a protocol fixes the size, the credential is short-lived, or a legacy field cannot hold more.

More bits do not compensate for an exposed secret. A 256-bit key committed to a public repository can be copied immediately; nobody needs to brute-force it. If a key may have leaked, revoke or rotate it instead of merely replacing it with a longer string.

Does a prefix make an API key stronger?

No, not when the prefix is fixed or predictable. Text such as live_, test_, or sk_ can help people and secret scanners identify a credential, but it does not add entropy. A 256-bit random value remains 256 random bits whether its printed form starts with five known characters or none.

Prefixes can still be useful. They can distinguish production from test material, identify a credential family, and reduce the chance that a key is pasted into the wrong system. Keep that purpose separate from authentication strength.

The same warning applies to suffixes and public key IDs. A public identifier can locate the right database record without revealing the secret, but the ID should not be accepted as proof of authorization. ClockTools reports fixed affixes and public IDs outside the random-bit total.

What else matters besides length?

Length is only the creation step. OWASP's Secrets Management guidance treats a secret as a lifecycle: creation, rotation, revocation, and expiration. Google Cloud's API key guidance also emphasizes restrictions, isolation, monitoring, avoiding client-code exposure, and rotation.

API key decision checklist covering randomness, representation, and lifecycle controls
API key decision checklist covering randomness, representation, and lifecycle controls

Before issuing a production key, check all three layers:

  1. Randomness: generate at least 128 unpredictable bits with a cryptographically secure source; prefer 256 bits for a new opaque key when compatible.
  2. Representation: choose an encoding the receiver accepts; count only the random portion, not a fixed prefix or separator.
  3. Lifecycle: scope permissions, transmit over HTTPS, store in a secrets manager, prevent plaintext logs, monitor use, define expiration where appropriate, and support fast revocation.

API keys are bearer credentials in many designs: whoever possesses the value can use its authority. They are usually better for identifying a calling application or project than for authenticating a human user. For sensitive user actions, combine or replace them with an authentication and authorization method designed for that purpose.

How can you generate the key safely?

For a quick browser-local value, open the API Key Generator, keep the randomness target at 256 bits, and select the required format. The default Base64URL setting produces 32 random bytes represented by 43 characters. Generation and export preparation happen locally in the browser, and the page intentionally excludes secret values from shared settings.

For production infrastructure, generate inside the trusted environment that will store the secret whenever possible. Node.js crypto.randomBytes, Python secrets, or OpenSSL can avoid a clipboard transfer. The ClockTools page displays commands for those environments without embedding a generated key.

After generation, register the value with the application that will verify it. The random string does not become a working credential merely because it looks like an API key. Permissions, expiry, revocation, and request verification belong to the receiving system. The ClockTools API-key methodology explains the browser source, rejection sampling, tested lengths, and limitations; the GUID Generator is available for identifiers that are not shared secrets.

Frequently Asked Questions

Is a 32-character API key secure?

It depends on the alphabet and generation method. Thirty-two random hexadecimal characters contain 128 bits, while 32 uniformly random letters and digits contain about 191 bits. A predictable 32-character string can still be weak.

How many characters is a 256-bit API key?

It is 64 hexadecimal characters, 43 unpadded Base64URL characters, 44 padded Base64 characters, or 52 unpadded Base32 characters. These are different representations of the same 32 random bytes.

Is 128 bits enough for an API key?

A uniformly random 128-bit opaque secret provides a very large guessing space and can be appropriate when the protocol or field size requires it. For new designs, 256 bits is a comfortable default when compatibility and storage allow.

Does a longer API key always make an API safer?

No. Length cannot repair predictable generation, excessive permissions, insecure storage, client-side exposure, plaintext logs, or missing revocation. Those controls must be designed separately.

Should an API key include a prefix?

A fixed prefix can identify the key type or environment and help secret scanners, but it adds no entropy. Count only the unpredictable portion when assessing strength.

Is Base64URL stronger than hexadecimal?

No. With the same random bytes, both carry the same entropy. Base64URL is simply more compact, while hexadecimal is often easier to inspect and widely supported.

About The Author

Vigneshwaran Vijayakumar

Founder, Developer and Publisher of ClockTools | Digital Marketing Manager | India

Vigneshwaran is an engineer with decades of technical experience, including professional work as a Digital Marketing Manager in Dubai. His work connects data analysis, search engine optimization, conversion-rate optimization, content systems, visual production, and applied AI and machine learning. At ClockTools, he turns that multidisciplinary experience into focused browser utilities and practical, source-aware guides.

LinkedIn profile