Provision Alert Rules via API

I’d like to extract my configured alert-rules from a self-hosted Grafana instance and port them over to another instance for initial provisioning. Based on these I’d like to continue working on the alerts, so it should be possible to edit the rules via UI on the new instance.

  • What Grafana version and what operating system are you using?

    • Grafana self-hosted v10.4.2
  • What are you trying to achieve?

    • Export data of existing rule-groups into portable format via API or UI
    • Export data of existing alert-rules into portable format via API or UI
    • Add or update an rule-group via API
    • Add or update an alert-rule via API
  • How are you trying to achieve it?

    • Either export the data using the “Export”-functionality in the Grafana UI or using the REST API and store the result in a JSON file.
    • On a new Grafana instance read from the JSON file and add/update the data via REST API
    • Using the API via curl (for testing purposes) and later via Ansible (for deployment automation)
  • What happened?

    • Export via UI seems to result in different data than the REST API. This however is expected and warned for in the docs.
    • Export via API does not contain all the data needed, especially in regards to rule-groups. What do I miss here?
    • API is deprecated without any non-deprecated API replacing it. Or do I overlook something?
    • API does not return a portable format, as the folderUid differs between Grafana instances. I’d like to transfer an alert-rule from one Grafana instance to another though (staging to production, dev branch to master branch, …). Both instances have the same folder names, but the ids differ. How do I solve that in a good way?
    • When adding/updating via API I very often simply get “400 - bad request data”, but there is not way to figure out what was wrong (missing attributes, malformed data, …). It’s very hard to figure out what exactly is wrong without a lot of experimentation. Is there a way to get more details about failing requests? Perhaps from the Grafana server logs?
  • What did you expect to happen?

    • A way to simple use the Grafana API to transfer alerts between grafana instances
    • Docs giving answers to the above questions to allow me to work with the API in an efficient way
  • Can you copy/paste the configuration(s) that you are having problems with?

    • Let me know if there’s anything I can paste here to help answering the questions.
  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.

    • A s stated above, nothing helpful.
  • Did you follow any online instructions? If so, what is the URL?

    • Provision via File: Not suitable to my usecase because user needs to be able to modify alert rules after initial provisioning
    • Grafana API docs: Not really helpful, as there’s not enough detail to answer my above questions
    • Grafana Swagger docs: Not really helping as well, even less detail than the API docs
    • Found a few forum posts, but nothing really helpful on that matter.
    • Found a few blog posts from thirdparties, but nothing useful as well

I suggest using the Terraform provider. From the UI, you can also export the resource in Terraform format.

To allow edits from the UI., enable the disable_provenance property

Folders should also be provisioned, and linked in alerting resources using tf variables like folder_uid = grafana_folder.test_folder.uid

Here a few working examples: provisioning-alerting-examples/terraform at main · grafana/provisioning-alerting-examples · GitHub

Which API to use?
The deprecation notice only applies to the legacy alerting API. The correct current API is /api/v1/provisioning/ and it is not deprecated.

Goal Endpoint
Export rule group GET /api/v1/provisioning/folder/{folderUID}/rule-groups/{group}
Import rule group PUT /api/v1/provisioning/folder/{folderUID}/rule-groups/{group}
List all rules GET /api/v1/provisioning/alert-rules

UI export vs API export? Ignore the UI export → it produces file-provisioning YAML, not suitable for API import. Always export via API directly.
folderUID and datasourceUID differ between instances? Resolve both by name on the target before importing:

Why 400 errors? Two causes:
Rule uid present in payload → Grafana tries to update a non-existent rule and fails
folderUID or datasourceUID doesn’t exist on target
Always strip id, uid, provenance before importing. Never replace datasourceUid: "-100" — it’s Grafana’s built-in expression engine.

How to make rules editable in UI after import?
Add X-Disable-Provenance: true header to the PUT request:

curl -s -X PUT \
  -H "Content-Type: application/json" \
  -H "X-Disable-Provenance: true" \
  -u admin:admin \
  "target:3000/api/v1/provisioning/folder/$TARGET_FOLDER_UID/rule-groups/infra-checks" \
  -d @rule_group_patched.json


Get more detail on 400 errors: Enable debug logging in grafana.ini:

ini
[log]
level = debug

Complete working flow will be->

  1. Export from source
  2. Resolve UIDs on target by name
  3. Patch the JSON
  4. Import ->UI-editable

Thanks so much for the detailed response. In the end I was able to figure it out.