Files
planet/docs/technical/en/api-integration-guide.md
T
linkong b3d2d9e9f1
ci / backend (push) Canceled after 0s
ci / frontend (push) Canceled after 0s
ci / delivery (push) Canceled after 0s
release: bump version to 0.81.0
2026-11-30 19:39:29 +08:00

65 lines
4.0 KiB
Markdown

# API Integration and Authentication Guide
This guide covers how external services, Unreal Engine (UE) projects, and internal automation scripts authenticate against Planet backend APIs while keeping public data endpoints protected from unauthenticated scraping.
## Domains and Responsibilities
| Domain | Role | Notes |
| --- | --- | --- |
| `https://rclaw.top` | Primary host & desktop default backend | Used by Planet Earth desktop clients and hot-update delivery; `/docs` and `/earth` redirect to `admin.rclaw.top` |
| `https://api.rclaw.top` | Dedicated API & WebSocket entry | Recommended endpoint for external integrations, UE clients, and automated scripts |
| `https://admin.rclaw.top` | Lightweight Admin Console & Gatekeeper Docs | Hosts `/settings?section=api_keys`, user role management, and controlled documentation |
> Public FastAPI `/docs` and `/openapi.json` are disabled in production. Authenticated `admin`, `super_admin`, or `docs_developer` users can inspect the full schema inside the in-app **API Reference** view on the Docs site or export the curated external OpenAPI schema from `/settings?section=api_keys`.
## Authentication Modes by Caller
### 1. UE Clients, Internal Tools & External Partners: `X-API-Key` (Recommended)
Administrators issue API keys from `/settings?section=api_keys`. Only a SHA-256 hash and masked prefix are persisted; the plaintext secret is displayed once upon creation.
- **UE / Internal preset**: Select `earth:all_read` (grants all Earth read-only routes plus `/ws` realtime streaming), **Never expires**, and **Unlimited** rate limit so UE projects need zero token-refresh logic.
- **External partner preset**: Select granular read scopes (`visualization:read`, `layers:read`, `vessels:read`, `news:read`, `data_products:read`, `situational:read`, `earth:read`, `interactables:read`, `tv:read`, `ws:connect`) along with explicit expiration and per-minute rate limits.
**HTTP example (`curl` / UE `FHttpModule`):**
```bash
curl -H "X-API-Key: <your_api_key>" \
https://api.rclaw.top/api/v1/visualization/geo/summary
```
**WebSocket example (UE `IWebSocket`):**
```text
wss://api.rclaw.top/ws?api_key=<your_api_key>
```
(Passing `X-API-Key` in the WebSocket upgrade header is also supported.)
### 2. Authorized Staff Accounts: `Authorization: Bearer <JWT>`
Self-registered `viewer` accounts cannot scrape protected data routes by default. A `super_admin` can grant the `api_access` Gatekeeper group in `/users` (`admin` and `super_admin` already have access).
```bash
curl -X POST https://api.rclaw.top/api/v1/auth/login \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=<user>&password=<password>"
```
Include `Authorization: Bearer <access_token>` on subsequent requests and refresh tokens with `POST /api/v1/auth/refresh`.
### 3. Zero-Login First-Party Earth Clients
- **Web Earth (`/earth`)**: Calls same-origin `POST /api/v1/auth/earth-session` (validated via same-origin headers and optional Cloudflare Turnstile verification) to obtain a short-lived `HttpOnly; SameSite=Strict` cookie (`planet_earth_session`) and WebSocket token with silent keep-alive renewal.
- **Planet Earth Desktop Client**: The Electron main process exchanges a desktop session credential at `POST /api/v1/auth/earth-session` and injects `X-Planet-Earth-Token` on all proxied API requests (automatically refreshing once on HTTP `401`).
## Public Exemptions & Write Endpoint Guards
The following endpoints remain publicly accessible for health checks, desktop client self-updates, and public documentation:
- `GET /health`, `GET /health/live`, `GET /health/ready`, `GET /api/health`
- `GET /api/v1/client/update-manifest`, `GET /api/v1/client/updates/files/{filename}`
- `POST /api/v1/auth/*`, `GET /api/v1/docs/catalog`, `GET /api/v1/docs/{lang}/{slug}`
State-mutating compute-center location endpoints (`/api/v1/visualization/compute-centers/{source_id}/collect-location` and `/location`) require administrator JWT authentication. `POST /api/v1/news/refresh` requires Earth/API access and enforces a 60-second per-actor cooldown window.