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

# Python-Beispiele

Ausführbare Python-Beispiele für die KiloCenter-gRPC-API — installieren Sie grpcio, generieren Sie Stubs und rufen Sie Uplink-Methoden auf.

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

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

### Voraussetzungen

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

Installieren Sie die gRPC-Python-Pakete:

```bash
pip install grpcio grpcio-tools
```

Generieren Sie Python-Stubs aus den Proto-Definitionen:

```bash
python -m grpc_tools.protoc \
  -I KC-Core/api/proto \
  --python_out=./gen \
  --grpc_python_out=./gen \
  KC-Core/api/proto/kilocenter.proto \
  KC-Core/api/proto/core.proto \
  KC-Core/api/proto/identity.proto
```

Dies erzeugt `kilocenter_pb2.py`, `core_pb2.py`, `identity_pb2.py`, und ihre `_grpc` Entsprechungen im `gen/` Verzeichnis.

### Systemstatus abrufen

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

```python
import sys
sys.path.insert(0, "gen")

import grpc
from google.protobuf.empty_pb2 import Empty

import kilocenter_pb2_grpc

# KiloCenter KC-Gateway gRPC-Endpunkt
server = "localhost:9090"

channel = grpc.insecure_channel(server)
client = kilocenter_pb2_grpc.KiloCenterServiceStub(channel)

resp = client.GetSystemStatus(Empty())

print(f"Version:             {resp.version}")
print(f"Status:              {resp.status}")
print(f"Laufzeit:              {resp.uptime}")
print(f"Aktive Endpunkte:    {resp.active_endpoints}")
print(f"Aktive Basisstationen: {resp.active_basestations}")
print(f"Verarbeitete Nachrichten:  {resp.messages_processed}")

for svc in resp.services:
    print(f"  Dienst: {svc.name}  healthy={svc.healthy}  latency={svc.latency_ms}ms")
```

### Endpunkte auflisten

Listet registrierte Endpunkte mit Paginierungsunterstützung auf. Hochvolumenprofil: Standardseitengröße 100, Maximum 1000.

```python
import sys
sys.path.insert(0, "gen")

import grpc

import kilocenter_pb2
import kilocenter_pb2_grpc

server = "localhost:9090"

channel = grpc.insecure_channel(server)
client = kilocenter_pb2_grpc.KiloCenterServiceStub(channel)

page_token = ""
page_size = 100

while True:
    resp = client.ListEndPoints(kilocenter_pb2.ListEndPointsRequest(
        page_size=page_size,
        page_token=page_token,
    ))

    print(f"Gesamtzahl der Endpunkte: {resp.total_count}")

    for ep in resp.endpoints:
        print(f"  EUI: {ep.epEui}  Name: {ep.name}  Status: {ep.status}  Klasse: {ep.ep_class}")

    if not resp.next_page_token:
        break
    page_token = resp.next_page_token
```

### Authentifizierung

#### Community Edition

Die Community Edition läuft im Single-Tenant-Modus, wobei die Authentifizierung und die Erzwingung von Organisationen 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` Der Wert muss mit dem im JWT authentifizierten Benutzer übereinstimmen. Fehlt `x-user-id` gibt `ErrTokenUserIDHeaderRequired`; bei einer Abweichung wird `ErrTokenIdentityMismatch`.

```python
dein-jwt-token
deine-organisations-uuid
deine-benutzer-uuid

metadata = [
    ("authorization", "Bearer " + token),
    ("x-organization-id", org_id),
    ("x-user-id", user_id),
]

resp = client.ListEndPoints(
    kilocenter_pb2.ListEndPointsRequest(page_size=100),
    metadata=metadata,
)
```

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

Erfordert zwei Header: `authorization` und `x-organization-id`. Tun **nicht** senden `x-user-id` — das Einfügen davon gibt `ErrTokenIdentityMismatch` um Benutzerinjektion zu verhindern.

```python
dein-api-schlüssel
deine-organisations-uuid

metadata = [
    ("authorization", "Bearer " + api_key),
    ("x-organization-id", org_id),
]

resp = client.ListEndPoints(
    kilocenter_pb2.ListEndPointsRequest(page_size=100),
    metadata=metadata,
)
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
