API Docs: create Authentication page and order sidebar menu (#92900)

* API Docs: create `Authentication` page and order sidebar menu

* fix minor typos

* Update capitalization

We don't capitalize titles as per https://grafana.com/docs/writers-toolkit/write/style-guide/capitalization-punctuation/

* Update docs/sources/developers/http_api/examples/_index.md

Co-authored-by: Jack Baldry <jack.baldry@grafana.com>

* Update docs/sources/developers/http_api/authentication.md

Co-authored-by: Jack Baldry <jack.baldry@grafana.com>

---------

Co-authored-by: Irene Rodriguez <irene.rodriguez@grafana.com>
Co-authored-by: Jack Baldry <jack.baldry@grafana.com>
This commit is contained in:
Pepe Cano
2024-09-06 08:17:20 +02:00
committed by GitHub
parent d8c3645f31
commit e87140bf75
6 changed files with 87 additions and 39 deletions

View File

@ -0,0 +1,18 @@
---
canonical: https://grafana.com/docs/grafana/latest/developers/http_api/examples/
keywords:
- grafana
- tutorials
- API
labels:
products:
- enterprise
- oss
title: 'HTTP API examples'
menuTitle: 'Examples'
weight: 02
---
# HTTP API examples
{{< section >}}

View File

@ -0,0 +1,106 @@
---
aliases:
- ../../../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/
keywords:
- grafana
- tutorials
- API
- Token
- Org
- Organization
labels:
products:
- enterprise
- oss
title: 'API Tutorial: Create Service Account tokens and dashboards for an organization'
---
# Create Service Account tokens and dashboards for an organization
Use the Grafana API to set up new Grafana organizations or to add dynamically generated dashboards to an existing organization.
## Authentication
There are two authentication methods to access the API:
- Basic authentication: A Grafana Admin user can access some parts of the Grafana API through basic authentication.
- 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.
## How to create a new organization and a Service Account 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. [Create the org](http://docs.grafana.org/http_api/org/#create-organization). Here is an example using curl:
```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.
1. Optional step. If the org was created previously and/or step 3 fails then first [add your Admin user to the org](http://docs.grafana.org/http_api/org/#add-user-in-organization):
```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
```
1. [Switch the org context for the Admin user to the new org](http://docs.grafana.org/http_api/user/#switch-user-context-for-signed-in-user):
```bash
curl -X POST http://admin:admin@localhost:3000/api/user/using/<id of new org>
```
1. [Create a Service Account]({{< relref "./serviceaccount/#create-service-account" >}}):
```bash
curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "role": "Admin"}' http://admin:admin@localhost:3000/api/serviceaccounts
```
1. [Create a Service Account token]({{< relref "./serviceaccount/#create-service-account-tokens" >}}) for the service account created in the previous step:
```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=="
}
```
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. [Add a dashboard](http://docs.grafana.org/http_api/dashboard/#create-update-dashboard) using the key (or bearer token as it is also called):
```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.

View File

@ -0,0 +1,43 @@
---
aliases:
- ../../../http_api/curl-examples/ # /docs/grafana/<GRAFANA_VERSION>/http_api/curl-examples/
- ../../http_api/curl-examples/ # /docs/grafana/<GRAFANA_VERSION>/developers/http_api/curl-examples/
canonical: /docs/grafana/latest/developers/http_api/examples/curl-examples/
description: cURL examples
keywords:
- grafana
- http
- documentation
- api
- curl
labels:
products:
- enterprise
- oss
title: cURL examples
---
# cURL examples
This page provides examples of calls to the Grafana API using cURL.
The most basic example for a dashboard for which there is no authentication. You can test the following on your local machine, assuming a default installation and anonymous access enabled, required:
```
curl http://localhost:3000/api/search
```
Here's a cURL command that works for getting the home dashboard when you are running Grafana locally with [basic authentication]({{< relref "/docs/grafana/latest/setup-grafana/configure-security/configure-authentication#basic-auth" >}}) enabled using the default admin credentials:
```
curl http://admin:admin@localhost:3000/api/search
```
To pass a username and password with [HTTP basic authorization]({{< relref "/docs/grafana/latest/administration/roles-and-permissions/access-control/manage-rbac-roles" >}}), encode them as base64.
You can't use authorization tokens in the request.
For example, to [list permissions associated with roles]({{< relref "/docs/grafana/latest/administration/roles-and-permissions/access-control/manage-rbac-roles" >}}) given a username of `user` and password of `password`, use:
```
curl --location '<grafana_url>/api/access-control/builtin-roles' --user 'user:password'
```