How-To Guides · Intermediate

How to Generate Secure Random Tokens for Apps & APIs

Learn how cryptographic randomness, entropy, token length, storage and expiry work. Use the Web Crypto API appropriately for API keys and temporary secrets.

7 min read By Editagram Team Updated
tokenssecurityapi keysrandom

What security tokens are used for

Applications use random tokens as API credentials, session identifiers, password-reset links, invitation codes and other bearer secrets. A bearer token grants access to whoever possesses it, so an attacker who obtains a valid token may be able to use it without knowing a password. Protect tokens like credentials.

Use a cryptographically secure random source

Do not use Math.random() to create authentication secrets. It is intended for general-purpose pseudorandom choices, not unpredictable credentials. In browsers, crypto.getRandomValues() provides cryptographically strong random bytes through the Web Crypto API. Editagram’s Random Token Generator uses that API for its random source.

Understand entropy, not just character count

The number of characters alone does not determine token strength. Entropy depends on the number of possible values and how uniformly they are selected. For a uniformly random token drawn from an alphabet of N symbols and length L, the ideal entropy is L × log2(N) bits. If an application has a specific threat model, choose enough entropy to make guessing infeasible over the token’s lifetime.

Choose an appropriate token format

Use a character set that is safe for the context where the token will travel. URL-safe Base64 or hexadecimal encodings can be convenient for links and headers. A larger alphabet can provide more entropy per character, but escaping and transport rules matter. Avoid truncating tokens or applying predictable prefixes that reduce their effective randomness.

Generate and validate on the server where appropriate

A browser-side generator can help create random values for development or one-off uses, but production authentication systems usually need server-side issuance, validation, revocation and audit controls. A client-generated string is not automatically a registered API key or an authenticated session. The system that issues a token must control what it grants access to.

Store tokens safely

Do not commit secrets to source control or place long-lived credentials in public client-side code. Store server-side tokens in an appropriate secret store and limit access. Where a database must retain bearer tokens, consider storing a cryptographic hash and comparing a presented token safely, depending on how the token is used.

Use expiration, rotation and revocation

Give tokens only the lifetime and permissions they need. Short-lived reset links and temporary invitations should expire; long-lived API credentials need a rotation and revocation path. Expiry does not replace revocation, and an exposed token should be invalidated promptly. Log token identifiers and events carefully without logging the secret itself.

Testing and operational safeguards

Test token generation for correct length, allowed characters and expected encoding. Ensure random bytes are not accidentally logged, cached or included in analytics. Apply rate limits to token redemption and use one-time semantics for reset or invitation links. Review the design with a security professional before using generated values for production authentication.

Frequently Asked Questions

Is Math.random secure enough for API tokens?

No. Use a cryptographically secure random source such as the Web Crypto API in the browser or an appropriate cryptographic random generator on the server.

How many characters should a secure token have?

Choose length based on the alphabet, entropy target, token lifetime and threat model. Character count by itself is not a security guarantee.

Can a browser-generated token be used as a production API key?

A random string may be suitable as secret material, but a production system must also securely issue, register, validate, scope, store, rotate and revoke it.

Should tokens be stored in plaintext?

Avoid unnecessary plaintext storage and never commit tokens to source control. For server-side bearer-token databases, storing a hash can reduce exposure if the database is leaked, depending on the application design.

Related Tools

  • Random Token Generator

    Generate cryptographically secure random tokens for API keys and sessions.

  • Password Generator

    Generate strong passwords with Web Crypto randomness.

  • JWT Decoder

    Decode JWT tokens to inspect header and payload. For inspection only, not verification.

Related Guides

Explore more learning resources in Editagram Academy.