Cannot import community dashboards using the API

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

Those “community” dashboards are exported from the UI = you can import them via UI. API/UI export produce different outputs, so you can’t mix their outputs.

It is still possible to use API, but it very hackish, undocumented approach. See example in readme GitHub - monitoringartist/grafana-aws-cloudwatch-dashboards: 40+ Grafana dashboards for AWS CloudWatch metrics: EC2, Lambda, S3, ELB, EMR, EBS, SNS, SES, SQS, RDS, EFS, ElastiCache, Billing, API Gateway, VPN, Step Functions, Route 53, CodeBuild, ... - but don’t expect that it will be working for you case - you have input variable, so you will need to deal with it first.

The API documentation states that the endpoint /api/dashboards/db

Creates a new dashboard or updates an existing dashboard.

Unless I’m understanding something incorrectly, shouldn’t it successfully accept a dashboard as a json file via a POST? I can alter the community dashboard as needed, I’m just trying to understand what exactly needs to be changed for the dashboard import to work

You are correct. But import API accepted json format is different then json format produced by UI export.

I have been able to perform an import using postman to /api/dashboards/import. I simply copied the form data of the POST (body) of the request from the devtools when I added the dashboard manually via the Grafana UI.
I have also been able to pinpoint the issue to the content-length header, however I haven’t figured out how do it via curl or wget but I’ll continue to update this thread

Thank you for the help, I’ve posted the solution. I actually based it off of the one in the github link you provided, so thank you :grinning: