Add support for variables in plugins

DevelopersAdd support for variables

Variables are placeholders for values, and can be used to create things like templated queries and dashboard or panel links. For more information on variables, refer to Templates and variables.

This guide explains how to leverage template variables in your panel plugins and data source plugins.

We’ll see how you can turn a string like this:

SELECT * FROM services WHERE id = "$service"


SELECT * FROM services WHERE id = "auth-api"

Grafana provides a couple of helper functions to interpolate variables in a string template. Let’s see how you can use them in your plugin.

Interpolate variables in panel plugins

For panels, the replaceVariables function is available in the PanelProps.

Add replaceVariables to the argument list, and pass it a user-defined template string.

export const SimplePanel: React.FC<Props> = ({ options, data, width, height, replaceVariables }) => {
  const query = replaceVariables('Now displaying $service')

  return <div>{ query }</div>

Interpolate variables in data source plugins

For data sources, you need to use the getTemplateSrv, which returns an instance of TemplateSrv.

  1. Import getTemplateSrv from the runtime package.

    import { getTemplateSrv } from '@grafana/runtime';
  2. In your query method, call the replace method with a user-defined template string.

    async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
      const query = getTemplateSrv().replace('SELECT * FROM services WHERE id = "$service"'), options.scopedVars);
      const data = makeDbQuery(query);
      return { data };