Humanidy makes it easy to create strongly typed, human-readable identifiers in C#.
The identifiers are of the form,
pi_3LKQhvGUcADgqoEM3bh6pslE
└─┘└──────────────────────┘
└─ Prefix └─ Randomly generated alphanumeric characters
Inspired by Stripe's identifiers, Humanidy is built to,
- Be human-decipherable - if you see
3e08d1dc-de54-40e8-a490-33082b7b3e34in your diagnostics where multiple objects are identified byGuid, you're none-the-wiser. If you seeuser_3LKQhvGUcADgqoEM3bh6pslE, you know straight away that it's a user identifier. Stripe also distinguish between live and test API keys withsk_live_andsk_test_prefixes. - Be type-safe and avoid "primitive obsession" - If you've got multiple objects identified by an identifier of the same type (commonly
Guidorint), they're easily mixed up in method signatures. Strongly-typed identifiers prevent these bugs at compile time. - Avoid coupling and enumeration attacks - Using the row number from your SQL database as the identifier ties you to the database implementation and can expose you to enumeration attacks. Humanidy identifiers are decoupled from the underlying storage and don't leak information about your database.
This format is commonly used by companies other than Stripe, too, like GitHub, OpenAI, and Anthropic, to name a few.
dotnet add package HumanidyYou can add PrivateAssets="All" and ExcludeAssets="runtime" to the PackageReference to prevent the package being included in other projects and the build output.
using Humanidy;
[Humanidy("user")]
public partial struct UserId;
var userId = UserId.NewId();
Console.WriteLine(userId); // Something like "user_Zum7hYK9w0bLE"The source generator implements equality, comparison, and serialization, including
IEquatable<T>andIComparable<T>, as well as their operator overloads.ISpanParsable<T>andISpanFormattablewhich is used by ASP.NET Core's route and model binding, as well as enabling efficient formatting into interpolated strings.IUtf8SpanParsable<T>andIUtf8SpanFormattablewhich is used to efficiently read and write JSON.