Skip to content
Tutorial Getting started

Getting started

Install Klyroo.Hotkey in your .NET 11 backend, register the typed HTTP client, provision one persistent cloud browser per user, request a human handoff, verify the signed webhook that resumes your workflow, and connect a Blazor or .NET MAUI client safely.

On this page

Prerequisites

  • The .NET 11 SDK and a server-side host (net11.0 ASP.NET Core minimal API, Blazor Web App server project, or background worker).
  • A Hotkey workspace API key and a webhook endpoint secret stored in server-side secret configuration (user secrets, environment variables, or a secret manager — never shipped to a browser or mobile binary).

1. Build and install the package

Klyroo.Hotkey is packaged independently from this repository. Pack it to a local folder and add it to your server project:

Shell
git clone https://github.com/Klyroo/klyroo-hotkey.git
cd klyroo-hotkey
dotnet pack src/Klyroo.Hotkey -c Release -o artifacts
dotnet add /path/to/Server.csproj package Klyroo.Hotkey --version 0.1.0-preview.1 --no-restore
dotnet restore /path/to/Server.csproj --source "$PWD/artifacts" --source https://api.nuget.org/v3/index.json

2. Register the client

Store your key in server configuration (Hotkey__ApiKey in environment variables or dotnet user-secrets set "Hotkey:ApiKey" "...") and register HotkeyClient in your server project's Program.cs:

Program.cs
using Klyroo.Hotkey;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddKlyrooHotkey(
    services => services.GetRequiredService<IConfiguration>()["Hotkey:ApiKey"]
        ?? throw new InvalidOperationException("Missing server setting Hotkey:ApiKey."));

var app = builder.Build();

AddKlyrooHotkey configures a named HttpClient ("Klyroo.Hotkey") backed by SocketsHttpHandler with AllowAutoRedirect = false, UseCookies = false, a 2-minute pooled connection lifetime, a 10-second connect timeout and a 32 KiB response header limit, and binds HotkeyClient as a typed client.

3. Provision a browser and request a handoff

In an authenticated server endpoint, resolve the signed-in user's stable ID on the server and map it to their Hotkey browser. Pass persisted operation IDs as idempotencyKey so retries stay safe:

PortalEndpoints.cs
using System.Security.Claims;
using Klyroo.Hotkey;

app.MapPost("/api/portal/handoff", async (
    ClaimsPrincipal user,
    HotkeyClient hotkey,
    IBrowserDirectory directory,
    CancellationToken cancellationToken) =>
{
    var userId = user.FindFirstValue(ClaimTypes.NameIdentifier);
    if (string.IsNullOrEmpty(userId))
        return Results.Unauthorized();

    var browser = await hotkey.GetOrCreateBrowserAsync(
        new BrowserCreate(
            ExternalId: userId,
            TimeZone: "Europe/Paris",
            Languages: ["fr-FR"]),
        idempotencyKey: $"provision:{userId}",
        cancellationToken: cancellationToken);

    await directory.SaveBrowserIdAsync(userId, browser.Id, cancellationToken);

    var operationId = $"handoff:{userId}:{DateTime.UtcNow:yyyyMMddHH}";
    var ask = await hotkey.CreateAskAsync(
        new AskCreate(
            Browser: browser.Id,
            Reason: "Sign in to your reporting portal",
            Url: new Uri("https://reports.example.com/sign-in"),
            TimeoutMinutes: 15,
            SaveSignIn: true),
        idempotencyKey: operationId,
        cancellationToken: cancellationToken);

    return Results.Ok(new
    {
        ask.Id,
        ask.Status,
        HandoffUrl = ask.Link,
        ask.LinkExpires
    });
}).RequireAuthorization();

Store browser.Id in your server-side user table and never accept a client-supplied browser ID without checking ownership. Deliver ask.Link only to the authenticated session of that user.

4. Verify the signed webhook

When the person completes or cancels the handoff, Hotkey sends a signed webhook to your backend. Buffer the exact raw HTTP request bytes before parsing JSON, verify them with HotkeyWebhook.VerifyAndRead, and deduplicate on delivery.Id:

HotkeyWebhookEndpoint.cs
using Klyroo.Hotkey;

app.MapPost("/webhooks/hotkey", async (
    HttpRequest request,
    IConfiguration config,
    IWebhookStore store,
    CancellationToken cancellationToken) =>
{
    using var buffer = new MemoryStream();
    await request.Body.CopyToAsync(buffer, cancellationToken);

    var signature = request.Headers["Hotkey-Signature"].ToString();
    var secret = config["Hotkey:WebhookSecret"]
        ?? throw new InvalidOperationException("Missing server setting Hotkey:WebhookSecret.");

    var delivery = HotkeyWebhook.VerifyAndRead(buffer.ToArray(), signature, secret);
    if (delivery is null)
        return Results.Unauthorized();

    // Record delivery.Id with a unique constraint before resuming workflow state.
    if (!await store.TryRecordEventAsync(delivery.Id, delivery.Type, cancellationToken))
        return Results.Ok();

    if (delivery.Type == "ask.done" && delivery.Browser is { Id: var browserId })
        await store.ResumeWorkflowForBrowserAsync(browserId, delivery.Id, cancellationToken);

    return Results.Ok();
});

5. Call the endpoint from a Blazor or .NET MAUI client

Your frontend never touches HotkeyClient or any API key. A Blazor component or .NET MAUI page calls your own authenticated endpoint and opens the expiring link returned for that user:

PortalSync.razor
@inject HttpClient Api

<KlyButton Label="Sign in to reporting portal" OnClick="StartHandoffAsync" Loading="_loading" />

@if (_handoffUrl is not null)
{
    <KlyAlert Color="KlyColor.Info" Title="Action needed">
        <Description>
            Open the private link to finish signing in, then return here.
            <KlyLink Href="@_handoffUrl" Target="KlyLinkTarget.Blank" Label="Open handoff link" />
        </Description>
    </KlyAlert>
}

@code {
    private bool _loading;
    private string? _handoffUrl;

    private async Task StartHandoffAsync()
    {
        _loading = true;
        var result = await Api.PostAsJsonAsync("/api/portal/handoff", new { });
        result.EnsureSuccessStatusCode();
        var body = await result.Content.ReadFromJsonAsync<HandoffResponse>();
        _handoffUrl = body?.HandoffUrl?.ToString();
        _loading = false;
    }

    private sealed record HandoffResponse(string Id, string Status, Uri? HandoffUrl, DateTimeOffset? LinkExpires);
}

Next steps

  • Guides: idempotent provisioning, browser lifecycle, handoff renewal, webhook deduplication, live-view embeds, downloads, usage exports, scoped keys and offline unit testing.
  • Concepts: server-side ownership, workspace vs browser-scoped keys, retry and idempotency rules, HMAC-SHA256 verification and design trade-offs.
  • Platforms: server hosting, Blazor render modes, .NET MAUI clients, NativeAOT and trimming.
  • API reference: every public type, property and method in Klyroo.Hotkey.
  • FAQ: troubleshooting, known limitations and common questions.