Grafana API , how to create datasource & dashboards in specific ORG

Hello all,
Grafana API , how to create datasource & dashboards in specific ORG, the API call missing that , or there any sequence of doing that?


The org is taken from the api key/bearer token. An api key is organization specific so to create data sources and dashboards for an org, create an API key for that org.

Hi, thanks for the reply, it works. But, what could be the sequence of the API calls to script the process the of creation the ORG default datasource & default dashboards in this ORG.
something like:

  1. Create Org
  2. Create API Key (Token) ,parse the response , but how i can assign it to this org?
    rest per your answer, i just send the create Dashboard & datasource using Token value.

Thank you for your help.

If I understand your question correctly, you want to know how to create an org and an API Key (token) via the API before you have a token?

Two options:

  • Create the org and first API key manually in the Grafana UI
  • Use Basic Auth. So you would create the org and the first token with the Grafana admin user and using Basic Auth. The API documentation is wrong in a few places here where it looks like you can use a token but where it actually requires Basic Auth. (It’s on my todo list to fix)

An example of using basic auth to access the api (admin user and password is admin):

curl http://admin:admin@localhost:3000/api/orgs

Hi Daniel,
thanks, yes, I am actually using the basic auth to create an ORG.

  1. Create Org → Test.
  2. create Token

I performed the quick test . and the token has been created in the Main.Org instead of Test.
So the question , how i can create the token via API inside the chosen Org. ,
I was thinking that I have to do the next:

  • create the org
  • create the user
  • assign the user to the org
  • create Token using the newly created user login
  • create the dashboard & datasource
  • delete the user.

However, you may suggest more optimal solution.


Ok, this was a bit complicated. Going to put this in a guide on the docs site.

  1. Create the org:

    curl -X POST -H "Content-Type: application/json" -d '{"name":"apiorg"}' http://admin:admin@localhost:3000/api/orgs
  2. Optional step. If the org is already created and step 3 fails then first add your Admin user to the org:

    curl -X POST -H "Content-Type: application/json" -d '{"loginOrEmail":"admin", "role": "Admin"}' http://admin:admin@localhost:3000/api/orgs/<org id of new org>/users
  3. Switch the org context for the Admin user to the new org:

    curl -X POST http://admin:admin@localhost:3000/api/user/using/<id of new org>
  4. Create the token:

    curl -X POST -H "Content-Type: application/json" -d '{"name":"apikeycurl", "role": "Admin"}' http://admin:admin@localhost:3000/api/auth/keys

Here is a first draft of a guide for creating a Token and adding a dashboard:

Hi Daniel,
thanks , i will check after my vacation!
Have a nice weekend.

Thank you for creating this document. I came looking for exactly this information.

I wonder whether you could expand on how the token is intended to be used. For example, is this like a session token, which should be re-created every time my application connects to the API? Or is this something permanent which I should save in my application and re-use each time? Does is expire after a certain time? Should I explicitly delete it when my session ends?

It is not a temporary token and is meant to be reused. You should treat the token/api key like a password and save it in a safe place.

It does not expire but you can revoke it via the Grafana GUI if you ever need to limit access (user has left the company or the token gets lost)

Sorry, I’ve been distracted for a bit and didn’t reply sooner, but thank you for the answer; this helps.

I’ve got two grafana instances. I connect to them thru java code.

First one has external administrator (I don’t control this instance). I go thru steps 1, 3 and 4. 1st (returns organization id= 120) and 3rd are ok with response code 200. 4th step returns also ok, but the key is assigned to organization with id=1. Tried also to use step number 2, but it returns 409 (I belive it’s ok, the admin was assigned by default to this organization).

Second instance is local on my desktop. If there, the script returns correct key for new organization (doing steps 1,3,4).

Any suggestions what could go wrong? Any configuration issue? What can make that switching org context for Admin returns with 200, but it’s still sam org with id = 1.

Please mention id number with in " " it will work