Grafana Notification Templates

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Checked on 2026-11-09 against Grafana 13.3.x. Tested with Grafana Enterprise 13.3.0 in the hands-on lab: the lab’s template group bookshelf renders the title and message of the webhook notifications; the HCL passes terraform validate with provider 4.54.0.

To do this by hand, follow Send Notifications.

The default Grafana notification is accurate and verbose. That is usually fine, until the person who reads it at three in the morning needs the runbook link in the first line, or the receiving system needs a payload of a different shape. A notification template changes the text that a contact point sends: the title or subject, the message body, and for webhooks the whole JSON document. Templates never change which alerts fire or where they are routed; they only format the message once routing has finished. What belongs in a good message in the first place is discussed in Designing Actionable Alerts and Notifications and Incident Response; this page is the Grafana mechanism. The contact points that consume templates are described in Contact Points and Integrations.

Three kinds of template, one language

Grafana uses the same template language, Go’s text/template, in three places, with different data in each.

Where Set on What it is for

Annotations

The alert rule

Per-alert information (summary, description) computed from the query result. Included in notifications by default.

Labels

The alert rule

Derived or dynamic labels that change alert identity, grouping and routing.

Notifications

The contact point

The final message for a whole group of alerts sent to one destination.

The data differs. In annotation and label templates the variables are the alert’s query data ($labels, $values); in a notification template, dot (.) is the notification data, which describes a group of firing and resolved alerts. The official guidance is to keep per-alert detail in annotations, written once next to the rule, and to use notification templates to arrange it, rather than computing extra information at notification time. Label and annotation templates are covered in Labels, Annotations and Rule Templates. Labels must not print query values, because that creates a new alert instance for every value.

The template language in five minutes

A template is text with actions between double braces. Everything outside the braces is copied as is.

Construct Meaning

{{ .Status }}

Print a value. Dot (.) is a cursor: it starts as the notification data and changes inside range, with and when another template is called with an argument.

{{ $x := .CommonLabels.alertname }}

A variable, created inside the template (not shared between templates).

{{ if }}...{{ else if }}...{{ else }}...{{ end }}

Conditional output. with is like if but also sets dot to the value inside the block.

{{ range .Alerts.Firing }}...{{ end }}

Loop over a list or map. Inside the loop dot is the current element. Supports else for the empty case, index and value variables, break and continue.

{{ define "name" }}...{{ end }} and {{ template "name" . }}

Define a reusable named template and run it with an argument that becomes its dot.

{{- ... -}}

Trim whitespace and line breaks before or after the action. Without trimming, indentation and newlines appear in the message.

{{/* comment */}}

A comment.

Functions and operators include and, or, not, index, len, slice, call, html, js, urlquery, print, printf, println and the comparisons eq, ne, lt, le, gt and ge. Grafana adds string and time helpers, listed below. Two traps are worth knowing now: a template name must not reuse the name of a default template, because Grafana does not report duplicates, and the meaning of dot changes inside range, so a reference to the top-level data from inside a loop needs a variable saved before the loop ({{ $status := .Status }}).

The notification data

In a notification template dot holds the following.

Name Type Meaning

.Receiver

string

Name of the contact point sending the notification.

.Status

string

firing if at least one alert is firing, otherwise resolved.

.Alerts

list of Alert

All firing and resolved alerts in the notification; .Alerts.Firing and .Alerts.Resolved select one state.

.GroupLabels

KV

The labels the group was built from (the group-by option of the notification policy).

.CommonLabels, .CommonAnnotations

KV

Labels and annotations common to all alerts in the notification.

.ExternalURL

string

Link to Grafana (or to the external Alertmanager that sent the notification).

.GroupKey

string

Identifies the group.

.TruncatedAlerts

integer

Number of truncated alerts; supported by the webhook and OnCall integrations.

Each Alert has Status, Labels, Annotations, StartsAt, EndsAt (set only when the end is known), GeneratorURL and Fingerprint. For Grafana-managed alerts it also has DashboardURL and PanelURL (present when the rule carries the dashboard UID and panel ID annotations), SilenceURL, Values (the values of the expressions used in the condition), ValueString and OrgID. A KV is a set of string pairs: access a value with dot notation (.CommonLabels.grafana_folder), and use the methods SortedPairs, Names, Values and Remove. A Time is Go’s time.Time.

This model explains the difference between group-level and instance-level data. .CommonLabels is the intersection of the labels of every alert in the group: with five Bookshelf alerts in one group it still holds application, but .CommonLabels.instance is empty because the instances differ. Anything specific to one alert (its own summary, its value, its runbook) has to be read inside a range over .Alerts.

