> For the complete documentation index, see [llms.txt](https://docs.kiloiot.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kiloiot.io/kilo-docs-de/kilo-center/kilo-mioty-service-center/integrations/api-reference/c-examples.md).

# C#-Beispiele

Ausführbare C#-Beispiele für die KiloCenter-gRPC-API — erstellen Sie ein Konsolenprojekt, fügen Sie Grpc.Net.Client hinzu und rufen Sie Methoden auf.

Diese Seite bietet ausführbare C#-Beispiele für die KiloCenter-gRPC-API. Die vollständige API-Referenz finden Sie unter API Reference.

Für allgemeine C#-gRPC-Dokumentation siehe [grpc.io/docs/languages/csharp](https://grpc.io/docs/languages/csharp/).

### Voraussetzungen

> Alle Befehle in diesem Leitfaden setzen voraus, dass Ihr Arbeitsverzeichnis `kilocenter-modules/`.

Erstellen Sie ein neues Konsolenprojekt und fügen Sie die erforderlichen NuGet-Pakete hinzu:

```bash
dotnet new console -n KiloCenterClient
cd KiloCenterClient
dotnet add package Grpc.Net.Client
dotnet add package Google.Protobuf
dotnet add package Grpc.Tools
```

Fügen Sie die Proto-Dateien zu Ihrer `.csproj` für die Codegenerierung hinzu:

```xml
<ItemGroup>
  <Protobuf Include="path/to/KC-Core/api/proto/kilocenter.proto"
            GrpcServices="Client"
            AdditionalImportDirs="path/to/KC-Core/api/proto" />
  <Protobuf Include="path/to/KC-Core/api/proto/core.proto"
            GrpcServices="None"
            AdditionalImportDirs="path/to/KC-Core/api/proto" />
  <Protobuf Include="path/to/KC-Core/api/proto/identity.proto"
            GrpcServices="None"
            AdditionalImportDirs="path/to/KC-Core/api/proto" />
</ItemGroup>
```

Ersetze `path/to/` durch den tatsächlichen Pfad zum KiloCenter-Repository. Beim Erstellen des Projekts werden C#-Stubs automatisch generiert.

### Systemstatus abrufen

Ruft den aktuellen Systemstatus ab, einschließlich Version, Laufzeit und Dienstgesundheit.

```csharp
using Grpc.Net.Client;
using Google.Protobuf.WellKnownTypes;
using Kilocenter.Api.V1;

// KiloCenter-KC-Gateway-gRPC-Endpunkt
var server = "http://localhost:9090";

using var channel = GrpcChannel.ForAddress(server);
var client = new KiloCenterService.KiloCenterServiceClient(channel);

var resp = await client.GetSystemStatusAsync(new Empty());

Console.WriteLine($"Version:             {resp.Version}");
Console.WriteLine($"Status:              {resp.Status}");
Console.WriteLine($"Betriebszeit:        {resp.Uptime}");
Console.WriteLine($"Aktive Endpunkte:    {resp.ActiveEndpoints}");
Console.WriteLine($"Aktive Basisstationen: {resp.ActiveBasestations}");
Console.WriteLine($"Verarbeitete Nachrichten:  {resp.MessagesProcessed}");

foreach (var svc in resp.Services)
{
    Console.WriteLine($"  Dienst: {svc.Name}  gesund={svc.Healthy}  Latenz={svc.LatencyMs}ms");
}
```

### Endpunkte auflisten

Listet registrierte Endpunkte mit Unterstützung für Paginierung auf. Profil mit hohem Volumen: Standard-Seitengröße 100, Maximum 1000.

```csharp
using Grpc.Net.Client;
using Kilocenter.Api.V1;

var server = "http://localhost:9090";

using var channel = GrpcChannel.ForAddress(server);
var client = new KiloCenterService.KiloCenterServiceClient(channel);

var pageToken = "";
var pageSize = 100;

tun
{
    var resp = await client.ListEndPointsAsync(new ListEndPointsRequest
    {
        PageSize = pageSize,
        PageToken = pageToken,
    });

    Console.WriteLine($"Endpunkte insgesamt: {resp.TotalCount}");

    foreach (var ep in resp.Endpoints)
    {
        Console.WriteLine($"  EUI: {ep.EpEui}  Name: {ep.Name}  Status: {ep.Status}  Klasse: {ep.EpClass}");
    }

    pageToken = resp.NextPageToken;
}
while (!string.IsNullOrEmpty(pageToken));
```

### Authentifizierung

#### Community Edition

Die Community Edition läuft im Single-Tenant-Modus, wobei Authentifizierung und Organisationsdurchsetzung deaktiviert sind (`auth.enabled: false`, `org_enforcement_enabled: false`). Die obigen Beispiele funktionieren ohne Header.

#### Enterprise: JWT-Benutzerprinzipal

Erfordert drei Header: `authorization`, `x-organization-id`, und `x-user-id`. Der `x-user-id` muss mit dem authentifizierten Benutzer im JWT übereinstimmen. Fehlt `x-user-id` gibt `ErrTokenUserIDHeaderRequired`; eine Nichtübereinstimmung gibt `ErrTokenIdentityMismatch`.

```csharp
using Grpc.Core;

var token = "your-jwt-token";
var orgId = "your-organization-uuid";
var userId = "your-user-uuid";

var headers = new Metadata
{
    { "authorization", "Bearer " + token },
    { "x-organization-id", orgId },
    { "x-user-id", userId },
};

var resp = await client.ListEndPointsAsync(
    new ListEndPointsRequest { PageSize = 100 },
    headers
);
```

#### Enterprise: Service-Account-API-Schlüssel

Erfordert zwei Header: `authorization` und `x-organization-id`. Nicht **nicht** senden `x-user-id` — das Einschließen gibt `ErrTokenIdentityMismatch` zur Vermeidung von Benutzer-Injection.

```csharp
var apiKey = "your-api-key";
var orgId = "your-organization-uuid";

var headers = new Metadata
{
    { "authorization", "Bearer " + apiKey },
    { "x-organization-id", orgId },
};

var resp = await client.ListEndPointsAsync(
    new ListEndPointsRequest { PageSize = 100 },
    headers
);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.kiloiot.io/kilo-docs-de/kilo-center/kilo-mioty-service-center/integrations/api-reference/c-examples.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
