# Posting an alert using Grafana 8.0 API: any worked example?

**URL:** <https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607>\
**Category:** Alerting\
**Tags:** unified-alerting, api\
**Created:** [June 28, 2021, 12:54pm UTC](https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607 "2021-06-28T12:54:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fab1](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@fab1](https://community.grafana.com/u/fab1)\
**Post date:** [June 28, 2021, 12:54pm UTC](https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607/1 "2021-06-28T12:54:03Z")

</div>

- My Grafana version: 8.0.4 through Docker WSL 2 based engine running under Win 10. Docker version 3.4.0. Docker-Engine 20.10.7

- I’d like to set up a sample alert using the Unified Alerting API 1.1.0, as documented here: [Swagger Editor](https://editor.swagger.io/?url=https://raw.githubusercontent.com/grafana/grafana/main/pkg/services/ngalert/api/tooling/post.json)

- What I have tried: I have set up an alert manually in the grafana UI, then downloaded every configuration setting I could (using GET in the API). Finally I have deleted my alert in the UI and tried to POST it back using the API. When posting, it did not work.

- Using GET, I got information from “alertmanager/grafana/config/api/v1/alerts” and “ruler/grafana/api/v1/rules}” only. I have then deleted the alert from the server, and posted exactly what I have received back to “alertmanager/grafana/config/api/v1/alerts”. This first post worked. Then I tried to post back what I had received from “ruler/grafana/api/v1/rules”. This didn’t work and I got the following error: `404 Client Error: Not Found for url: http://127.0.0.1:3000/api/ruler/grafana/api/v1/rules`. If I append a “/{Namespace}” to the request url, I get the following in the http response: b’{“message”:“folder not found”,“status”:“not-found”}’, text: ‘{“message”:“folder not found”,“status”:“not-found”}’, status: 404.  
So, I guess I need to specify a new namespace somewhere else, or what? Maybe I need to “create” a namespace within the first POST request?

- I expected the second post worked. Why did it not?

- The following is the json payload sent within the second post:

```auto
{'existing folder on server': [{'interval': '1m',
                                    'name': 'some name here',
                                    'rules': [{'annotations': {' __dashboardUid__': 'dahsboard UID here',
                                                               ' __panelId__': '2'},
                                               'expr': '',
                                               'for': '5m',
                                               'grafana_alert': {'condition': 'B',
                                                                 'data': [{'datasourceUid': 'UID received with GET',
                                                                           'model': {'format': 'time_series',
                                                                                     'group': [],
                                                                                     'intervalMs': 1000,
                                                                                     'maxDataPoints': 43200,
                                                                                     'metricColumn': 'none',
                                                                                     'rawQuery': True,
                                                                                     'rawSql': 'SELECT\n'
                                                                                               ' '
                                                                                               '$__time(time_recorded),\n'
                                                                                               ' '
                                                                                               'co2\n'
                                                                                               'FROM\n'
                                                                                               ' '
                                                                                               'airthings.measurements\n'
                                                                                               'WHERE\n'
                                                                                               ' '
                                                                                               '$__timeFilter(time_recorded)\n'
                                                                                               ' '
                                                                                               'AND '
                                                                                               'sensor_serial_num '
                                                                                               '= '
                                                                                               "'2930039276'\n"
                                                                                               'ORDER '
                                                                                               'BY '
                                                                                               'time_recorded '
                                                                                               'ASC',
                                                                                     'refId': 'A',
                                                                                     'select': [[{'params': ['value'],
                                                                                                  'type': 'column'}]],
                                                                                     'timeColumn': 'time',
                                                                                     'where': [{'name': '$__timeFilter',
                                                                                                'params': [],
                                                                                                'type': 'macro'}]},
                                                                           'queryType': '',
                                                                           'refId': 'A',
                                                                           'relativeTimeRange': {'from': 604800,
                                                                                                 'to': 0}},
                                                                          {'datasourceUid': '-100',
                                                                           'model': {'conditions': [{'evaluator': {'params': [500],
                                                                                                                   'type': 'gt'},
                                                                                                     'operator': {'type': 'and'},
                                                                                                     'query': {'params': ['A']},
                                                                                                     'reducer': {'params': [],
                                                                                                                 'type': 'avg'},
                                                                                                     'type': 'query'}],
                                                                                     'datasource': ' __expr__',
                                                                                     'hide': False,
                                                                                     'intervalMs': 1000,
                                                                                     'maxDataPoints': 43200,
                                                                                     'refId': 'B',
                                                                                     'type': 'classic_conditions'},
                                                                           'queryType': '',
                                                                           'refId': 'B',
                                                                           'relativeTimeRange': {'from': 0,
                                                                                                 'to': 0}}],
                                                                 'exec_err_state': 'Alerting',
                                                                 'id': 3,
                                                                 'intervalSeconds': 60,
                                                                 'namespace_id': 1,
                                                                 'namespace_uid': 'level39_docker',
                                                                 'no_data_state': 'NoData',
                                                                 'orgId': 1,
                                                                 'rule_group': 'CO2 '
                                                                               '@ '
                                                                               'level '
                                                                               '39 '
                                                                               ': '
                                                                               'sensor '
                                                                               '$sensor',
                                                                 'title': 'test_alert_1',
                                                                 'uid': 'uid received here from GET',
                                                                 'updated': '2021-06-24T21:46:10Z',
                                                                 'version': 2}}]}]}

```

This is instead the payload sent within the first POST:

```auto
{'alertmanager_config': {'receivers': [{'grafana_managed_receiver_configs': [{'disableResolveMessage': False,
                                                                              'name': 'email '
                                                                                      'receiver',
                                                                              'secureFields': {},
                                                                              'settings': {'addresses': '<example@email.com>'},
                                                                              'type': 'email',
                                                                              'uid': 'uid received here from GET'}],
                                        'name': 'grafana-default-email'}],
                         'route': {'receiver': 'grafana-default-email'},
                         'templates': None},
 'template_files': None}

```

- Is it possible to have a completed worked example about how to create an alert from scratch using the API? Thank you.

---

<div class="post-metadata">

**Author:** ![anon20954878](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@anon20954878](https://community.grafana.com/u/anon20954878)\
**Post date:** [July 7, 2021, 11:25pm UTC](https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607/2 "2021-07-07T23:25:02Z")

</div>

You’re on the right track with having to create a folder/namespace first. Once you have that you should be able to continue with your example

---

<div class="post-metadata">

**Author:** ![dwhayslett](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@dwhayslett](https://community.grafana.com/u/dwhayslett)\
**Post date:** [January 11, 2022, 6:48pm UTC](https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607/3 "2022-01-11T18:48:39Z")

</div>

Were you able to get this working? I’m fighting the next step in this issue (as are many others, it seems). My “existing folder on server” exists, but I get “rule group name is not valid” in response from the POST. I created the folder via “Create → Folder” dialog in our Grafana page - is that not sufficient? Thanks!

The command I’m trying to use is

```auto
curl -X POST \
       -u admin:<password> \
       -H "Content-type: application/json" \
       -d $(cat AlertingRules.json) \
http://<grafanaIP>:3000/api/ruler/grafana/api/v1/rules/AlertingRules

```

I’ve never really found explicit documentation as to whether this is correct.

---

<div class="post-metadata">

**Author:** ![pilinato](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@pilinato](https://community.grafana.com/u/pilinato)\
**Post date:** [March 15, 2022, 6:36am UTC](https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607/4 "2022-03-15T06:36:05Z")

</div>

Hi I Got same message

“rule group name is not valid”

can anyone help ?

---

<div class="post-metadata">

**Author:** ![pats](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/pats/32/22280_2.png) [@pats](https://community.grafana.com/u/pats)\
**Post date:** [March 30, 2022, 2:14pm UTC](https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607/5 "2022-03-30T14:14:10Z")

</div>

Facing the same issue.

---

<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:13am UTC](https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607/6 "2022-04-14T08:13:33Z")

</div>

> [@fab1](#):
>
> `}]}`

Why are there no replies on creating Alert rules using the API?

---

<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:43am UTC](https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607/8 "2022-04-20T09:43:21Z")

</div>

The solution for "“rule group name is not valid” is given here: [NGalert / Grafana 8 alert feature: How to export / import alerts as yml/json? - #11 by callayn](https://community.grafana.com/t/ngalert-grafana-8-alert-feature-how-to-export-import-alerts-as-yml-json/51677/11), by ahmed1smael

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/grafana/original/2X/e/ef5308cc58c9e5a13187b5012eb99f406928462f.png) [@system](https://community.grafana.com/u/system)\
**Post date:** [June 28, 2022, 12:54pm UTC](https://community.grafana.com/t/posting-an-alert-using-grafana-8-0-api-any-worked-example/49607/9 "2022-06-28T12:54:42Z")

</div>

Closed on June 28, 2022, 12:54pm UTC

This topic was automatically closed after 365 days. New replies are no longer allowed.