Source: Grafana: Notification template reference (sections #alert, #kv and #time).

Functions

Grafana adds these functions to the Go built-ins.

Function Behaviour

title, toUpper, toLower, trimSpace

String case and whitespace.

match, reReplaceAll

Regular-expression test and replace.

join, stringSlice

Join a list with a separator; build a list of strings.

safeHtml

Mark a string as HTML that must not be escaped.

date, tz

Format a time with Go’s layout; convert it to a time zone such as Europe/Paris.

Namespaced functions exist as well: coll.Dict, coll.Slice, coll.Append (build maps and lists), data.JSON, data.ToJSON, data.ToJSONPretty (parse and serialise JSON), tmpl.Exec, tmpl.Inline (run a named template or a string as a template and return the text) and time.Now. The reference notes that they are not yet generally available in Grafana Cloud; on a self-managed 13.3 they are what makes a custom webhook payload practical.

Creating and using a template group

Templates are written in template groups. A group has a name, and its content may define several templates.

  1. Open Alerts & IRM, Alerting, Notification configuration, the Templates tab, and click + New notification template.

  2. Enter a name for the group and write the content. If the content has no define, Grafana wraps it in a {{ define "<name>" }} block using the group name; several templates in one group each need their own define and end.

  3. Save.

Each defined name must be unique across all groups, and the default names default.title, default.message, __subject, __text_values_list and __text_alert_list should not be reused. To use a template, open a contact point, click a field that accepts templates (title, subject, message, a webhook’s custom payload, …​) under Optional settings, click Edit, then either Select notification template or Enter custom message and write {{ template "bookshelf.message" . }}. The same template can serve several contact points, and a template belongs to no integration in particular. Without a template the integration uses the defaults default.title and default.message.

Preview before you save

The editor can render the template against sample data before it is saved, which is the single most useful feature when writing one. The preview starts from a default payload; use Select alert instances to add real alert instances, or Add custom data to add alerts with chosen labels, annotations, dashboard or panel, firing or resolved, then Refresh preview. The rendered output and the payload data are shown; fix every error before saving, because a broken template fails when a notification is sent, not when it is saved. Preview works only with the Grafana Alertmanager. In Grafana Cloud a change can take one to two minutes to take effect and the number of templates is limited by default (30).

A Bookshelf title and message

The first template gives every notification a compact title (for example [FIRING:2] Bookshelf high 5xx ratio), and the second a message that lists each firing alert with the links a responder needs.

{{ define "bookshelf.title" -}}
[{{ .Status | toUpper }}{{ if gt (len .Alerts.Firing) 0 }}:{{ len .Alerts.Firing }}{{ end }}] {{ .GroupLabels.alertname }} ({{ .CommonLabels.application }})
{{- end }}

{{ define "bookshelf.message" -}}
{{ len .Alerts.Firing }} firing, {{ len .Alerts.Resolved }} resolved
{{ range .Alerts.Firing }}
- {{ .Labels.alertname }} [{{ .Labels.severity }}]: {{ .Annotations.summary }}
  Runbook:   {{ .Annotations.runbook_url }}
  Dashboard: {{ .DashboardURL }}
  Silence:   {{ .SilenceURL }}
{{ end }}
{{- range .Alerts.Resolved }}
- RESOLVED {{ .Labels.alertname }} (started {{ .StartsAt.Format "15:04:05 MST" }})
{{ end }}
{{- end }}

In the integration, set the title or subject to {{ template "bookshelf.title" . }} and the message to {{ template "bookshelf.message" . }}. .GroupLabels.alertname is empty when the policy does not group by alertname, and the annotation runbook_url prints nothing when the rule has none; both are silent failures, so keep the rule annotations complete (see Labels, Annotations and Rule Templates).

Reference: Grafana: Notification template examples (firing and resolved alerts, a notification title, runbook and Grafana-managed links) and the reference for the fields used.

A message can also loop over the labels of an alert without naming them, which is handy when the label set varies.

{{ define "bookshelf.labels" -}}
{{ range .Labels.SortedPairs }}- {{ .Name }} = {{ .Value }}
{{ end }}
{{- end }}

Formatting per integration

A template produces text; how that text is displayed is up to the integration. Slack treats *bold* and _italic_ as formatting, other integrations render Markdown or HTML or nothing, and emoji work in titles and messages. Layout beyond that is fixed: HTML and CSS cannot be added to e-mail, Slack and Teams layouts cannot be changed (Slack blocks and Teams adaptive cards are generated by Grafana), and a webhook’s structure and headers can be changed only through its custom payload and configuration. Check the page of each integration before relying on formatting. A practical consequence: write the shared template as plain text with a line per fact, and let each integration’s title or subject field carry the only channel-specific part.

Source: Grafana: Template notifications (limitations) and the integration pages linked from Contact Points and Integrations.

A custom webhook payload

The webhook integration can replace its whole body with a template: Custom Payload holds the Payload Template and optional Payload Variables, which are available in the template as .Vars.<name>. When a custom payload is set the integration’s Title and Message are ignored. The feature is not generally available in Grafana Cloud. The template must produce valid JSON; the namespaced functions build it from maps and serialise it.

{{ define "bookshelf.webhook.payload" -}}
{{ coll.Dict
  "status" .Status
  "service" .CommonLabels.application
  "environment" .Vars.environment
  "firing" (len .Alerts.Firing)
  "resolved" (len .Alerts.Resolved)
  "title" (tmpl.Exec "default.title" .)
  "message" (tmpl.Exec "default.message" .)
  | data.ToJSONPretty " " }}
{{- end }}

With a payload variable environment = lab the lab receiver gets a small, stable document instead of the full default payload. A receiver that parses it can rely on five keys rather than on the nested structure of alerts.

Reference: Grafana: Webhook custom payload and the example "Custom JSON payload" on Grafana: Notification template examples.

Images in notifications

Grafana can attach a screenshot of the alert’s panel, so the recipient sees why it fired. It works only for alerts associated with a panel, identified by the rule’s dashboard UID and panel ID annotations. At most two screenshots are taken per alert (when it fires and when it resolves) and are not refreshed in between; the count, size and placement cannot be changed. It needs the Grafana image renderer and these settings in [unified_alerting.screenshots]: capture (enable), upload_external_image_storage (upload to remote storage, otherwise the images live on disk for up to temp_data_lifetime) and max_concurrent_screenshots (default 5, to be no higher than concurrent_render_request_limit).

Support varies. Some integrations upload the file (Discord up to 10, e-mail embedded, Pushover one, Slack with bot tokens up to 5, Telegram); others reference an image in cloud storage (Discord, e-mail, Google Chat, Microsoft Teams, Opsgenie, PagerDuty, Slack webhooks, webhook); several support none (DingDing, Kafka, Line, MQTT, Alertmanager, Sensu Go, Threema, VictorOps). Screenshots are not supported for Mimir or Loki rules, for alerts sent to external Alertmanagers, or with custom templates in e-mail. Grafana does not delete screenshots from cloud storage or from its internal server, so a retention policy is needed. The page documents no release status, and Grafana Cloud users must ask support to enable it. The metrics grafana_screenshot_successes_total and grafana_screenshot_failures_total show whether capture works. The webhook payload then carries imageURL for each alert.

Templates as code

Template groups can be provisioned from files, with Terraform or the HTTP API (the methods and export are in Provisioning Alerting Resources). In a provisioning file the template value is the text of the group. Provisioning does not interpolate the template group name or content, so a $ inside a template is written as is; only a template placed in a contact point setting needs a $ written $$.

apiVersion: 1

templates:
  - orgId: 1
    name: bookshelf
    template: |
      {{ define "bookshelf.title" -}}
      [{{ .Status | toUpper }}] {{ .GroupLabels.alertname }} ({{ .CommonLabels.application }})
      {{- end }}

deleteTemplates: []
resource "grafana_message_template" "bookshelf" {
  name     = "bookshelf"
  template = <<-EOT
    {{ define "bookshelf.title" -}}
    [{{ .Status | toUpper }}] {{ .GroupLabels.alertname }} ({{ .CommonLabels.application }})
    {{- end }}
  EOT
}

A limit worth knowing: alertmanager_max_template_output_bytes in [unified_alerting] (default 10 MB) truncates the expansion of a single template expression and logs a warning.

What can go wrong

  • A parse or execution error. It shows up when a notification is rendered, not when the group is saved; preview first and watch the delivery status of the contact point.

  • Empty values. .CommonLabels.instance or .GroupLabels.alertname is empty when alerts in the group differ or the policy does not group by it. Read per-alert data inside range.

  • Dot changed inside range. Save .Status or .CommonLabels in a variable before the loop.

  • Name clash. Defining a template with the name of a default template silently changes every notification that uses it.

  • Untrimmed whitespace. Blank lines and indentation appear in Slack and e-mail; use {{- and -}}.

  • Secrets or personal data in messages. Annotations are copied into every notification; keep them out of labels and annotations.

  • Expecting HTML or layout control. E-mail, Slack and Teams layouts are fixed.

  • Custom payload used in Grafana Cloud. It is not generally available there.

In the books

McCollam (Grafana 8) introduces message templates with a conditional on .Alerts.Firing that prints different text for one alert and for more than ten, and notes that the contact point’s own message field uses the same variables and functions; the same data model still applies. What the book predates: template groups and the preview editor, the namespaced coll, data, tmpl and time functions, the custom webhook payload, images in notifications and provisioning of templates.

Verify in the lab

  1. Create the template group bookshelf with bookshelf.title and bookshelf.message from this page. Open the preview, add custom data with alertname Bookshelf high 5xx ratio, a summary and a runbook_url annotation, and check the output.

  2. Set the title (or the webhook’s Title and Message) of the bookshelf-webhook contact point to the two templates.

  3. Start the load generator in failing-request mode. In the receiver output read the title and message fields and confirm the runbook, dashboard and silence links.

  4. Add Custom Payload with bookshelf.webhook.payload and the variable environment = lab; fire the alert again and confirm that the receiver now gets only status, service, environment, firing, resolved, title and message.

  5. Break the template on purpose (remove an end), watch the preview report the error, then fix it.

  6. Fire two alerts that share a group and check that .CommonLabels shows only the shared labels while the per-alert lines come from the range.