LightRateClient 1.0.0

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

LightRate Client C#

A C# client for the Lightrate token management API, providing easy-to-use methods for consuming tokens with local bucket management.

Installation

Install the package via NuGet:

Install-Package LightRateClient

Or via .NET CLI:

dotnet add package LightRateClient

Or add to your .csproj:

<PackageReference Include="LightRateClient" Version="1.0.0" />

Usage

Basic Usage

using LightRateClient.Client;
using LightRateClient.Models;

// Simple usage - pass your API key and application ID
var client = new LightRateClient("your_api_key", "your_application_id");

// With additional options
var options = new ClientOptions
{
    Timeout = 60,
    DefaultLocalBucketSize = 10
};
var client = new LightRateClient("your_api_key", "your_application_id", options);

Consuming Tokens

// Consume tokens by operation
var response = client.ConsumeTokens(
    userIdentifier: "user123",
    tokensRequested: 1,
    operation: "send_email"
);

// Or consume tokens by path
var response = client.ConsumeTokens(
    userIdentifier: "user123",
    tokensRequested: 1,
    path: "/api/v1/emails/send",
    httpMethod: "POST"
);

if (response.TokensConsumed > 0)
{
    Console.WriteLine($"Tokens consumed successfully. Remaining: {response.TokensRemaining}");
}
else
{
    Console.WriteLine("Failed to consume tokens");
}
Using Local Token Buckets

The client supports local token buckets for improved performance. Buckets are automatically created based on the rules returned by the API, and are matched against incoming requests using the matcher field from the rule.

// Configure client with default bucket size
var options = new ClientOptions
{
    DefaultLocalBucketSize = 20  // All operations use this bucket size
};
var client = new LightRateClient("your_api_key", "your_application_id", options);

// Consume tokens using local bucket (more efficient)
var result = client.ConsumeLocalBucketToken(
    userIdentifier: "user123",
    operation: "send_email"
);

Console.WriteLine($"Success: {result.Success}");
Console.WriteLine($"Used local token: {result.UsedLocalToken}");
Console.WriteLine($"Bucket status: {result.BucketStatus}");

Bucket Matching:

  • Buckets are matched using the matcher field from the rule, which supports regex patterns
  • Each user has separate buckets per rule, ensuring proper isolation
  • Buckets expire after 60 seconds of inactivity
  • Default rules (isDefault: true) do not create local buckets

Complete Example

using LightRateClient.Client;
using LightRateClient.Errors;
using LightRateClient.Models;

// Create a client with your API key and application ID
var client = new LightRateClient("your_api_key", "your_application_id");

try
{
    // Consume tokens
    var consumeResponse = client.ConsumeTokens(
        userIdentifier: "user123",
        tokensRequested: 1,
        operation: "send_email"
    );

    if (consumeResponse.TokensConsumed > 0)
    {
        Console.WriteLine($"Successfully consumed tokens. Remaining: {consumeResponse.TokensRemaining}");
        // Proceed with your operation
    }
    else
    {
        Console.WriteLine("Failed to consume tokens");
        // Handle rate limiting
    }
}
catch (UnauthorizedError e)
{
    Console.WriteLine($"Authentication failed: {e.Message}");
}
catch (TooManyRequestsError e)
{
    Console.WriteLine($"Rate limited: {e.Message}");
}
catch (APIError e)
{
    Console.WriteLine($"API Error ({e.StatusCode}): {e.Message}");
}
catch (NetworkError e)
{
    Console.WriteLine($"Network error: {e.Message}");
}

Error Handling

The client provides comprehensive error handling with specific exception types:

try
{
    var response = client.ConsumeTokens(...);
}
catch (UnauthorizedError e)
{
    Console.WriteLine($"Authentication failed: {e.Message}");
}
catch (NotFoundError e)
{
    Console.WriteLine($"Resource not found: {e.Message}");
}
catch (APIError e)
{
    Console.WriteLine($"API Error ({e.StatusCode}): {e.Message}");
}
catch (NetworkError e)
{
    Console.WriteLine($"Network error: {e.Message}");
}
catch (TimeoutError e)
{
    Console.WriteLine($"Request timed out: {e.Message}");
}

Available error types:

  • LightRateError - Base error class
  • ConfigurationError - Configuration-related errors
  • APIError - Base API error class
  • BadRequestError - 400 errors
  • UnauthorizedError - 401 errors
  • ForbiddenError - 403 errors
  • NotFoundError - 404 errors
  • UnprocessableEntityError - 422 errors
  • TooManyRequestsError - 429 errors
  • InternalServerError - 500 errors
  • ServiceUnavailableError - 503 errors
  • NetworkError - Network-related errors
  • TimeoutError - Request timeout errors

API Reference

Classes

LightRateClient

Main client class for interacting with the LightRate API.

Constructor:

LightRateClient(string apiKey, string applicationId)
LightRateClient(string apiKey, string applicationId, ClientOptions options)

Methods:

  • ConsumeTokens(userIdentifier, tokensRequested, operation, path, httpMethod) -> ConsumeTokensResponse
  • ConsumeLocalBucketToken(userIdentifier, operation, path, httpMethod) -> ConsumeLocalBucketTokenResponse
  • ConsumeTokensWithRequest(request) -> ConsumeTokensResponse
  • GetAllBucketStatuses() -> Dictionary<string, TokenBucketStatus>
  • ResetAllBuckets() -> void
  • GetConfiguration() -> Configuration

Development

After checking out the repo, run dotnet build to build the project.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/lightbourne-technologies/lightrate-client-csharp. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The package is available as open source under the terms of the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.0 220 12/4/2025