Grafana API SDK
The repository includes an SDK for the Grafana API. It's possible to interact with all publicly available Grafana HTTP API endpoints.
The core features that are implemented inside this SDK:
- All public Grafana API (HTTP) endpoints are supported
- Full API support for Grafana legacy alerting, current alerting, alerting channels, and alert provisioning
- Possibility to specify custom and self-signed certificates
- HTTP/2 support
Supported Features
The following list describes the currently supported features of the Grafana API functionality.
- Admin HTTP API
- Legacy Alerting HTTP API
- Alerting HTTP API
- Alerting Notification Channels HTTP API
- Annotations HTTP API
- Authentication HTTP API
- Dashboard
- Dashboard Permissions
- Dashboard Versions
- Public Dashboard
- Data source HTTP API
- Datasource query and resource caching
- Datasource Permissions HTTP API
- Folder HTTP API
- Folder Permissions HTTP API
- Search HTTP API
- External Group Sync HTTP API
- Access control HTTP API
- HTTP Preferences API
- HTTP Snapshot API
- Library Element HTTP API
- Query History API
- Licensing HTTP API
- Organization HTTP API
- Other HTTP API
- Playlist HTTP API
- Reporting API
- Short URL HTTP API
- SSO Settings
- Team HTTP API
- Team Sync
- User HTTP API
- Service Account HTTP API
- RBAC HTTP API
- Correlations API
- Alerting Provisioning HTTP API
Installation
Please be aware to not install the grafana-api
and grafana-api-sdk
packages in parallel and the same environment. This result in name clashes, and it's not possible to use the Grafana API SDK.
pip install grafana-api-sdk
Example
import json
from grafana_api.model import APIModel
from grafana_api.dashboard import Dashboard
model: APIModel = APIModel(host="test", token="test")
dashboard: Dashboard = Dashboard(model)
with open("/tmp/test/test.json") as file:
json_dashboard = json.load(file)
dashboard.create_or_update_dashboard(message="Create a new test dashboard", dashboard_json=json_dashboard, dashboard_path="test")
TLS/ mTLS for HTTP/1.1
It is possible to pass a custom ssl_context to the underlying library to perform the requests to the HTTP API. For this step and to support custom TLS/ mTLS, there is an option to inject the Python ssl_context. More information can be found here and a dummy TLS/ mTLS implementation below.
TLS
import ssl
from grafana_api.model import APIModel
ssl_ctx = ssl.create_default_context(
ssl.Purpose.SERVER_AUTH,
cafile="/test/path/ca.crt"
)
ssl_ctx.verify_mode = ssl.CERT_REQUIRED
model: APIModel = APIModel(host="test", token="test", ssl_context=ssl_ctx)
mTLS
import ssl
from grafana_api.model import APIModel
ssl_ctx = ssl.create_default_context(
ssl.Purpose.SERVER_AUTH,
cafile="/test/path/ca.crt",
)
ssl_ctx.verify_mode = ssl.CERT_REQUIRED
ssl_ctx.load_cert_chain(certfile="/test/path/client.crt", keyfile="/test/path/client.key")
model: APIModel = APIModel(host="test", token="test", ssl_context=ssl_ctx)
TLS/ mTLS for HTTP/2
It is possible to pass a custom HTTP/2 conform ssl_context to the underlying library to perform the requests to the HTTP API. For this step and to support custom TLS/ mTLS, there is an option to create the ssl_context by the execution of httpx.create_ssl_context()
function. More information can be found here and a dummy TLS/ mTLS implementation below.
TLS
import httpx
from grafana_api.model import APIModel
ssl_ctx = httpx.create_ssl_context(
verify="/test/path/ca.crt",
http2=True,
)
model: APIModel = APIModel(host="test", token="test", ssl_context=ssl_ctx)
mTLS
import httpx
from grafana_api.model import APIModel
ssl_ctx = httpx.create_ssl_context(
cert=("/test/path/client.crt", "/test/path/client.key"),
verify="/test/path/ca.crt",
http2=True,
)
model: APIModel = APIModel(host="test", token="test", ssl_context=ssl_ctx)
Templating
If you want to template your JSON document based on a predefined folder structure you can check out one of my other project and integrate the functionality inside your code.