A client library for accessing the Grafana HTTP API, written in Python
—
Dashboard snapshot creation and management for sharing and archiving dashboard states. Snapshots allow you to capture dashboard configurations and share them with configurable expiration times and external storage options.
Create new dashboard snapshots with flexible configuration options including expiration times and external storage.
def create_new_snapshot(self, dashboard: dict, name: Optional[str] = None,
expires: Optional[int] = None, external: Optional[bool] = None,
key: Optional[str] = None, delete_key: Optional[str] = None):
"""
Create a new dashboard snapshot.
Parameters:
dashboard (dict): Required. The complete dashboard model to snapshot
name (str, optional): Name for the snapshot
expires (int, optional): Expiration time in seconds (3600=1hr, 86400=1day).
Default is never expire
external (bool, optional): Save on external server rather than locally. Default False
key (str, optional): Define unique key. Required if external is True
delete_key (str, optional): Unique key for deletion. Required if external is True
Returns:
dict: Snapshot creation result with URL and keys
"""List and retrieve existing dashboard snapshots for viewing and management.
def get_dashboard_snapshots(self):
"""
Get list of all dashboard snapshots.
Returns:
list: List of snapshot objects with metadata
"""
def get_snapshot_by_key(self, key: str):
"""
Retrieve a specific snapshot by its key.
Parameters:
key (str): The snapshot key
Returns:
dict: Snapshot data and dashboard configuration
"""Remove snapshots using either the snapshot key or delete key for different permission levels.
def delete_snapshot_by_key(self, snapshot_id: str):
"""
Delete a snapshot using its ID/key.
Parameters:
snapshot_id (str): The snapshot identifier
Returns:
dict: Deletion result
"""
def delete_snapshot_by_delete_key(self, snapshot_delete_key: str):
"""
Delete a snapshot using its delete key (creator-only deletion).
Parameters:
snapshot_delete_key (str): The delete key for the snapshot
Returns:
dict: Deletion result
"""from grafana_client import GrafanaApi, TokenAuth
# Create API client
auth = TokenAuth(token="your-grafana-api-token")
api = GrafanaApi(auth=auth, host="your-grafana-host")
# Get a dashboard to snapshot
dashboard_data = api.dashboard.get_dashboard("dashboard-uid")
# Create a basic snapshot
snapshot_result = api.snapshots.create_new_snapshot(
dashboard=dashboard_data["dashboard"],
name="Weekly Report Snapshot"
)
print(f"Snapshot created: {snapshot_result['url']}")
print(f"Snapshot key: {snapshot_result['key']}")
print(f"Delete key: {snapshot_result['deleteKey']}")
# Create a snapshot with expiration (24 hours)
snapshot_result = api.snapshots.create_new_snapshot(
dashboard=dashboard_data["dashboard"],
name="Temporary Dashboard Snapshot",
expires=86400 # 24 hours in seconds
)
# Create an external snapshot
external_snapshot = api.snapshots.create_new_snapshot(
dashboard=dashboard_data["dashboard"],
name="External Snapshot",
external=True,
key="my-external-key",
delete_key="my-delete-key"
)# List all snapshots
snapshots = api.snapshots.get_dashboard_snapshots()
for snapshot in snapshots:
print(f"Snapshot: {snapshot['name']} - Key: {snapshot['key']}")
# Retrieve specific snapshot
snapshot_key = "AbCdEf123456"
snapshot_data = api.snapshots.get_snapshot_by_key(snapshot_key)
print(f"Retrieved snapshot: {snapshot_data['snapshot']['name']}")
# Delete snapshot by key
api.snapshots.delete_snapshot_by_key(snapshot_key)
print("Snapshot deleted")
# Delete snapshot using delete key (creator privilege)
delete_key = "delete-key-123"
api.snapshots.delete_snapshot_by_delete_key(delete_key)
print("Snapshot deleted using delete key")# Create a snapshot for sharing
dashboard_uid = "dashboard-to-share"
dashboard_data = api.dashboard.get_dashboard(dashboard_uid)
# Create snapshot with 7-day expiration
share_snapshot = api.snapshots.create_new_snapshot(
dashboard=dashboard_data["dashboard"],
name=f"Shared Dashboard - {dashboard_data['dashboard']['title']}",
expires=604800 # 7 days
)
# Share the URL
share_url = share_snapshot["url"]
print(f"Share this URL: {share_url}")
# Store delete key for later cleanup
delete_key = share_snapshot["deleteKey"]
print(f"Delete key (keep private): {delete_key}")Install with Tessl CLI
npx tessl i tessl/pypi-grafana-client@5.0.1