API Tutorial: How To Create API Tokens And Dashboards For A Specific Organization

A common scenario is to want to via the Grafana API setup new Grafana organizations or to add dynamically generated dashboards to an existing organization.

Authentication

There are two ways to authenticate against the API: basic authentication and API Tokens.

Some parts of the API are only available through basic authentication and these parts of the API usually require that the user is a Grafana Admin. But all organization actions are accessed via an API Token. An API Token is tied to an organization and can be used to create dashboards etc but only for that organization.

How To Create A New Organization and an API Token

The task is to create a new organization and then add a Token that can be used by other users. In the examples below which use basic auth, the user is admin and the password is admin.

  1. curl -X POST -H "Content-Type: application/json" -d '{"name":"apiorg"}' http://admin:admin@localhost:3000/api/orgs

This should return a response: {"message":"Organization created","orgId":6}. Use the orgId for the next steps.

  1. curl -X POST http://admin:admin@localhost:3000/api/user/using/<id of new org>
  • Create the API token: bash curl -X POST -H "Content-Type: application/json" -d '{"name":"apikeycurl", "role": "Admin"}' http://admin:admin@localhost:3000/api/auth/keys This should return a response: {"name":"apikeycurl","key":"eyJrIjoiR0ZXZmt1UFc0OEpIOGN5RWdUalBJTllUTk83VlhtVGwiLCJuIjoiYXBpa2V5Y3VybCIsImlkIjo2fQ=="}. Save the key returned here in your password manager as it is not possible to fetch again it in the future.

How To Add A Dashboard

Using the Token that was created in the previous step, you can create a dashboard or carry out other actions without having to switch organizations.

  1. curl -X POST --insecure -H "Authorization: Bearer eyJrIjoiR0ZXZmt1UFc0OEpIOGN5RWdUalBJTllUTk83VlhtVGwiLCJuIjoiYXBpa2V5Y3VybCIsImlkIjo2fQ==" -H "Content-Type: application/json" -d '{
  2. "dashboard": {
  3. "id": null,
  4. "title": "Production Overview",
  5. "tags": [ "templated" ],
  6. "timezone": "browser",
  7. "rows": [
  8. {
  9. }
  10. ],
  11. "schemaVersion": 6,
  12. "version": 0
  13. },
  14. "overwrite": false
  15. }' http://localhost:3000/api/dashboards/db

This import will not work if you exported the dashboard via the Share -> Export menu in the Grafana UI (it strips out data source names etc.). View the JSON and save it to a file instead or fetch the dashboard JSON via the API.