AmasiaLabs.Toolkit.FlowflakeId.Extensions 1.4.22

dotnet add package AmasiaLabs.Toolkit.FlowflakeId.Extensions --version 1.4.22
                    
NuGet\Install-Package AmasiaLabs.Toolkit.FlowflakeId.Extensions -Version 1.4.22
                    
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="AmasiaLabs.Toolkit.FlowflakeId.Extensions" Version="1.4.22" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AmasiaLabs.Toolkit.FlowflakeId.Extensions" Version="1.4.22" />
                    
Directory.Packages.props
<PackageReference Include="AmasiaLabs.Toolkit.FlowflakeId.Extensions" />
                    
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 AmasiaLabs.Toolkit.FlowflakeId.Extensions --version 1.4.22
                    
#r "nuget: AmasiaLabs.Toolkit.FlowflakeId.Extensions, 1.4.22"
                    
#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 AmasiaLabs.Toolkit.FlowflakeId.Extensions@1.4.22
                    
#: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=AmasiaLabs.Toolkit.FlowflakeId.Extensions&version=1.4.22
                    
Install as a Cake Addin
#tool nuget:?package=AmasiaLabs.Toolkit.FlowflakeId.Extensions&version=1.4.22
                    
Install as a Cake Tool

AmasiaLabs.Toolkit.FlowflakeId.Extensions

Dependency injection and formatting extensions for the Flowflake ID generator.

Overview

This package provides:

  • DI integration via ServiceCollectionExtensions (including decode-only scenarios)
  • DateTime extraction via FlowflakeIdParsingExtensions
  • Formatting helpers via FlowflakeIdFormattingExtensions
  • Component parsing helpers via FlowflakeIdParsingExtensions
  • Built-in codec implementations:
    • Base36 - Alphanumeric encoding (0-9, a-z)
    • Base58 - Bitcoin-style encoding (excludes ambiguous characters)
    • Base62 - Alphanumeric encoding (0-9, a-z, A-Z)
    • Base64Url - URL-safe Base64 encoding
    • Bech32 - Bech32/Bech32m encoding with built-in checksum (requires configuration)
    • CrockfordBase32 - Douglas Crockford's Base32 encoding
    • Hex - Hexadecimal encoding

Installation

dotnet add package AmasiaLabs.Toolkit.FlowflakeId.Extensions

Usage

Dependency Injection

Full ID Generation (with InstanceId)
// Simplest - uses IConfiguration from DI, reads from "Amasia:Toolkit:FlowflakeId"
services.AddFlowflakeId();

// With additional configuration
services.AddFlowflakeId(options =>
{
    options.FailoverInstanceId = 999; // Override some settings
});

// Using explicit configuration object
services.AddFlowflakeId(configuration);

// Using configuration section
services.AddFlowflakeId(configuration.GetSection("My:Custom:Section"));

// Using only code-based configuration
services.AddFlowflakeId(options =>
{
    options.InstanceId = 1;
    options.UseUtcNow = true;
    options.FlowflakeClock = new FlowflakeClockOptions
    {
        Epoch = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc),
        TimeSemantics = FlowflakeTimeSemantics.UtcNormalized
    };
});

Configuration format:

{
  "Amasia": {
    "Toolkit": {
      "FlowflakeId": {
        "InstanceId": 1,
        "UseUtcNow": true,
        "FlowflakeClock": {
          "Epoch": "2023-02-15T00:00:00Z",
          "TimeSemantics": "UtcNormalized"
        }
      }
    }
  }
}
Decode-Only Scenarios (no InstanceId required)

For services that only need to decode DateTime from existing IDs without generating new ones:

// Simplest - uses IConfiguration from DI, reads from "Amasia:Toolkit:FlowflakeId:FlowflakeClock"
services.AddFlowflakeClock();

// Using explicit configuration object
services.AddFlowflakeClock(configuration);

// Or with explicit section
services.AddFlowflakeClock(configuration.GetSection("My:Custom:Section"));

Configuration format for decode-only:

{
  "Amasia": {
    "Toolkit": {
      "FlowflakeId": {
        "FlowflakeClock": {
          "Epoch": "2023-02-15T00:00:00Z",
          "TimeSemantics": "UtcNormalized"
        }
      }
    }
  }
}

Usage example:

// Get the clock options from DI
var clockOptions = serviceProvider.GetRequiredService<IOptions<FlowflakeClockOptions>>();

// Extract DateTime from any Flowflake ID
long existingId = 123456789L;
var dateTime = existingId.GetDateTimeFromFlowflakeId(clockOptions.Value.ToFlowflakeClock());

// Also works with other parsing extensions
var instanceId = existingId.GetInstanceIdFromFlowflakeId();
var sequence = existingId.GetSequenceNumberFromFlowflakeId();
var timestamp = existingId.GetTimestampFromFlowflakeId();

This is useful for:

  • Microservices that receive IDs from other services
  • Analytics/reporting services that need to extract timestamps
  • Audit/logging systems that need to decode ID components
  • Any service that consumes but doesn't generate Flowflake IDs

Formatting Extensions

var id = 123456789L;

