Skip to content
GreenKube

REST API

GreenKube exposes a complete REST API powered by FastAPI, providing programmatic access to all metrics, recommendations, and configuration.

The API is served at http://<service>:8000/api/v1/ and includes:

  • Auto-generated OpenAPI docs at /docs (Swagger UI) and /redoc
  • CORS support for cross-origin dashboard access
  • Optional authentication via bearer token (GREENKUBE_API_KEY)
  • Rate limiting via slowapi to protect against abuse
  • Pagination support on metrics endpoints (offset and limit)
  • JSON responses with consistent error handling
  • Pydantic v2 validation on all request/response models
MethodPathDescription
GET/healthHealth check (liveness)
GET/api/v1/versionAPI and app version
GET/api/v1/configCurrent configuration (redacted)
GET/api/v1/health/servicesAggregated health status for all data sources (supports ?force=true to bypass cache)
GET/api/v1/health/services/{service_name}Health status for a single named service
POST/api/v1/config/servicesUpdate service URLs and tokens at runtime (session-scoped)
MethodPathDescription
GET/api/v1/metricsLatest metrics snapshot
GET/api/v1/metrics/namespacesMetrics grouped by namespace
GET/api/v1/metrics/nodesPer-node metrics and hardware info
GET/api/v1/metrics/podsPer-pod detailed metrics
MethodPathDescription
GET/api/v1/timeseriesHistorical metrics with filtering
GET/api/v1/timeseries/energyEnergy consumption over time
GET/api/v1/timeseries/carbonCarbon emissions over time
GET/api/v1/timeseries/costCost data over time
MethodPathDescription
GET/api/v1/recommendationsActive recommendations
GET/api/v1/recommendations/historyHistorical recommendations (filterable by scope: pod, namespace, node)
GET/api/v1/recommendations/summaryAggregated savings potential
MethodPathDescription
GET/api/v1/report/summaryPreview row count and totals (CO₂e, energy, cost) before downloading
GET/api/v1/report/exportStream a downloadable CSV or JSON file
MethodPathDescription
GET/prometheus/metricsPrometheus-format metrics for scraping (CO₂e, cost, energy, CPU, memory, network, nodes, grid intensity, sustainability score, recommendations)

Most endpoints support common query parameters:

?from=2024-01-01T00:00:00Z # Start time (ISO 8601)
&to=2024-01-31T23:59:59Z # End time (ISO 8601)
&namespace=production # Filter by namespace
&group_by=daily # Aggregation: hourly/daily/weekly/monthly
&limit=100 # Pagination
&offset=0 # Pagination offset
Terminal window
curl http://localhost:8000/api/v1/metrics | jq
Terminal window
curl "http://localhost:8000/api/v1/timeseries/carbon?namespace=production&from=2024-01-01&group_by=daily"
Terminal window
curl http://localhost:8000/api/v1/recommendations?severity=critical

GreenKube supports optional bearer-token authentication via the GREENKUBE_API_KEY environment variable (or config.api.apiKey in Helm values):

Terminal window
# With authentication enabled
curl -H "Authorization: Bearer YOUR_API_KEY" http://localhost:8000/api/v1/metrics

When no API key is configured, the API is open. For external exposure, you can combine the API key with:

  • Kubernetes Ingress with authentication
  • OAuth2 proxy
  • Network policies

Rate limiting is available via slowapi to protect the API from abuse:

# In values.yaml
config:
api:
rateLimit: 60 # requests per minute (0 = disabled)

CORS is enabled by default to allow the dashboard to communicate with the API:

greenkube:
corsOrigins: "*" # Restrict in production

Use the JSON API data source plugin to pull GreenKube metrics into Grafana dashboards.

Add carbon budget checks to your pipeline:

Terminal window
CARBON=$(curl -s http://greenkube:8000/api/v1/metrics | jq '.total_carbon_grams')
if [ $(echo "$CARBON > 1000" | bc) -eq 1 ]; then
echo "Carbon budget exceeded!"
exit 1
fi

Poll recommendations and send alerts for critical findings.