2022-05-26 23:06:25 +08:00
---
aliases:
2024-09-06 14:17:20 +08:00
- ../../../http_api/create-api-tokens-for-org/ # /docs/grafana/< GRAFANA_VERSION > /http_api/create-api-tokens-for-org/
- ../../../tutorials/api_org_token_howto/ # /docs/grafana/< GRAFANA_VERSION > /tutorials/api_org_token_howto/
canonical: /docs/grafana/latest/developers/http_api/examples/create-api-tokens-for-org/
2022-05-26 23:06:25 +08:00
keywords:
- grafana
- tutorials
- API
- Token
- Org
- Organization
Explicitly set all front matter labels in the source files (#71548)
* Set every page to have defaults of 'Enterprise' and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration pages to have of 'Cloud', 'Enterprise', and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/enterprise-licensing pages to have 'Enterprise' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/organization-management pages to have 'Enterprise' and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/provisioning pages to have 'Enterprise' and 'Open source' labels
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/recorded-queries pages to have labels cloud,enterprise
* Set administration/roles-and-permissions/access-control pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set administration/stats-and-license pages to have labels cloud,enterprise
* Set alerting pages to have labels cloud,enterprise,oss
* Set breaking-changes pages to have labels cloud,enterprise,oss
* Set dashboards pages to have labels cloud,enterprise,oss
* Set datasources pages to have labels cloud,enterprise,oss
* Set explore pages to have labels cloud,enterprise,oss
* Set fundamentals pages to have labels cloud,enterprise,oss
* Set introduction/grafana-cloud pages to have labels cloud
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Fix introduction pages products
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set panels-visualizations pages to have labels cloud,enterprise,oss
* Set release-notes pages to have labels cloud,enterprise,oss
* Set search pages to have labels cloud,enterprise,oss
* Set setup-grafana/configure-security/audit-grafana pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set setup-grafana/configure-security/configure-authentication pages to have labels cloud,enterprise,oss
* Set setup-grafana/configure-security/configure-authentication/enhanced-ldap pages to have labels cloud,enterprise
* Set setup-grafana/configure-security/configure-authentication/saml pages to have labels cloud,enterprise
* Set setup-grafana/configure-security/configure-database-encryption/encrypt-secrets-using-hashicorp-key-vault pages to have labels cloud,enterprise
* Set setup-grafana/configure-security/configure-request-security pages to have labels cloud,enterprise,oss
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set setup-grafana/configure-security/configure-team-sync pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set setup-grafana/configure-security/export-logs pages to have labels cloud,enterprise
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
* Set troubleshooting pages to have labels cloud,enterprise,oss
* Set whatsnew pages to have labels cloud,enterprise,oss
* Apply updated labels from review
Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com>
---------
Signed-off-by: Jack Baldry <jack.baldry@grafana.com>
Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com>
2023-07-18 16:10:12 +08:00
labels:
products:
- enterprise
- oss
2023-10-31 21:29:08 +08:00
title: 'API Tutorial: Create Service Account tokens and dashboards for an organization'
2022-05-26 23:06:25 +08:00
---
2017-06-18 02:59:19 +08:00
2023-10-31 21:29:08 +08:00
# Create Service Account tokens and dashboards for an organization
2017-06-18 02:59:19 +08:00
2020-09-28 23:27:50 +08:00
Use the Grafana API to set up new Grafana organizations or to add dynamically generated dashboards to an existing organization.
2017-06-18 02:59:19 +08:00
## Authentication
2020-11-10 04:26:49 +08:00
There are two authentication methods to access the API:
2017-06-18 02:59:19 +08:00
2020-11-10 04:26:49 +08:00
- Basic authentication: A Grafana Admin user can access some parts of the Grafana API through basic authentication.
2023-10-31 21:29:08 +08:00
- Service Account tokens: All organization actions can be accessed through a Service Account token. A Service Account token is associated with an organization. It can be used to create dashboards and other components specific for that organization.
2017-06-18 02:59:19 +08:00
2023-10-31 21:29:08 +08:00
## How to create a new organization and a Service Account Token
2017-06-18 02:59:19 +08:00
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` .
2025-03-06 20:59:08 +08:00
1. [Create the org ](/docs/grafana/<GRAFANA_VERSION>/http_api/org/#create-organization ). Here is an example using curl:
2017-06-18 02:59:19 +08:00
2021-08-06 21:52:36 +08:00
```bash
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.
2017-06-18 02:59:19 +08:00
2025-03-06 20:59:08 +08:00
1. Optional step. If the org was created previously and/or step 3 fails then first [add your Admin user to the org ](/docs/grafana/<GRAFANA_VERSION>/http_api/org/#add-user-in-organization ):
2021-08-06 21:52:36 +08:00
```bash
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
```
2017-06-18 02:59:19 +08:00
2025-03-06 20:59:08 +08:00
1. [Switch the org context for the Admin user to the new org ](/docs/grafana/<GRAFANA_VERSION>/http_api/user/#switch-user-context-for-signed-in-user ):
2021-08-06 21:52:36 +08:00
```bash
curl -X POST http://admin:admin@localhost:3000/api/user/using/< id of new org >
```
2017-06-18 02:59:19 +08:00
2025-03-06 20:59:08 +08:00
1. [Create a Service Account ](../../serviceaccount/#create-service-account ):
2017-06-18 02:59:19 +08:00
2021-08-06 21:52:36 +08:00
```bash
2023-10-31 21:29:08 +08:00
curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "role": "Admin"}' http://admin:admin@localhost:3000/api/serviceaccounts
2021-08-06 21:52:36 +08:00
```
2025-03-06 20:59:08 +08:00
1. [Create a Service Account token ](../../serviceaccount/#create-service-account-tokens ) for the service account created in the previous step:
2023-10-31 21:29:08 +08:00
```bash
curl -X POST -H "Content-Type: application/json" -d '{"name":"test-token"}' http://admin:admin@localhost:3000/api/serviceaccounts/< service account id > /tokens
```
This should return a response:
```http
HTTP/1.1 200
Content-Type: application/json
{
"id": 7,
"name": "test-token",
"key": "eyJrIjoiVjFxTHZ6dGdPSjg5Um92MjN1RlhjMkNqYkZUbm9jYkwiLCJuIjoiZ3JhZmFuYSIsImlkIjoxfQ=="
}
```
2017-06-18 02:59:19 +08:00
2021-08-06 21:52:36 +08:00
Save the key returned here in your password manager as it is not possible to fetch again it in the future.
2017-06-18 02:59:19 +08:00
2019-11-05 17:23:00 +08:00
## How to add a dashboard
2017-06-18 02:59:19 +08:00
2017-10-06 01:01:03 +08:00
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.
2017-06-18 02:59:19 +08:00
2025-03-06 20:59:08 +08:00
1. [Add a dashboard ](/docs/grafana/<GRAFANA_VERSION>/http_api/dashboard/#create-update-dashboard ) using the key (or bearer token as it is also called):
2017-06-18 02:59:19 +08:00
2021-08-06 21:52:36 +08:00
```bash
curl -X POST --insecure -H "Authorization: Bearer eyJrIjoiR0ZXZmt1UFc0OEpIOGN5RWdUalBJTllUTk83VlhtVGwiLCJuIjoiYXBpa2V5Y3VybCIsImlkIjo2fQ==" -H "Content-Type: application/json" -d '{
"dashboard": {
"id": null,
"title": "Production Overview",
"tags": [ "templated" ],
"timezone": "browser",
"rows": [
{
}
],
"schemaVersion": 6,
"version": 0
},
"overwrite": false
}' http://localhost:3000/api/dashboards/db
```
> **Note:** If you export a dashboard for sharing externally using the Share > Export menu in the Grafana UI, you cannot import that dashboard. Instead, click **View JSON** and save it to a file or fetch the JSON output through the API.