// Using built-in codecs with enum (Base62 is default)
string base62 = id.FormatFlowflakeId(); // Uses Base62 by default
string base36 = id.FormatFlowflakeId(FlowflakeIdCodec.Base36);
string base58 = id.FormatFlowflakeId(FlowflakeIdCodec.Base58);
string base64Url = id.FormatFlowflakeId(FlowflakeIdCodec.Base64Url);
string crockford = id.FormatFlowflakeId(FlowflakeIdCodec.CrockfordBase32);
string hex = id.FormatFlowflakeId(FlowflakeIdCodec.Hex);

// Parse back to ID
long decoded1 = base62.ParseFlowflakeId(); // Uses Base62 by default
long decoded2 = base36.ParseFlowflakeId(FlowflakeIdCodec.Base36);
long decoded3 = base58.ParseFlowflakeId(FlowflakeIdCodec.Base58);
long decoded4 = hex.ParseFlowflakeId(FlowflakeIdCodec.Hex);

// Using Bech32 codec (requires explicit instantiation with parameters)
// Note: Bech32 is not available via enum due to required constructor parameters
var bech32Codec = new Bech32Codec("flow", bech32M: true); // HRP prefix + checksum variant
string bech32 = id.FormatFlowflakeId(bech32Codec); // e.g., "flow1..."
long decodedBech32 = bech32.ParseFlowflakeId(bech32Codec);

// Using custom codec instance
var customCodec = new MyCustomCodec();
string custom = id.FormatFlowflakeId(customCodec);
long decoded5 = custom.ParseFlowflakeId(customCodec);

// Using codec provider directly
var codec = FlowflakeIdCodecProvider.GetCodec(FlowflakeIdCodec.Base62);
string encoded = id.FormatFlowflakeId(codec);

Bech32 Codec Details

Bech32 encodes IDs in format: {hrp}1{data}{checksum}

Features:

  • Limited alphabet (no ambiguous characters)
  • Built-in strong checksum (6 characters)
  • Suitable for manual input, printing, QR codes
  • Error detection and correction capabilities

Constructor parameters:

  • hrp (Human-Readable Part) - namespace prefix in lowercase (e.g., "flow", "id", "test")
  • bech32M - checksum variant:
    • false - classic Bech32 (BIP-173)
    • true - Bech32m (BIP-350, improved error protection, recommended default)

When to use:

  • Need resilience against typos/corruption (manual input, print, QR, messages)
  • Can accept longer output (~13 data chars for 62 bits + 6 checksum + hrp + '1')

Note: Bech32 codec is not available via enum in FlowflakeIdCodecProvider due to required constructor parameters.

Parsing Extensions

Extract information from Flowflake IDs without needing the generator instance:

using AmasiaLabs.Toolkit.FlowflakeId.Extensions;
using AmasiaLabs.Toolkit.FlowflakeId.Abstractions;

long id = 123456789L;

// Extract components from the ID
int instanceId = id.GetInstanceIdFromFlowflakeId();      // Extract instance ID (bits 22-30)
int sequence = id.GetSequenceNumberFromFlowflakeId();     // Extract sequence (bits 0-21)
long timestamp = id.GetTimestampFromFlowflakeId();        // Extract timestamp (seconds since epoch)

// Extract DateTime using various approaches:

// 1. Using FlowflakeClock directly
var clock = new FlowflakeClock(epoch, FlowflakeTimeSemantics.UtcNormalized);
DateTime dt1 = id.GetDateTimeFromFlowflakeId(clock);

// 2. Using FlowflakeIdOptions (from full generator config)
var options = serviceProvider.GetRequiredService<IOptions<FlowflakeIdOptions>>();
DateTime dt2 = id.GetDateTimeFromFlowflakeId(options.Value.ToFlowflakeClock());

// 3. Using FlowflakeClockOptions (decode-only config)
var clockOptions = serviceProvider.GetRequiredService<IOptions<FlowflakeClockOptions>>();
DateTime dt3 = id.GetDateTimeFromFlowflakeId(clockOptions.Value.ToFlowflakeClock());

// 4. Using explicit epoch and semantics
DateTime dt4 = id.GetDateTimeFromFlowflakeId(
    epoch: new DateTime(2023, 2, 15, 0, 0, 0, DateTimeKind.Utc),
    semantics: FlowflakeTimeSemantics.UtcNormalized);

Dependencies

  • AmasiaLabs.Toolkit.FlowflakeId.Abstractions
  • AmasiaLabs.Toolkit.FlowflakeId
  • Microsoft.Extensions.Hosting
  • Microsoft.Extensions.Options.DataAnnotations
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AmasiaLabs.Toolkit.FlowflakeId.Extensions:

Package Downloads
AmasiaLabs.Toolkit.FlowflakeId.Grpc

gRPC service wrapper around Flowflake ID generator (IFlowflakeId)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.22 109 9/4/2026
1.4.21 133 7/15/2026
1.4.20 118 7/6/2026
1.4.19 130 7/1/2026
1.4.18 144 6/18/2026
1.4.17 131 5/1/2026
1.4.15 127 4/22/2026
1.4.14 124 4/21/2026
1.4.13 129 4/21/2026