# Creating alert rule via HTTP API

**URL:** <https://community.grafana.com/t/creating-alert-rule-via-http-api/63961>\
**Category:** Grafana Cloud\
**Tags:** api, alerting\
**Created:** [April 13, 2022, 2:34pm UTC](https://community.grafana.com/t/creating-alert-rule-via-http-api/63961 "2022-04-13T14:34:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iluanna](https://avatars.discourse-cdn.com/v4/letter/i/9de053/32.png) [@iluanna](https://community.grafana.com/u/iluanna)\
**Post date:** [April 13, 2022, 2:34pm UTC](https://community.grafana.com/t/creating-alert-rule-via-http-api/63961/1 "2022-04-13T14:34:59Z")

</div>

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](https://grafana.com/docs/grafana/latest/http_api/dashboard/)). 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.

---

<div class="post-metadata">

**Author:** ![gsuttie2001](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/gsuttie2001/32/22552_2.png) [@gsuttie2001](https://community.grafana.com/u/gsuttie2001)\
**Post date:** [April 14, 2022, 8:06am UTC](https://community.grafana.com/t/creating-alert-rule-via-http-api/63961/2 "2022-04-14T08:06:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ximenaaliaguilla](https://avatars.discourse-cdn.com/v4/letter/x/ba9def/32.png) [@ximenaaliaguilla](https://community.grafana.com/u/ximenaaliaguilla)\
**Post date:** [April 15, 2022, 1:47pm UTC](https://community.grafana.com/t/creating-alert-rule-via-http-api/63961/3 "2022-04-15T13:47:59Z")

</div>

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

- Our API for notification channels: [Alerting Notification Channels HTTP API | Grafana Labs](https://grafana.com/docs/grafana/latest/http_api/alerting_notification_channels/)
- Our OpenAPI documentation via Swagger for working with Grafana Cloud APIs that includes Grafana Alerting / AlertManager: [Swagger Editor](https://editor.swagger.io/?url=https://raw.githubusercontent.com/grafana/grafana/main/public/api-merged.json) (look for the “alertmanager” section)
- Our general guide for [using the Grafana APIs](https://grafana.com/docs/grafana/latest/http_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](http://grafana.net/) instance to authenticate usage. The API Keys can be generated from the **Configurations \> API Keys** page inside your Grafana.

---

<div class="post-metadata">

**Author:** ![gsuttie2001](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/gsuttie2001/32/22552_2.png) [@gsuttie2001](https://community.grafana.com/u/gsuttie2001)\
**Post date:** [April 16, 2022, 9:03am UTC](https://community.grafana.com/t/creating-alert-rule-via-http-api/63961/4 "2022-04-16T09:03:54Z")

</div>

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](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”  
}  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![iluanna](https://avatars.discourse-cdn.com/v4/letter/i/9de053/32.png) [@iluanna](https://community.grafana.com/u/iluanna)\
**Post date:** [April 20, 2022, 9:33am UTC](https://community.grafana.com/t/creating-alert-rule-via-http-api/63961/5 "2022-04-20T09:33:23Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![aceankit99](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/aceankit99/32/24197_2.png) [@aceankit99](https://community.grafana.com/u/aceankit99)\
**Post date:** [November 22, 2022, 10:54am UTC](https://community.grafana.com/t/creating-alert-rule-via-http-api/63961/6 "2022-11-22T10:54:54Z")

</div>

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”  
},
