Hello,
I’m trying to import this community dashboard to Grafana via the API.
According to the documentation, the correct endpoint would be /api/dashboards/db
, however when looking at the browser’s devtools while importing the dashboard via the Grafana dashboard I noticed that /api/dashboards/import
was being used instead (and this is an undocumented endpoint).
Regardless I’ve tried with both, and so far I haven’t had any success. Here’s what I did:
- Downloaded the Grafana’s community dashboard json
- Altered value of “value” to match the datasource uid I wanted to attach to the dashboard:
"inputs": [ { "name": "DS_PROMETHEUS", "type": "datasource", "pluginId": "prometheus", "value": "this_one_right_here" }
- Saved the json with the updated datasource value and attempted to POST to both endpoints
On the /api/dashboards/import
I receive the following error:
"message": "Dashboard must be set"
On the /api/dashboards/db
I receive this one instead:
{ "message": "bad request data", "traceID": "" }
I’ve searched plenty of similar posts but so far I haven’t been able to find anything that matches this exact issue. Do I need to change anything else on the community’s dashboard json so it’s usable as json data in the POST request?
Additional information:
- I’m running the
grafana/grafana:9.5.2
image in a docker container - I have been able to perform other API-related requests
- The dashboards imports successfully and works via regular Grafana dashboard UI import
- The datasource guid exists and is correct, and have also tried changing to other created test datasources
Thank you.
SOLUTION:
I have been able to come up with a workaround. In order for a community dashboard to be able to be imported via the Grafana api endpoint /api/dashboards/import
the json file must first be changed to include additional values at the end. In order to get the correct format of the json, we must first import it normally via the Grafana’s UI and look for the POST request to api/dashboards/import
in the devtools. Once found, copy the POST data (which will be the updated dashboard json) and save it to a different file.
Keep in mind this new json will have the guid of the datasource you chose hardcoded. If you need a different datasource than the one chosen via the GUI, then either query the api endpoint for all the datasources and get the guid
, or go to the datasource on the Grafana UI and copy it from the URL.
Also Content-Type: application/json
and Accept: application/json
were necessary for the POST to process correctly.
Here’s the part that I was missing on the dashboard’s json file:
Here is a working curl
:
curl 'http://username:password@grafana:3000/api/dashboards/import' \ # An API token also works
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data @test.json` # path to the updated json dashboard file
And here is the same request using ansible’s uri
module:
- name: Import dashboard to Grafana
uri:
url: "http://user:password@{{ server }}:{{ port }}/api/dashboards/import"
method: "POST"
force_basic_auth: true
src: ./path_to_updated_dashboard_json.json
body_format: json
headers:
Accept: application/json
Content-Type: application/json
status_code: 200