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/metricsPer-pod metrics with pagination
GET/api/v1/metrics/summaryAggregated cluster/namespace totals
GET/api/v1/metrics/timeseriesBucketed time-series for charts
GET/api/v1/metrics/by-namespaceCO₂e, cost, energy aggregated per namespace
GET/api/v1/metrics/top-podsTop-N pods by CO₂e for a time window
GET/api/v1/metrics/dashboard-summaryPre-computed KPI cache (fast)
GET/api/v1/metrics/dashboard-timeseries/{window_slug}Pre-computed timeseries for 24h, 7d, 30d, 1y, ytd
POST/api/v1/metrics/dashboard-summary/refreshOn-demand cache refresh
MethodPathDescription
GET/api/v1/metrics/timeseriesHistorical metrics with last, start/end, granularity
MethodPathDescription
GET/api/v1/recommendationsRun recommender engine live
GET/api/v1/recommendations/activePersisted active recommendations (?refresh=true to re-run)
GET/api/v1/recommendations/topTop-N ranked by projected annual savings — ?limit=5&metric=co2 or metric=cost
GET/api/v1/recommendations/ignoredPermanently ignored recommendations
GET/api/v1/recommendations/appliedApplied recommendations ordered by most recent application
GET/api/v1/recommendations/historyHistorical records with optional time filtering
GET/api/v1/recommendations/savingsAggregate savings by recommendation type
PATCH/api/v1/recommendations/{id}/applyMark as applied and record savings
PATCH/api/v1/recommendations/{id}/ignorePermanently ignore
DELETE/api/v1/recommendations/{id}/ignoreRestore an ignored recommendation
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:

?start=2024-01-01T00:00:00Z # Start time (ISO 8601)
&end=2024-01-31T23:59:59Z # End time (ISO 8601)
&last=7d # Relative window (e.g., 24h, 7d, 30d, ytd, 1y)
&namespace=production # Filter by namespace
&granularity=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/metrics/timeseries?namespace=production&last=7d&granularity=daily"
Terminal window
curl http://localhost:8000/api/v1/recommendations/active
Terminal window
curl "http://localhost:8000/api/v1/recommendations/top?limit=5&metric=co2"

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:
apiRateLimit: "60/minute" # slowapi format (0 = disabled)

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

config:
corsOrigins: "*" # Restrict in production, e.g. "https://my-company.internal"

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

Use the --fail-on-co2 CLI flag or poll the API:

Terminal window
# Via CLI
greenkube report --last 24h --fail-on-co2 1000
# Via API + jq
CARBON=$(curl -s "http://greenkube:8000/api/v1/metrics/summary?last=24h" | jq '.total_co2e_grams')
if [ "$(echo "$CARBON > 1000" | bc)" -eq 1 ]; then
echo "Carbon budget exceeded: ${CARBON}g CO₂e"
exit 1
fi

Poll recommendations and send alerts for critical findings.