Generate a NanoID—a compact, URL-safe random identifier—using crypto.getRandomValues entirely in the browser.
How it works
You choose a length from 1 to 64. The generator samples random bytes and maps them onto the URL-safe alphabet (digits, uppercase and lowercase letters, _, and -), rejecting out-of-range samples so every character is uniformly chosen from that alphabet. Default length 21 matches common NanoID practice for collision resistance in typical app ID spaces.
When to use it
- Mint opaque primary keys or invite tokens without a server round-trip
- Create short public slugs that avoid
+and/from Base64 - Prototype ID formats before wiring a backend library
Limitations
This is not a UUID and not sorted by time. Collision risk grows if you use very short lengths at high volume. The browser entropy source must be available (crypto.getRandomValues). Lengths outside 1–64 return invalid-length.
Example
Length 21 produces a 21-character string such as V1StGXR8_Z5jdHi6B-myT (example shape only; each run differs). Length 0 or 65 fails with invalid-length.