Update an existing alert rule via Alerting provisioning API

Hi everyone :wave:

Grafana version: 9.0.2 and operating system: Ubuntu

I’m trying to change the name of an existing alert rule via Alerting provisioning API.
In documentation I found this method:

Here is my Python code:

login = 'admin'
pswrd = 'admin'
auid = 'some_alert_unique_id'
data = {
   'title':'New alert title'
}
url = f'http://{login}:{pswrd}@grafana.localhost:8000/api/v1/provisioning/alert-rules/{auid}'
r = requests.put(url, data = data)

print(r.text)
print(r.status_code)

When I execute this I am getting:

{"message":"bad request data","traceID":"00000000000000000000000000000000"}
400

I think that I am just don’t fully understand how to use requests library, I mean how to PUT things correctly, but GET method works totally fine and I receive the JSON of the alert rule.

I’ve searched related topics on the forum but didn’t find anything (nor on GitHub).

1 Like

I figured it by myself :slight_smile:

The problem was that I tried to change just a part of an alert, which was kinda intuitive for me, but not appropriate for Provisioning API. Instead, I should have specified the whole configuration of an alert, not only 'title'.

So, the 'data' JSON must look like this:

data = {
    'id': 294,
    'uid': auid,
    'orgID': 1,
    'folderUID': 'Your folder UID',
    'ruleGroup': 'Your rule group',
    'title': 'Your alert title',
    'condition': 'A',
    'data':[
        {
            'refId': 'The name of query',
            'queryType': 'sql',
            'relativeTimeRange': {'from': 18221112, 'to': 0},
            'datasourceUid': '7eMUser7k',
            'model': {
                'format': 0,
                'intervalMs': 1000,
                'maxDataPoints': 43200,
                'meta': {
                    'builderOptions': {
                        'fields': [], 
                        'limit': 100, 
                        'mode': 'list'
                    }
                },
                'queryType': 'sql',
                'rawSql': "select 'Hi' ",
                'refId': 'The name of query'
            }
        },
        {
            'refId': 'A',
            'queryType': '',
            'relativeTimeRange': {'from': 0, 'to': 0},
            'datasourceUid': '-100',
            'model': {
                'conditions': [
                    {
                        'evaluator': {'params': [-3], 'type': 'lt'},
                        'operator': {'type': 'and'},
                        'query': {'params': ['The name of query']},
                        'reducer': {'params': [], 'type': 'last'},
                        'type': 'query'
                    }
                ],
            'datasource': {'type': '__expr__', 'uid': '-100'},
            'hide': False,
            'intervalMs': 1000,
            'maxDataPoints': 43200,
            'refId': 'A',
            'type': 'classic_conditions'}}],
    'noDataState': 'OK',
    'execErrState': 'OK',
    'for': 0,
    'annotations': {
        '__dashboardUid__': 'dashbourd_uid', 
        '__panelId__': 'panel_id'
    },
    'labels': {
        'some': 'label'
    }
}

When I tried to PUT the whole structure it worked and an alert appeared in Grafana UI.
As not to get confused with this big JSON, I just have sent a GET request to get this JSON, changed what I needed to change and did a PUT request to the same url.

So, basically your are not “updating an existing alert rule” as it is said in documentation, but you are recreating it, because the process of creating of new alert is almost the same, but instead of PUT you are doing POST and without alert UID in the url.