Update Report Recipients via API

What Grafana version and what operating system are you using?

I am using Grafana version Grafana v11.1.0-71516 (0ba54152dc) on Grafana Cloud.

What are you trying to achieve?

I am trying to update the recipients of an existing report via the Grafana API.

How are you trying to achieve it?

I am using a Python script to send a PUT request to the Grafana API to update the recipients list of an existing report.

What happened?

I received a 400 Bad Request error, with the message {"message":"bad request data","traceID":"56283328ae5542906f90204f11bdc710"}.

What did you expect to happen?

I expected the recipients list of the report to be updated successfully, adding the new recipient to the existing list.

Can you copy/paste the configuration(s) that you are having problems with?

Here is my Python code:

import requests
import json

grafana_url = 'http://***********.grafana.net/api/reports/11'
api_key = '***************'  # Generated via service account at the instance level

# Headers
headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json'
}

# Fetch the existing report configuration
response = requests.get(grafana_url, headers=headers)
if response.status_code != 200:
    print(f'Failed to fetch report: {response.status_code}, {response.text}')
    exit()

# Update the recipients list
report_config = response.json()
report_config['recipients'] = report_config['recipients'] + ', newrecipient@example.com'

# Send the PUT request to update the report configuration
response = requests.put(grafana_url, headers=headers, json=report_config)
if response.status_code == 200:
    print('Recipient added successfully.')
    print(response.text)
else:
    print(f'Failed to add recipient: {response.status_code}, {response.text}')

This is the response I received:

Failed to add recipient: 400, {"message":"bad request data","traceID":"56283328ae5542906f90204f11bdc710"}

Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.

No errors were received in the Grafana UI. The error message received from the API response is: {"message":"bad request data","traceID":"56283328ae5542906f90204f11bdc710"}.

Did you follow any online instructions? If so, what is the URL?

Yes, I followed the instructions on the Grafana documentation page for updating reports via the API: Reporting API | Grafana documentation. Unfortunately, I think this documentation has an error, the example it gives for updating the data uses GET, when the description it gives suggests using PUT.

Thank you for your help!

Reporting is only available in Grafana Enterprise

IMHO You will be better to contact your enterprise support.

I got it working! I discovered that allow_redirects needs to be set to True in requests.put(grafana_url, headers=headers, json=report_config, allow_redirects=True).

import requests

grafana_url = 'https://******.grafana.net/api/reports/11'  # Ensure HTTPS is used
api_key = '*********'  # Generated via service account at the instance level, e.g., https://*****.grafana.net rather than https://grafana.com/orgs/******

# Headers
headers = {
  'Authorization': f'Bearer {api_key}',
  'Content-Type': 'application/json'
}

# Fetch the existing report configuration
response = requests.get(grafana_url, headers=headers)
if response.status_code != 200:
  print(f'Failed to fetch report: {response.status_code}, {response.text}')
  exit()

# Update the recipients list (while preventing duplicates)
report_config = response.json()
existing_recipients = report_config.get('recipients', '')
unique_recipients = ", ".join(sorted(set(['newrecipient@example.com'] + existing_recipients.split(', '))))
report_config['recipients'] = unique_recipients

# print("JSON object being sent:\n", json.dumps(report_config, indent=2))

# Send the PUT request to update the report configuration using the JSON parameter
response = requests.put(grafana_url, headers=headers, json=report_config, allow_redirects=True)
if response.status_code == 200: print(f'Recipient updated successfully: {response.status_code}, {response.text}')
else: print(f'Failed to add recipient: {response.status_code}, {response.text}')

Alternatively, if the json data is already in a json file, then allow_redirects does not need to be set to True and the following code works:

import requests

url = "https://*******.grafana.net/api/reports/11"
headers = {
  "Authorization": "Bearer ***********",
  "Content-Type": "application/json"
}

# Read the data from the report.json file
with open('report.json', 'r') as file:
  data = file.read()
  # print(data)

# Send the PUT request
response = requests.put(url, headers=headers, data=data)
if response.status_code == 200: print(f'Recipient updated successfully: {response.status_code}, {response.text}')
else: print(f'Failed to add recipient: {response.status_code}, {response.text}')