Philiprehberger.IdGenerator


Sortable, URL-safe unique ID generators — ULID, monotonic ULID, NanoID, UUID v7, and prefixed IDs.
Installation
dotnet add package Philiprehberger.IdGenerator
Usage
using Philiprehberger.IdGenerator;
// Generate a ULID (sortable, 26 chars)
var ulid = Id.NewUlid();
Console.WriteLine(ulid); // "01HXYZ..."
// Generate a monotonic ULID — strictly increasing within a millisecond
var mono = Id.NewMonotonicUlid();
// Generate a NanoID (21 chars, URL-safe)
var nano = Id.NewNanoId();
Console.WriteLine(nano); // "V1StGXR8_Z5jdHi6B-myT"
// Generate a NanoID with custom size and alphabet
var custom = Id.NewNanoId(size: 10, alphabet: "0123456789abcdef");
// Generate a Stripe-style prefixed ID
var prefixed = Id.NewPrefixed("usr");
Console.WriteLine(prefixed); // "usr_01HXYZ..."
// Generate a short ID (12 chars)
var shortId = Id.NewShortId();
Console.WriteLine(shortId); // "a8f3kQ_9xZrT"
// Generate a UUID v7 (RFC 9562) — time-sortable Guid
Guid guidV7 = Id.NewGuidV7();
ULID
using Philiprehberger.IdGenerator;
var ulid = new Ulid();
// Extract timestamp
DateTimeOffset timestamp = ulid.Timestamp;
// Parse from string
var parsed = Ulid.Parse("01HXYZ...");
// Safe parsing
if (Ulid.TryParse("01HXYZ...", out var result))
{
Console.WriteLine(result);
}
// Convert to GUID
Guid guid = ulid.ToGuid();
// Round-trip through raw bytes
byte[] bytes = ulid.ToByteArray();
var restored = Ulid.FromBytes(bytes);
// ULIDs are sortable
var a = Id.NewUlid();
var b = Id.NewUlid();
Console.WriteLine(a < b); // true (a was created first)
Monotonic ULID
using Philiprehberger.IdGenerator;
// Guaranteed to sort strictly after the previous monotonic ULID,
// even when called multiple times within the same millisecond.
var a = Ulid.NewMonotonic();
var b = Ulid.NewMonotonic();
var c = Ulid.NewMonotonic();
Console.WriteLine(a < b && b < c); // true
UUID v7
using Philiprehberger.IdGenerator;
// RFC 9562 UUID v7 — 48-bit Unix-ms timestamp + 74 bits random.
// Sortable as a string, fits anywhere a Guid is expected.
Guid id = Id.NewGuidV7();
Console.WriteLine(id);
Prefixed IDs
using Philiprehberger.IdGenerator;
// Create prefixed IDs for different entity types
var userId = PrefixedId.Create("usr"); // "usr_01HXYZ..."
var orgId = PrefixedId.Create("org"); // "org_01HXYZ..."
// Validate a prefixed ID
bool valid = PrefixedId.Validate("usr_01HXYZ...", "usr"); // true
bool wrong = PrefixedId.Validate("usr_01HXYZ...", "org"); // false
JSON Serialization
using System.Text.Json;
using Philiprehberger.IdGenerator;
var options = new JsonSerializerOptions();
options.Converters.Add(new UlidJsonConverter());
var ulid = Id.NewUlid();
var json = JsonSerializer.Serialize(ulid, options); // "\"01HXYZ...\""
var back = JsonSerializer.Deserialize<Ulid>(json, options);
API
Id (static factory)
| Method | Description |
|---|
NewUlid() | Generate a new ULID |
NewMonotonicUlid() | Generate a strictly-increasing ULID (monotonic within a millisecond) |
NewNanoId(int size = 21, string? alphabet = null) | Generate a NanoID |
NewPrefixed(string prefix) | Generate a prefixed ID |
NewShortId(int length = 12) | Generate a short random ID |
NewGuidV7() | Generate a UUID v7 Guid (RFC 9562) |
Ulid (readonly struct)
| Member | Description |
|---|
Ulid() | Create a new ULID with current timestamp |
Ulid.NewMonotonic() | Create a monotonic ULID guaranteed to sort after any previous monotonic value |
Ulid.FromBytes(ReadOnlySpan<byte>) | Create a ULID from its 16-byte representation |
Timestamp | Extract the timestamp as DateTimeOffset |
Parse(string) | Parse a ULID from a 26-char string |
TryParse(string?, out Ulid) | Safely parse a ULID string |
ToGuid() | Convert to a Guid |
ToByteArray() | Return a copy of the 16-byte payload |
ToString() | 26-char Crockford Base32 representation |
==, !=, <, >, <=, >= | Comparison operators |
NanoId (static)
| Method | Description |
|---|
Generate(int size = 21, string? alphabet = null) | Generate a random NanoID |
PrefixedId (static)
| Method | Description |
|---|
Create(string prefix) | Create a prefixed ID with a ULID suffix |
Validate(string id, string expectedPrefix) | Check prefix and ULID validity |
UlidJsonConverter
| Description |
|---|
| System.Text.Json converter for ULID serialization/deserialization |
Development
dotnet build src/Philiprehberger.IdGenerator.csproj --configuration Release
dotnet test tests/Philiprehberger.IdGenerator.Tests/Philiprehberger.IdGenerator.Tests.csproj --configuration Release
Support
If you find this project useful:
⭐ Star the repo
🐛 Report issues
💡 Suggest features
❤️ Sponsor development
🌐 All Open Source Projects
💻 GitHub Profile
🔗 LinkedIn Profile
License
MIT