---
title: "Send events from .NET and Azure Functions"
description: "Install the .NET server SDK, name the person on every call, and make sure events leave before an Azure Function returns."
canonical: "https://www.togohq.ai/docs/dotnet"
applies_to: "TogoHq.Sdk 1.x"
updated: "2026-11-24T00:00:00Z"
---

# Send events from .NET and Azure Functions

Install the .NET server SDK, name the person on every call, and make sure events leave before an Azure Function returns.

Use the server SDK where the thing happened on your server — a payment confirmed, a subscription
renewed, an invite accepted. In browser code, use [`@togohq/sdk-browser`](/docs/reference/sdk-browser).
.NET 8 or later.

## Azure Functions (isolated worker)

```bash
dotnet add package TogoHq.Sdk.Functions.Worker
```

```csharp
using TogoHq.Sdk.Functions.Worker;

var builder = FunctionsApplication.CreateBuilder(args);
builder.UseTogo(); // sends each invocation's events before it returns
builder.Services.AddTogo(o => o.WriteKey = Environment.GetEnvironmentVariable("TOGO_WRITE_KEY")!);
builder.Build().Run();
```

Then take `ITogoClient` in your function's constructor and call `Track`:

```csharp
public sealed class PaymentWebhook(ITogoClient togo)
{
    [Function("payment-webhook")]
    public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData request)
    {
        // ...confirm the payment first
        togo.Track("order_paid", new Dictionary<string, object?> { ["plan"] = "pro" }, TogoPerson.User(userId));
        return request.CreateResponse(HttpStatusCode.OK);
    }
}
```

**Do not skip `UseTogo()`.** The Functions host may freeze the process the moment a function
returns, and a frozen process sends nothing, so events waiting for the next batch can be lost. The
middleware sends them before the invocation completes, also when the function throws. It covers
every function in the app, including ones you add later.

## Anywhere else

```bash
dotnet add package TogoHq.Sdk
```

```csharp
using TogoHq.Sdk;

// One client per process, shared by every request.
await using var togo = new TogoClient(new TogoClientOptions { WriteKey = writeKey });
togo.Track("order_paid", new Dictionary<string, object?> { ["plan"] = "pro" }, TogoPerson.User(userId));
```

Disposing the client sends what is queued. In a short-lived process, `await togo.FlushAsync()`
before it ends.

In a serverless handler with no `UseTogo()` — AWS Lambda, for example — flush in a `finally`, so the
events leave before the handler returns, also when it throws:

```csharp
public class Function
{
    // One client per process: Lambda reuses it across warm invocations.
    private static readonly TogoClient togo = new(new TogoClientOptions
    {
        WriteKey = Environment.GetEnvironmentVariable("TOGO_WRITE_KEY"),
    });

    public async Task<APIGatewayProxyResponse> Handler(APIGatewayProxyRequest request, ILambdaContext context)
    {
        try
        {
            // ...confirm the payment first
            togo.Track("order_paid", new Dictionary<string, object?> { ["plan"] = "pro" }, TogoPerson.User(userId));
            return new APIGatewayProxyResponse { StatusCode = 200 };
        }
        finally
        {
            await togo.FlushAsync();
        }
    }
}
```

## Name the person on every call

A server serves many people, so nothing is remembered between calls. Use **the same `userId` the
browser passes to `identify()`**, or each user becomes two people. Before sign-in, forward the
browser's anonymous id with `TogoPerson.Anonymous(id)`. More:
[Send events for the right person](/docs/guides/server-events).

## Check it works

Confirm the events arrived: [Check that events arrive](/docs/verify).

Use a test key (`togo_pkt_…`) while you build. `Track` returns the event's id; after
`await togo.FlushAsync()`, ask your coding agent to check those ids with
[`verify_tracking_by_event_ids`](/docs/reference/mcp-tools#verify_tracking_by_event_ids). If events
do not arrive, see [Server events are missing](/docs/troubleshooting/server-events).

A local run will not show a missing `UseTogo()` or `FlushAsync()`: your machine never freezes the
process, so the SDK's timer sends the batch anyway. Where `AWS_LAMBDA_FUNCTION_NAME` or
`FUNCTIONS_WORKER_RUNTIME` is set — Azure Functions Core Tools sets the second locally — the SDK
logs one `[togo] events were sent twice by the 10-second timer …` warning when the timer sends
twice with no `FlushAsync()` between, naming the fix. After you deploy a function, trigger it and
watch **Events → Live View → Production** in the dashboard.

Every method and option: [`TogoHq.Sdk` reference](/docs/reference/sdk-dotnet).
