Skip to content

NanoID

NanoID is a modern, secure, URL-friendly unique identifier generator designed as a compact alternative to UUID.

NanoID is a tiny, secure, URL-friendly unique string ID generator:

  • Compact: 21 characters vs 36 for UUID (no hyphens)
  • URL-safe: Uses alphabet with no special URL encoding needed (A-Za-z0-9_-)
  • Secure: Cryptographically strong randomness using hardware random generator
  • Uniform distribution: No modulo bias (uses masking + rejection sampling)
  • Collision resistant: Same probability as UUID v4 for default 21-char length
  • Customizable: Configurable size and alphabet

Example NanoID: V1StGXR8_Z5jdHi6B-myT

Enable NanoID generation in your application:

use Cline\StronglyTypedId\Enums\GeneratorType;
use Cline\StronglyTypedId\Facades\IdGenerator;
IdGenerator::setGenerator(GeneratorType::NanoId);

Now all ID generation will use NanoIDs:

$userId = UserId::generate();
// e.g., "V1StGXR8_Z5jdHi6B-myT"

Create NanoIDs with custom lengths:

use Cline\StronglyTypedId\Generators\NanoIdGenerator;
// 10-character IDs
$generator = new NanoIdGenerator(size: 10);
$shortId = $generator->generate(); // "V1StGXR8_Z"
// 50-character IDs for extra security
$generator = new NanoIdGenerator(size: 50);
$longId = $generator->generate();

Use custom character sets for specific requirements:

use Cline\StronglyTypedId\Generators\NanoIdGenerator;
// Numeric only (e.g., for legacy systems)
$generator = new NanoIdGenerator(size: 21, alphabet: '0123456789');
// Hexadecimal
$generator = new NanoIdGenerator(size: 21, alphabet: '0123456789ABCDEF');
// No ambiguous characters (0,O,1,I,5,S)
$generator = new NanoIdGenerator(
size: 21,
alphabet: '2346789ABCDEFGHJKLMNPQRTUVWXY'
);
FeatureNanoIDUUID
Length21 chars36 chars
URL-safeYesNo (hyphens)
ConfigurableYes (size, alphabet)No (fixed format)
SortableNoOnly v6/v7
Storage21 bytes36 bytes (string)

Advantages of NanoID:

  • 42% shorter for same collision resistance
  • URL-friendly by default
  • Configurable for specific needs

Advantages of UUID:

  • Standardized (RFC 4122)
  • Native database support
  • Time-based versions (v7)

For the default 21-character length with 64-character alphabet:

  • Entropy: ~126 bits (equivalent to UUID v4’s 122 bits)
  • IDs needed for 1% collision probability: ~8.9×10¹⁸ IDs
SizeEntropy1% Collision After
10 chars~60 bits~1.2 billion IDs
21 chars~126 bits~8.9×10¹⁸ IDs
32 chars~192 bitsPractically never

Store NanoIDs as VARCHAR or CHAR:

Schema::create('users', function (Blueprint $table) {
$table->char('id', 21)->primary(); // Default size
});

For chronological queries, add a separate timestamp:

Schema::create('events', function (Blueprint $table) {
$table->char('id', 21)->primary();
$table->timestamp('created_at')->index();
});
'_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
'23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz'
'0123456789ABCDEFGHJKLMNPQRSTUVWXYZ'

Choose NanoID when:

  • User-facing IDs (shorter, cleaner URLs)
  • API keys and tokens
  • QR codes (fewer chars = smaller codes)
  • Client-side ID generation
  • Custom format requirements

Choose UUID when:

  • Database UUID column types needed
  • Time-ordered IDs required (use v7)
  • Deterministic generation needed (v3/v5)
  • Enterprise system integration