Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

gen - Generate IDs

Generate new identifiers of various types.

Usage

idt gen <TYPE> [OPTIONS]

Arguments

ArgumentDescription
TYPEID type to generate (uuid, uuidv7, ulid, nanoid, snowflake, etc.)

Options

OptionDescription
-n, --count <N>Number of IDs to generate (default: 1)
-f, --format <FORMAT>Output encoding format
-T, --template <TPL>Wrap each ID in a template string ({} = placeholder)
-o, --output <FORMAT>Output format (json, yaml, toml)
--no-newlineDon’t print trailing newline (single ID only)

UUID Options

OptionDescription
--uuid-version <V>UUID version (1, 4, 6, 7)
--namespace <NS>Namespace for UUID v3/v5 (dns, url, oid, x500, or UUID)
--name <NAME>Name for UUID v3/v5

NanoID Options

OptionDescription
--alphabet <CHARS>Custom alphabet
--length <N>Custom length (default: 21)

Snowflake Options

OptionDescription
--preset <NAME>Snowflake preset (twitter, discord, instagram, sonyflake, mastodon)
--epoch <EPOCH>Custom epoch (discord, twitter, or milliseconds since Unix epoch)
--field <NAME=VALUE>Set a Snowflake field value (e.g., --field shard_id=42)
--machine-id <N>Machine/worker ID (0-31 for Twitter/Discord, 0-65535 for Sonyflake)
--datacenter-id <N>Datacenter ID (0-31, Twitter/Discord layout only)

Note: --preset and --epoch cannot be used together.

TypeID Options

OptionDescription
--prefix <PREFIX>Type prefix for TypeID

Supported Types

TypeAliasDescription
uuid-UUIDv4 (random) by default
uuidv1-UUIDv1 (timestamp + MAC)
uuidv4-UUIDv4 (random)
uuidv6-UUIDv6 (reordered timestamp)
uuidv7-UUIDv7 (Unix timestamp + random)
uuid-nil-Nil UUID (all zeros)
uuid-max-Max UUID (all ones)
ulid-ULID
nanoid-NanoID
snowflake-Snowflake ID

Examples

Basic Generation

# Generate a random UUID (v4)
idt gen uuid

# Generate UUIDv7 (time-sortable)
idt gen uuidv7

# Generate ULID
idt gen ulid

# Generate NanoID
idt gen nanoid

# Generate Snowflake ID
idt gen snowflake

Multiple IDs

# Generate 10 UUIDs
idt gen uuid -n 10

# Generate 100 ULIDs
idt gen ulid -n 100

UUID Versions

# UUIDv1 (timestamp-based)
idt gen uuidv1

# UUIDv6 (reordered timestamp)
idt gen uuidv6

# UUIDv7 (Unix timestamp)
idt gen uuidv7

# Or use --uuid-version flag
idt gen uuid --uuid-version 7

NanoID Customization

# Custom length
idt gen nanoid --length 32

# Custom alphabet (hex characters only)
idt gen nanoid --alphabet "0123456789abcdef"

# Both
idt gen nanoid --length 16 --alphabet "0123456789ABCDEF"

Snowflake Customization

# Use a preset (layout + epoch + timestamp resolution)
idt gen snowflake --preset twitter
idt gen snowflake --preset discord
idt gen snowflake --preset instagram --field shard_id=42
idt gen snowflake --preset sonyflake       # 10ms timestamp resolution
idt gen snowflake --preset mastodon

# With machine and datacenter IDs
idt gen snowflake --preset twitter --machine-id 1 --datacenter-id 2

# Backward-compatible epoch flag
idt gen snowflake --epoch discord
idt gen snowflake --epoch twitter
idt gen snowflake --epoch 1420070400000

Output Formats

# Generate and output as hex
idt gen uuid -f hex

# Generate and output as Base64
idt gen uuidv7 -f base64

# Save to file
idt gen uuid -n 1000 -o uuids.txt

Structured Output (JSON, YAML, TOML)

# Single ID as JSON
idt gen uuid --output json
# Output: {"id":"550e8400-e29b-41d4-a716-446655440000"}

# JSON shorthand
idt gen uuid --json
idt gen uuid -j

# Multiple IDs as JSON array
idt gen uuid -n 3 --output json
# Output: ["550e8400-...", "6ba7b810-...", "7c9e6679-..."]

# YAML output
idt gen uuid --output yaml
# Output: id: '550e8400-e29b-41d4-a716-446655440000'

# TOML output
idt gen uuid --output toml
# Output: id = "550e8400-e29b-41d4-a716-446655440000"

Template Output

# Wrap in SQL statement
idt gen uuidv7 -T 'INSERT INTO users (id) VALUES ("{}");'

# JSON fixture
idt gen uuidv7 -n 3 -T '{"id": "{}"}'

# Combined with format conversion
idt gen uuidv7 --format hex -T 'id={}'

Note: --template cannot be used with structured output formats (--json, --output). If the template does not contain {}, a warning is printed to stderr.

Without Trailing Newline

# Useful for scripting
ID=$(idt gen uuid --no-newline)
echo "Generated: $ID"