Manage dashboard's variables using API

Hi
Is it possible to manage a dashboard’s variable using APIs? I didn’t find anything here: Dashboard HTTP API | Grafana Labs

Could you say a bit more about what specifically you’d like to do? Is it about provisioning new dashboards, or setting values for existing variables on existing dashboards, or adding/removing variables from dashboards?

In principle by using the API to update a dashboard JSON definition, you should also be able to set different default values - which are part of the definition - as well as make other modification. I haven’t tried it in practice, but it’s probably worth a shot.

I have a dashboard with the variable defined in it and now I want to update the values of the variable using APIs

Got it. Have you tried pushing new values by updating the dashboard JSON definition and pushing an update via the API’s create/update endpoint?

1 Like

Hi, I tried to update the variable using the dashboard API, but I get {"message":"Not found"} error
I have changed dashboard -> templating -> list[0] -> query

Can you share the full error message, including status code? Also a skeleton of your request, if possible (you can leave out the dashboard definition).

It sounds like an issue with the API usage. Are you definitely following the JSON schema described here? Your JSON payload needs to contain a dashboard object with the new dashboard definition, but same dashboard id as the existing dashboard. I think you might also have to set overwrite: true at the top level (outside the dashboard payload).

Sure, Here is the full request:

POST / HTTP/1.1
Host: 127.0.0.1:5000
User-Agent: Go-http-client/1.1
Content-Length: 2723
Authorization: Bearer eAAAAAAAAAAAAAA=
Accept-Encoding: gzip

{"dashboard":{"id":"674","uid":"Uu76AdCMz","templating":{"list":[{"name":"project","query":"a-.*,b-.*,c-.*"}]}},"overwrite":true,"message":"[Auto] Updated the variable"}

Thanks! Can you share the actual response though? I think what you shared was only the request.

And just to check, I assume that a dashboard with that combination of ID/UID exists already?

Sorry I forgot to attach the response:

HTTP/1.1 404 Not Found
Cache-Control: no-cache
Content-Type: application/json; charset=UTF-8
Expires: -1
Pragma: no-cache
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-Xss-Protection: 1; mode=block
Date: Sun, 16 May 2021 13:30:03 GMT
Content-Length: 23

{"message":"Not found"}

And yes, I can fetch the dashboard from the UID and I took the ID from the response
I send the POST request to this URL: https://grafana.tld/api/dashboards/uid

Great, thanks @nesabian!

So, based on the docs, the endpoint you need is /api/dashboards/db on the server. But in your request it looks like you do

I.e. you’re POSTing to the root path, which isn’t a valid endpoint, and therefore returns a 404 error.

I just saw the addition to your last post, where you do show what looks like a correct URL. Which leaves me confused, because it doesn’t look like your request. Can you confirm that you can successfully call any Grafana API endpoints?

Sorry I forgot to mention, I have captured the request by sending the request from my go app to the nc server to send it to you so that’s why the request is sent to the root, but the real address which I’m sending the requests to is https://grafana.tld/api/dashboards/uid
I just realized I cannot send any POST request to /api/dashboards/uid, I took the below request from grafana API samples:

 curl -X POST -i -H "Authorization: Bearer AAA=" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"dashboard":{"id":null,"uid":null,"title":"Yashar Production Overview","tags": [ "templated" ],"timezone": "browser","schemaVersion": 16,"version": 0, "refresh": "25s"},"folderId": 0,"message": "Made changes to xyz","overwrite": false}' https://grafana.tld/api/dashboards/uid/ 
HTTP/1.1 404 Not Found
Cache-Control: no-cache
Content-Type: application/json; charset=UTF-8
Expires: -1
Pragma: no-cache
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-Xss-Protection: 1; mode=block
Date: Sun, 16 May 2021 14:12:13 GMT
Content-Length: 23

{"message":"Not found"}

But Other APIs are working correctly. e.g:

curl -X GET -i -H "Authorization: Bearer AAAA=" https://grafana.tld/api/dashboards/uid/Uu76AdCMz
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Type: application/json
Expires: -1
Pragma: no-cache
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-Xss-Protection: 1; mode=block
Date: Sun, 16 May 2021 14:15:42 GMT
Transfer-Encoding: chunked

{"meta"....

Grafana version: Grafana v7.4.3 (010f20c1c8)

Right, thanks for clarifying.

The correct endpoint for the POST request is /api/dashboards/db.

1 Like

Oh, sorry again
So then I can create a new dashboards but cannot update the part of it (using /api/dashboards/db

Thanks! Can you clarify what the actual result is when you try to update?

By the way, keep in mind that you can’t update “a part” of a dashboard - you need to submit the entire new JSON definition.

I literally send a GET request to get the whole dashboard JSON and then update the variables and update the whole dashboard part as well as overwrite and message sections

Ok, and what was the result? (That was my question)

E.g. did you get a 200/OK response when you submitted the new dashboard to the API endpoint? But the update didn’t show up?

The result is always {"message":"Not found"} and 404 status code
But creating the new dashboards works correctly (200)

Ok, I think you need to double-check the request that you’re making here. If you call the /api/dashboards/db endpoint with a POST request you should not get a 404 error. You would in principle only get a 404 error if you call a non-existent endpoint URL. Note that the path s literally /api/dashboards/db - the dashboard ID or UID is not included in the path. Per the documentation, the possible responses from that endpoint are:

Status Codes:

  • 200 – Created
  • 400 – Errors (invalid json, missing or invalid fields, etc)
  • 401 – Unauthorized
  • 403 – Access denied
  • 412 – Precondition failed

Maybe something funny is happening with your nc server? If you’d like to troubleshoot further, please post the full request (omitting dashboard definition) and response as you have done above.

1 Like

Thanks svetb for your helps, I realised there is something wrong in my request header and now the API is working as expected

1 Like

How to load Grafana dashboards using API in our website instead of using iframe.