How create Dashboard and Panel via API

Hi All,

I know how to create a dashboard via API but I don’t find instructions to how to create panels within that dashboard still via API. Any idea?

There is no API for panels only.

You can create & update dashboards using this API
http://docs.grafana.org/http_api/dashboard/#create-update-dashboard

I saw that :slight_smile: But what is the point to create an empty dashboard?

1 Like

The panels need to be defined within the JSON that you submit in your POST request.

The example in the docs doesn’t spell this out, beyond

dashboard – The complete dashboard model

To get hands-on with this, you can (1) create a new dashboard with some panels manually, (2) export that dashboard’s definition as JSON, (3) put the exported dashboard definition inside the “dashboard” field of a new JSON object, (4) POST the resulting JSON object to the API endpoint. This will create a copy of your original dashboard. From there on, you can edit the JSON model you’re posting in order to modify or add any panels you desire.

So to your original question, if you want to add a panel to an existing dashboard you can obtain its definition via the API, add the panel to the JSON object, and push the updated model. (keep the same id/uid and set "overwrite": true)

1 Like

After I obtain dashboard definition via the API, I push the updated model. When I do this, the dashboard goes to another folder. Do you know why this happens and how to fix it?

I had the same problem. Each dashboard has a folderId associated with it. If you don’t specify one in your POST, Grafana will assume you want the dashboard to go in folderId 0 (which in my case is the General folder). So to avoid this, make sure you reuse the same folderId value when you POST an update to a dashboard.

How do you export json model ? manually ? Because i tried manually and then post to a new dashboard, replacing with the new uid but it does not work …
(i’m using powershell : Invoke-WebRequest -Uri “https://mygrafana.net/api/dashboards/uid/$dashboard_uid” -Method POST -Body ($params|ConvertTo-Json) -ContentType “application/json”) in $params there’s the json model

Can you help me ? i feel i’m close!
Maybe i need to update the json model i don’t know