docs/sources/as-code/observability-as-code/foundation-sdk/_index.md
The Grafana Foundation SDK is a set of tools, types, and libraries that let you define Grafana dashboards and resources using strongly typed code. By writing your dashboards as code, you can:
The SDK supports multiple programming languages, including Go, TypeScript, Python, PHP, and Java, so you can choose the one that best fits your development environment. Refer to the Grafana Foundation SDK GitHub repository for further details.
{{< youtube id="_OKQoABmg0Q" >}}
Here's a quick overview of how the Grafana Foundation SDK works:
DashboardBuilder, then add panels, queries, and other components step by step.As you build more advanced dashboards, you’ll work with additional builders and types to support richer functionality. The SDK supports not just panels and queries, but also variables, thresholds, field overrides, transformations, and more. Refer to the full API reference to explore what's possible.
Refer to Foundation SDK key concepts for a more extensive explanation, and read on to see these concepts in action.
Ensure you have the following prerequisites:
npm or yarn for TypeScript or pip for Python.To install the Foundation SDK:
docker-compose stack.go or typescript tab to view instructions to install the SDK. For other languages, refer to the Grafana Foundation SDK documentation for installation instructions.{{< code >}}
go get github.com/grafana/grafana-foundation-sdk/go@latest
npm install @grafana/grafana-foundation-sdk
{{< /code >}}
See the following examples in Go and Typescript to create a simple dashboard:
This code defines a dashboard titled “My Dashboard” with a two panels:
testdata data source random_walk scenario.{{< code >}}
package main
// Import the appropriate Grafana Foundation SDK packages
import (
"encoding/json"
"log"
"github.com/grafana/grafana-foundation-sdk/go/cog"
"github.com/grafana/grafana-foundation-sdk/go/common"
"github.com/grafana/grafana-foundation-sdk/go/dashboard"
"github.com/grafana/grafana-foundation-sdk/go/stat"
"github.com/grafana/grafana-foundation-sdk/go/testdata"
"github.com/grafana/grafana-foundation-sdk/go/timeseries"
)
func main() {
// Define a data source reference for our testdata data source
testdataRef := dashboard.DataSourceRef{
Type: cog.ToPtr("grafana-testdata-datasource"),
Uid: cog.ToPtr("testdata"),
}
// Define our dashboard as strongly typed code
builder := dashboard.NewDashboardBuilder("My Dashboard").
WithPanel(
stat.NewPanelBuilder().
Title("Version").
Datasource(testdataRef).
ReduceOptions(common.NewReduceDataOptionsBuilder().
Calcs([]string{"lastNotNull"}).
Fields("/.*/")).
WithTarget(
testdata.NewDataqueryBuilder().
ScenarioId("csv_content").
CsvContent("version\nv1.2.3"),
),
).
WithPanel(
timeseries.NewPanelBuilder().
Title("Random Time Series").
Datasource(testdataRef).
WithTarget(
testdata.NewDataqueryBuilder().
ScenarioId("random_walk"),
),
)
// Build the dashboard - errors in configuration will be thrown here
dashboard, err := builder.Build()
if err != nil {
log.Fatalf("failed to build dashboard: %v", err)
}
// Output the generated dashboard as JSON
dashboardJson, err := json.MarshalIndent(dashboard, "", " ")
if err != nil {
log.Fatalf("failed to marshal dashboard: %v", err)
}
log.Printf("Dashboard JSON:\n%s", dashboardJson)
}
// Import the appropriate Grafana Foundation SDK packages
import * as common from '@grafana/grafana-foundation-sdk/common';
import * as dashboard from '@grafana/grafana-foundation-sdk/dashboard';
import * as stat from '@grafana/grafana-foundation-sdk/stat';
import * as testdata from '@grafana/grafana-foundation-sdk/testdata';
import * as timeseries from '@grafana/grafana-foundation-sdk/timeseries';
// Define a data source reference for our testdata data source
const testDataRef: dashboard.DataSourceRef = {
type: 'grafana-testdata-datasource',
uid: 'testdata',
};
// Define our dashboard as strongly typed code
const builder = new dashboard.DashboardBuilder('My Dashboard')
.withPanel(
new stat.PanelBuilder()
.title('Version')
.reduceOptions(new common.ReduceDataOptionsBuilder().calcs(['lastNotNull']).fields('/.*/'))
.datasource(testdataRef)
.withTarget(new testdata.DataqueryBuilder().scenarioId('csv_content').csvContent('version\nv1.2.3'))
)
.withPanel(
new timeseries.PanelBuilder()
.title('Random Time Series')
.datasource(testdataRef)
.withTarget(new testdata.DataqueryBuilder().scenarioId('random_walk'))
);
// Build the dashboard - errors in configuration will be thrown here
const dashboard = builder.build();
// Output the generated dashboard as JSON
console.log(JSON.stringify(dashboard, null, 2));
{{< /code >}}
After you've defined your dashboard as code, build the final dashboard representation using the dashboard builder, typically using the build() function depending on language choice, and output the result as a JSON.
With the JSON payload, you can:
If you want to explore further and see a more real-world example of using the Grafana Foundation SDK, watch the following walkthrough:
{{< youtube id="ZjWdGVsrCiQ" >}}
In this video, we generate a dashboard from code and deploy it using the Grafana API, covering patterns and practices you'd use in production environments. It also includes a working example of a web service that emits metrics and logs, and shows how to deploy a dashboard alongside it using Docker Compose.
You can find the full source code for this example in the intro-to-foundation-sdk repository.
Now that you understand the basics of using the Grafana Foundation SDK, here are some next steps: