> 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/go-examples.md).

# Go-Beispiele

Ausführbare Go-Beispiele für die KiloCenter-gRPC-API — generieren Sie Stubs mit buf, authentifizieren Sie sich und rufen Sie Uplink-Methoden auf.

Diese Seite bietet ausführbare Go-Beispiele für die KiloCenter gRPC API. Die vollständige API-Referenz finden Sie in der API-Referenz.

Allgemeine Go-gRPC-Dokumentation finden Sie unter [grpc.io/docs/languages/go](https://grpc.io/docs/languages/go/).

### Voraussetzungen

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

Go-Stubs aus den Proto-Definitionen mit buf generieren:

```bash
cd KC-Core/api/proto && buf generate
```

Dies erzeugt generierten Code in `KC-Core/api/gen/kilocenter/v1/`.

Installieren Sie die erforderlichen Go-Module:

```bash
go get google.golang.org/grpc
go get google.golang.org/protobuf
```

### Systemstatus abrufen

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

```go
package main

import (
	"context"
	"fmt"
	"log"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/protobuf/types/known/emptypb"

	kilocenterv1 "github.com/kilocenter/KC-Core/api/gen/kilocenter/v1"
)

// server ist der gRPC-Endpunkt von KiloCenter KC-Gateway.
var server = "localhost:9090"

func main() {
	conn, err := grpc.NewClient(server, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("Verbindung fehlgeschlagen: %v", err)
	}
	defer conn.Close()

	client := kilocenterv1.NewKiloCenterServiceClient(conn)

	resp, err := client.GetSystemStatus(context.Background(), &emptypb.Empty{})
	if err != nil {
		log.Fatalf("GetSystemStatus fehlgeschlagen: %v", err)
	}

	fmt.Printf("Version:            %s\n", resp.Version)
	fmt.Printf("Status:             %s\n", resp.Status)
	fmt.Printf("Laufzeit:           %s\n", resp.Uptime.AsTime())
	fmt.Printf("Aktive Endpunkte:   %d\n", resp.ActiveEndpoints)
	fmt.Printf("Aktive Basisstationen: %d\n", resp.ActiveBasestations)
	fmt.Printf("Verarbeitete Nachrichten: %d\n", resp.MessagesProcessed)

	for _, svc := range resp.Services {
		fmt.Printf("  Dienst: %s  gesund=%v  Latenz=%dms\n", svc.Name, svc.Healthy, svc.LatencyMs)
	}
}
```

### Endpunkte auflisten

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

```go
package main

import (
	"context"
	"fmt"
	"log"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"

	kilocenterv1 "github.com/kilocenter/KC-Core/api/gen/kilocenter/v1"
)

var server = "localhost:9090"

func main() {
	conn, err := grpc.NewClient(server, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("Verbindung fehlgeschlagen: %v", err)
	}
	defer conn.Close()

	client := kilocenterv1.NewKiloCenterServiceClient(conn)

	pageToken := ""
	pageSize := int32(100)

	for {
		resp, err := client.ListEndPoints(context.Background(), &kilocenterv1.ListEndPointsRequest{
			PageSize:  pageSize,
			PageToken: pageToken,
		})
		if err != nil {
			log.Fatalf("ListEndPoints fehlgeschlagen: %v", err)
		}

		fmt.Printf("Gesamtzahl der Endpunkte: %d\n", resp.TotalCount)

		for _, ep := range resp.Endpoints {
			fmt.Printf("  EUI: %s  Name: %s  Status: %s  Klasse: %s\n",
				ep.EpEui, ep.Name, ep.Status, ep.EpClass)
		}

		if resp.NextPageToken == "" {
			break
		}
		pageToken = resp.NextPageToken
	}
}
```

### 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`.

```go
import "google.golang.org/grpc/metadata"

token := "your-jwt-token"
orgID := "your-organization-uuid"
userID := "your-user-uuid"

ctx := metadata.AppendToOutgoingContext(context.Background(),
	"authorization", "Bearer "+token,
	"x-organization-id", orgID,
	"x-user-id", userID,
)

resp, err := client.ListEndPoints(ctx, &kilocenterv1.ListEndPointsRequest{
	PageSize: 100,
})
```

#### 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.

```go
apiKey := "your-api-key"
orgID := "your-organization-uuid"

ctx := metadata.AppendToOutgoingContext(context.Background(),
	"authorization", "Bearer "+apiKey,
	"x-organization-id", orgID,
)

resp, err := client.ListEndPoints(ctx, &kilocenterv1.ListEndPointsRequest{
	PageSize: 100,
})
```


---

# 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/go-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.
