API key generator

Generate random API keys, secret keys and tokens in hex, base64 or as a UUID. Pick the size, add a prefix if you want one, and copy. They're made in your browser.

Each key has 256 bits of randomness and is 64 characters long.

Keys are generated in your browser with the Web Crypto API and are never sent to a server. Generate a new one before you use it: the keys shown when the page loads came from our server.

How to use the generator

Pick a format and a size, and the keys update. The default, 256 bits in hexadecimal, is a safe choice for almost anything: an API key, a webhook signing secret, a session secret, an encryption key.

  • Format. How the random bytes are written out. The table below shows what each looks like
  • Size. How much randomness is in the key. 128 bits is already beyond guessing. 256 bits is the common default
  • Prefix. Optional text at the start, like sk_live_. It adds no security. It tells you what a key is when you find one in a log
  • How many. Up to 500 at once, one per line

Click "Generate new keys" before you copy one. The keys on the page when it first loads were made on our server so the page isn't empty, and a key that has travelled over a network shouldn't be used as a secret. Every batch after that is made on your computer.

Formats and what they look like

For a 256-bit key:

Format Length Characters Good for
Hexadecimal 64 0-9, a-f Anything. The safest to paste into config files and URLs
Base64url 43 Letters, digits, - and _ Shorter keys that are still safe in URLs and headers
Base62 43 Letters and digits only Keys people double-click to select, since there's no punctuation
Base64 44 Letters, digits, +, /, = Config values that expect standard base64
UUID version 4 36 Hex in five groups Identifiers. It has 122 random bits

Base64url is the "URL and filename safe" alphabet from RFC 4648. It swaps + and / for - and _, so the key doesn't need escaping in a URL.

A UUID is a fine API key if your system already expects one, but it's an identifier format first. The UUID generator makes version 4 and version 7 values in bulk. RFC 9562 gives a version 4 UUID 122 random bits, with the other 6 fixed to mark the version and variant.

How long should an API key be?

Long enough that guessing is hopeless, which takes less than people think.

Size Possible keys Verdict
64 bits About 18 quintillion The floor for a session ID, according to OWASP. Too small for a long-lived key
128 bits About 3.4 × 1038 Can't be guessed. Fine for API keys
256 bits About 1.2 × 1077 The usual default. Required for some uses, see below
512 bits About 1.3 × 10154 Only when an algorithm asks for it

OWASP's session management guidance asks for at least 64 bits of entropy in a session identifier. An API key lives much longer than a session, so 128 bits or more is the sensible minimum.

What matters is that the bits are random. A long key built from a timestamp, a user ID or a weak random function can be predicted. This page uses the browser's cryptographically secure random generator, crypto.getRandomValues.

Secrets for JWTs, webhooks and encryption

Some uses come with a required size.

  • JWT signing with HS256. The standard says the key must be at least as long as the hash output: 256 bits for HS256, 384 for HS384, 512 for HS512. That's from RFC 7518. Pick 256, 384 or 512 bits here to match
  • Webhook signing secrets. These are HMAC keys too. 256 bits is the normal choice
  • AES keys. AES takes a key of exactly 128, 192 or 256 bits. Generate the size your library expects, in the encoding it expects, which is usually hex or base64
  • Session and cookie secrets. Frameworks sign cookies with these. 256 bits or more

If a framework's documentation says "generate a random 32-byte secret", that's 256 bits: choose it here in hex or base64.

Generate an API key from the command line

For scripts and servers, generate the key where it will be used.

OpenSSL (macOS, Linux, Git Bash on Windows)

openssl rand -hex 32
openssl rand -base64 32

Python

python3 -c "import secrets; print(secrets.token_hex(32))"
python3 -c "import secrets; print(secrets.token_urlsafe(32))"

Node.js

node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

Ruby

ruby -rsecurerandom -e "puts SecureRandom.hex(32)"

A UUID

uuidgen

Each of these uses the operating system's cryptographic random source. Avoid anything based on Math.random(), rand() or the current time: those are predictable.

Designing API keys for your own product

If you're adding API keys to something you're building, a few choices save trouble later.

  • Add a prefix. Stripe's keys start with sk_live_, sk_test_, pk_live_ and rk_live_, so anyone can tell a secret key from a publishable one and live from test. A prefix also lets code scanners spot a key that was committed by mistake
  • Store a hash, not the key. Show the key once when it's created, then keep only a SHA-256 of it. If your database leaks, the keys don't. A fast hash is fine here because the key is already random, unlike a password
  • Keep a few visible characters. Store the last four separately so users can tell their keys apart in a list
  • Compare in constant time. Use your language's constant-time comparison when checking a key, so response timing doesn't reveal how much of it matched
  • Let users rotate. Allow two active keys at once, so a key can be replaced without downtime
  • Scope and expire. A key that can only read, or only reach one project, does less damage when it leaks

You can check the hash you're storing with the checksum calculator: paste the key as text and compare the SHA-256.

API keys and license keys aren't the same thing

They look alike, and they do different jobs.

An API key identifies a program calling your server. It lives in a config file or an environment variable, and its owner keeps it secret.

A license key proves that a person paid for software that runs on their own computer. The customer types it in, so it should be short and easy to read. It can't be kept secret from the person using it, and it has to be tied to a purchase, limited to a number of devices, and revocable after a refund.

That's why a license key needs a server behind it, and a random string isn't enough. If that's what you need:

LicenseSeat does this for desktop apps, games and plugins sold from your own website.

Common questions about API keys

Is it safe to generate an API key online?

It depends on where the key is made. On this page, clicking "Generate new keys" creates them in your browser with the Web Crypto API, and they're never sent anywhere. Don't use the keys shown when the page first loads, since those came from our server. For production secrets, the command-line methods above are the most cautious option.

How long should an API key be?

At least 128 bits of randomness, which is 32 hexadecimal characters. 256 bits, or 64 hexadecimal characters, is the common default and costs nothing extra.

What's the difference between an API key and a secret key?

Usage varies. Often the "API key" identifies who's calling and may be visible, while the "secret key" proves it and must stay private. Many services use one random value for both jobs. Either way, both are random strings, and this generator makes either.

How do I generate a JWT secret?

For HS256, choose 256 bits here and copy the result, or run openssl rand -hex 32. The JWT standard requires the key to be at least as long as the hash output, so use 384 bits for HS384 and 512 for HS512.

Should I use a UUID as an API key?

You can. A version 4 UUID has 122 random bits, which is enough, as long as it comes from a cryptographic random source. A plain 256-bit key is stronger and just as easy to handle.

Hex or base64?

Hex is longer and never causes trouble: no characters that need escaping, no case sensitivity problems. Base64url is about a third shorter for the same strength. Avoid standard base64 in URLs, because + and / need escaping there.

How should I store API keys?

In your application's database, store a SHA-256 hash of each key, never the key itself. On the client side, keep the key in an environment variable or a secrets manager, and out of source control.

Can two generated keys ever be the same?

Not in practice. With 256 random bits there are about 1.2 × 1077 possible keys. You could generate billions per second for the lifetime of the universe and not expect a repeat.

Does a prefix make a key weaker?

No. The prefix is public information, and the strength comes from the random part after it. This generator adds the prefix on top of the full random key.