Creating alert rule via HTTP API

Hey,
I’m trying to create alert rule via http api. I read that it can be done using dashboard http api, by modifying dashboard json (like it’s said on this website: Dashboard HTTP API | Grafana Labs). I’ve tried it, but without succes. I can add new panel, but not an alert rule.

Also when I add an alert rule to dashboard panel manually (from the panel edition level), it doesn’t appear in the dashboard json.

I’m using Grafana Cloud.

Does anybody know if it’s possible to create an alert rule remotly, via HTTP and how to do that? Many thanks for help.

I have the same problem - does anyone have a working sample?

Yes this is possible. Here are our resources for creating alerting rules rules via API:

These API endpoints should be compatible with deployment versioning/instrumentation tools like Terraform. Be sure that you use an API Key created from inside your grafana.net instance to authenticate usage. The API Keys can be generated from the Configurations > API Keys page inside your Grafana.

So the docs are pretty poor in this area tbh - so here is a working sample.

Json for an alert is below:- make sure to change the URL to post to which for me was http://grafana.staged-by-discourse.com/api/ruler/grafana/api/v1/rules/demo

demo was the name of the folder where the dashboard is

{
“name”: “Azure SQL - CPU %”,
“interval”: “1m”,
“rules”: [
{
“expr”: “”,
“for”: “5m”,
“labels”: {
“Customer”: “cloud adventure”,
“alertto”: “gregor”
},
“annotations”: {
“summary”: “Azure SQL - CPU %”
},
“grafana_alert”: {
“id”: 119,
“orgId”: 27,
“title”: “Azure SQL - CPU %”,
“condition”: “B”,
“data”: [
{
“refId”: “A”,
“queryType”: “Azure Monitor”,
“relativeTimeRange”: {
“from”: 600,
“to”: 0
},
“datasourceUid”: {{datasourceUid}},
“model”: {
“azureMonitor”: {
“aggregation”: “Average”,
“alias”: “{{ resourcename }} - {{ metric }}”,
“dimensionFilters”: ,
“metricDefinition”: “Microsoft.Sql/servers/databases”,
“metricName”: “cpu_percent”,
“metricNamespace”: “Microsoft.Sql/servers/databases”,
“resourceGroup”: “rg-grafanaresources”,
“resourceName”: “grafanadb/grafanadb”,
“timeGrain”: “auto”
},
“hide”: false,
“intervalMs”: 1000,
“maxDataPoints”: 43200,
“queryType”: “Azure Monitor”,
“refId”: “A”,
“subscription”: {{SubscriptionId}}
}
},
{
“refId”: “B”,
“queryType”: “”,
“relativeTimeRange”: {
“from”: 0,
“to”: 0
},
“datasourceUid”: “-100”,
“model”: {
“conditions”: [
{
“evaluator”: {
“params”: [
75
],
“type”: “gt”
},
“operator”: {
“type”: “and”
},
“query”: {
“params”: [
“A”
]
},
“reducer”: {
“params”: ,
“type”: “last”
},
“type”: “query”
}
],
“datasource”: {
“type”: “expr”,
“uid”: “-100”
},
“hide”: false,
“intervalMs”: 1000,
“maxDataPoints”: 43200,
“refId”: “B”,
“type”: “classic_conditions”
}
}
],
“intervalSeconds”: 60,
“rule_group”: “Azure SQL - CPU %”,
“no_data_state”: “NoData”,
“exec_err_state”: “Alerting”
}
}
]
}

1 Like

Hi, I’m following what you say, but I get a response message: folder not found. Instead of your “demo” I put the folder name where my alert rules are, not the dashboards (but even when I put dashboards folder, the response is the same).

Hi @gsuttie2001. You have used “gt” for greater than…so I suppose “lt” will be less than…what will be the code for no data? I cant find anything related to that in the documentation.

“evaluator”: {
“params”: [
75
],
“type”: “gt”
},