Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
Set up Grafana CLI
You can configure Grafana CLI in two ways: using environment variables or through a configuration file.
- Environment variables are ideal for CI environments and support a single context.
- Configuration files can manage multiple contexts, making it easier to switch between different Grafana instances.
Use environment variables
Grafana CLI communicates with Grafana via its REST API, which requires authentication credentials.
At a minimum, set the URL of your Grafana instance and the organization ID:
GRAFANA_SERVER='http://localhost:3000' GRAFANA_ORG_ID='1' grafanactl config checkDepending on your authentication method, you may also need to set:
- A token for a Grafana service account (recommended)
- A username and password for basic authentication
To persist your configuration, consider creating a context.
A full list of supported environment variables is available in the reference documentation.
Define contexts
Contexts allow you to easily switch between multiple Grafana instances. By default, the CLI uses a context named default.
To configure the default context:
grafanactl config set contexts.default.grafana.server http://localhost:3000
grafanactl config set contexts.default.grafana.org-id 1
# Authenticate with a service account token
grafanactl config set contexts.default.grafana.token service-account-token
# Or use basic authentication
grafanactl config set contexts.default.grafana.user admin
grafanactl config set contexts.default.grafana.password adminYou can define additional contexts in the same way:
grafanactl config set contexts.staging.grafana.server https://staging.grafana.example
grafanactl config set contexts.staging.grafana.org-id 1Note
In these examples,
defaultandstagingare the names of the contexts.
Configuration file
Grafana CLI stores its configuration in a YAML file. The CLI determines the configuration file location in the following order:
- If the
--configflag is provided, the specified file is used. - If
$XDG_CONFIG_HOMEis set:$XDG_CONFIG_HOME/grafanactl/config.yaml - If
$HOMEis set:$HOME/.config/grafanactl/config.yaml - If
$XDG_CONFIG_DIRSis set:$XDG_CONFIG_DIRS/grafanactl/config.yaml
Note
Use
grafanactl config checkto display the configuration file currently in use.
Useful commands
Check the current configuration:
grafanactl config checkNote
This command is useful to troubleshoot your configuration.
List all available contexts:
grafanactl config list-contextsSwitch to a specific context:
grafanactl config use-context stagingView the full configuration:
grafanactl config view


