Open Spanner
SDKs

C#

Use Open Spanner from .NET services.

Install:

dotnet add package OpenSpanner

Example

Create a meter and record usage from a trusted .NET service:

using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using OpenSpanner;
using OpenSpanner.Models;

var authProvider = new BaseBearerTokenAuthenticationProvider(
    new ApiKeyProvider(Environment.GetEnvironmentVariable("OPEN_SPANNER_API_KEY")!)
);
var adapter = new HttpClientRequestAdapter(authProvider)
{
    BaseUrl = Environment.GetEnvironmentVariable("OPEN_SPANNER_BASE_URL") ?? "http://localhost:18081",
};
var client = new OpenSpannerClient(adapter);

var meterName = "api_requests";

await client.V1.Meters.PostAsync(new MeterCreateRequest
{
    Name = meterName,
    Description = "API requests served",
    Unit = "request",
    Aggregation = "sum",
    EventRetentionDays = 90,
});

var metadata = new UsageCreateRequest_metadata();
metadata.AdditionalData["endpoint"] = "/v1/orders";
metadata.AdditionalData["region"] = "us-east";

await client.V1.Usages.PostAsync(new UsageCreateRequest
{
    IdempotencyKey = Guid.NewGuid().ToString(),
    Subject = "org_123",
    Meter = meterName,
    Quantity = 1,
    Metadata = metadata,
});

sealed class ApiKeyProvider(string apiKey) : IAccessTokenProvider
{
    public AllowedHostsValidator AllowedHostsValidator { get; } = new();

    public Task<string> GetAuthorizationTokenAsync(
        Uri uri,
        Dictionary<string, object>? additionalAuthenticationContext = null,
        CancellationToken cancellationToken = default)
    {
        return Task.FromResult(apiKey);
    }
}

Create an API key in the dashboard first and expose it to your backend process as OPEN_SPANNER_API_KEY.

Run The Full Example

cd examples/rest/basic/csharp
dotnet run --project OpenSpanner.Example.csproj

On this page