Philiprehberger.IdGenerator 0.2.0

dotnet add package Philiprehberger.IdGenerator --version 0.2.0
                    
NuGet\Install-Package Philiprehberger.IdGenerator -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Philiprehberger.IdGenerator" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.IdGenerator" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Philiprehberger.IdGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.IdGenerator --version 0.2.0
                    
#r "nuget: Philiprehberger.IdGenerator, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Philiprehberger.IdGenerator@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.IdGenerator&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Philiprehberger.IdGenerator&version=0.2.0
                    
Install as a Cake Tool

Philiprehberger.IdGenerator

CI NuGet Last updated

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

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 102 6/15/2026
0.1.8 123 4/1/2026
0.1.7 111 3/27/2026
0.1.6 110 3/23/2026
0.1.5 104 3/23/2026
0.1.4 108 3/17/2026
0.1.3 111 3/16/2026
0.1.2 112 3/16/2026
0.1.1 114 3/16/2026