2016-11-24 10:16:24 +01:00
+++
title = "Data source HTTP API "
description = "Grafana Data source HTTP API"
keywords = ["grafana", "http", "documentation", "api", "data source"]
2021-04-15 14:08:58 -07:00
aliases = ["/docs/grafana/latest/http_api/datasource/"]
2016-11-24 10:16:24 +01:00
+++
2016-02-05 01:47:34 -08:00
# Data source API
2016-02-02 22:59:22 -08:00
2021-09-29 17:45:27 +02:00
> If you are running Grafana Enterprise and have [Fine-grained access control]({{< relref "../enterprise/access-control/_index.md" >}}) enabled, for some endpoints you would need to have relevant permissions.
> Refer to specific resources to understand what permissions are required.
2019-09-19 15:04:56 -07:00
## Get all data sources
2016-02-02 22:59:22 -08:00
`GET /api/datasources`
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ---------------- | -------------- |
| datasources:read | datasources:\* |
### Examples
2016-02-02 22:59:22 -08:00
**Example Request** :
2017-10-05 19:01:03 +02:00
```http
GET /api/datasources HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
```
2016-02-02 22:59:22 -08:00
**Example Response** :
2017-10-05 19:01:03 +02:00
```http
HTTP / 1.1 200
Content-Type : application/json
[
2019-12-10 15:30:06 +02:00
{
"id" : 1 ,
"orgId" : 1 ,
2021-01-13 13:16:27 -05:00
"uid" : "H8joYFVGz"
2019-12-10 15:30:06 +02:00
"name" : "datasource_elastic" ,
"type" : "elasticsearch" ,
"typeLogoUrl" : "public/app/plugins/datasource/elasticsearch/img/elasticsearch.svg" ,
"access" : "proxy" ,
"url" : "http://mydatasource.com" ,
"password" : "" ,
"user" : "" ,
"database" : "grafana-dash" ,
"basicAuth" : false ,
"isDefault" : false ,
"jsonData" : {
"esVersion" : 5 ,
"logLevelField" : "" ,
"logMessageField" : "" ,
"maxConcurrentShardRequests" : 256 ,
"timeField" : "@timestamp"
},
"readOnly" : false
}
2017-10-05 19:01:03 +02:00
]
```
2016-02-02 22:59:22 -08:00
2019-05-22 17:54:08 +02:00
## Get a single data source by Id
2016-02-02 22:59:22 -08:00
`GET /api/datasources/:datasourceId`
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ---------------- | ---------------------------------------------------------------------------- |
| datasources:read | datasources:\*<br>datasources:id:\*<br>datasources:id:1 (single data source) |
### Examples
2016-02-02 22:59:22 -08:00
**Example Request** :
2017-10-05 19:01:03 +02:00
```http
GET /api/datasources/1 HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
```
2016-03-08 02:19:52 -08:00
**Example Response** :
2017-10-05 19:01:03 +02:00
```http
HTTP / 1.1 200
Content-Type : application/json
{
2019-12-10 15:30:06 +02:00
"id" : 1 ,
2021-01-13 13:16:27 -05:00
"uid" : "kLtEtcRGk" ,
"orgId" : 1 ,
"name" : "test_datasource" ,
"type" : "graphite" ,
"typeLogoUrl" : "" ,
"access" : "proxy" ,
"url" : "http://mydatasource.com" ,
"password" : "" ,
"user" : "" ,
"database" : "" ,
"basicAuth" : false ,
"basicAuthUser" : "" ,
"basicAuthPassword" : "" ,
"withCredentials" : false ,
"isDefault" : false ,
"jsonData" : {
"graphiteType" : "default" ,
"graphiteVersion" : "1.1"
},
"secureJsonFields" : {},
"version" : 1 ,
"readOnly" : false
}
```
## Get a single data source by UID
`GET /api/datasources/uid/:uid`
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ---------------- | -------------------------------------------------------------------------------------- |
| datasources:read | datasources:\*<br>datasources:uid:\*<br>datasources:uid:kLtEtcRGk (single data source) |
### Examples
2021-01-13 13:16:27 -05:00
**Example request:**
```http
GET /api/datasources/uid/kLtEtcRGk HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
```
**Example Response** :
```http
HTTP / 1.1 200
Content-Type : application/json
{
"id" : 1 ,
"uid" : "kLtEtcRGk" ,
2019-12-10 15:30:06 +02:00
"orgId" : 1 ,
"name" : "test_datasource" ,
"type" : "graphite" ,
"typeLogoUrl" : "" ,
"access" : "proxy" ,
"url" : "http://mydatasource.com" ,
"password" : "" ,
"user" : "" ,
"database" : "" ,
"basicAuth" : false ,
"basicAuthUser" : "" ,
"basicAuthPassword" : "" ,
"withCredentials" : false ,
"isDefault" : false ,
"jsonData" : {
"graphiteType" : "default" ,
"graphiteVersion" : "1.1"
},
"secureJsonFields" : {},
"version" : 1 ,
"readOnly" : false
2017-10-05 19:01:03 +02:00
}
```
2016-03-08 02:19:52 -08:00
2016-03-10 01:31:10 -08:00
## Get a single data source by Name
2016-03-08 02:19:52 -08:00
2018-02-23 14:52:45 +01:00
`GET /api/datasources/name/:name`
2016-03-08 02:19:52 -08:00
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ---------------- | ---------------------------------------------------------------------------------------------- |
| datasources:read | datasources:\*<br>datasources:name:\*<br>datasources:name:test_datasource (single data source) |
### Examples
2016-03-08 02:19:52 -08:00
**Example Request** :
2017-10-05 19:01:03 +02:00
```http
GET /api/datasources/name/test_datasource HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
```
2016-02-02 22:59:22 -08:00
**Example Response** :
2017-10-05 19:01:03 +02:00
```http
HTTP / 1.1 200
Content-Type : application/json
{
2019-12-10 15:30:06 +02:00
"id" : 1 ,
2021-01-13 13:16:27 -05:00
"uid" : "kLtEtcRGk" ,
2019-12-10 15:30:06 +02:00
"orgId" : 1 ,
"name" : "test_datasource" ,
"type" : "graphite" ,
"typeLogoUrl" : "" ,
"access" : "proxy" ,
"url" : "http://mydatasource.com" ,
"password" : "" ,
"user" : "" ,
"database" : "" ,
"basicAuth" : false ,
"basicAuthUser" : "" ,
"basicAuthPassword" : "" ,
"withCredentials" : false ,
"isDefault" : false ,
"jsonData" : {
"graphiteType" : "default" ,
"graphiteVersion" : "1.1"
},
"secureJsonFields" : {},
"version" : 1 ,
"readOnly" : false
2017-10-05 19:01:03 +02:00
}
```
2016-02-02 22:59:22 -08:00
2016-03-10 01:31:10 -08:00
## Get data source Id by Name
`GET /api/datasources/id/:name`
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ------------------- | ---------------------------------------------------------------------------------------------- |
2021-10-05 09:39:00 +02:00
| datasources.id:read | datasources:\*<br>datasources:name:\*<br>datasources:name:test_datasource (single data source) |
2021-09-29 17:45:27 +02:00
### Examples
2016-03-10 01:31:10 -08:00
**Example Request** :
2017-10-05 19:01:03 +02:00
```http
GET /api/datasources/id/test_datasource HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
```
2016-03-10 01:31:10 -08:00
**Example Response** :
2017-10-05 19:01:03 +02:00
```http
HTTP / 1.1 200
Content-Type : application/json
2016-03-10 01:31:10 -08:00
2017-10-05 19:01:03 +02:00
{
"id" : 1
}
```
2016-03-10 01:31:10 -08:00
2019-05-22 17:54:08 +02:00
## Create a data source
2016-02-02 22:59:22 -08:00
`POST /api/datasources`
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ------------------ | ----- |
| datasources:create | n/a |
### Examples
2017-08-10 16:03:42 -07:00
**Example Graphite Request** :
2016-02-02 22:59:22 -08:00
2017-10-05 19:01:03 +02:00
```http
POST /api/datasources HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
{
"name" : "test_datasource" ,
"type" : "graphite" ,
"url" : "http://mydatasource.com" ,
"access" : "proxy" ,
"basicAuth" : false
}
```
2016-02-02 22:59:22 -08:00
2019-12-10 15:30:06 +02:00
**Example Graphite Response** :
```http
HTTP / 1.1 200
Content-Type : application/json
{
"datasource" : {
"id" : 1 ,
"orgId" : 1 ,
"name" : "test_datasource" ,
"type" : "graphite" ,
"typeLogoUrl" : "" ,
"access" : "proxy" ,
"url" : "http://mydatasource.com" ,
"password" : "" ,
"user" : "" ,
"database" : "" ,
"basicAuth" : false ,
"basicAuthUser" : "" ,
"basicAuthPassword" : "" ,
"withCredentials" : false ,
"isDefault" : false ,
"jsonData" : {},
"secureJsonFields" : {},
"version" : 1 ,
"readOnly" : false
},
"id" : 1 ,
"message" : "Datasource added" ,
"name" : "test_datasource"
}
```
2020-06-11 12:12:27 +02:00
> **Note:** By defining `password` and `basicAuthPassword` under `secureJsonData` Grafana encrypts them securely as an encrypted blob in the database. The response then lists the encrypted fields under `secureJsonFields`.
2019-12-10 15:30:06 +02:00
**Example Graphite Request with basic auth enabled** :
```http
POST /api/datasources HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
{
"name" : "test_datasource" ,
"type" : "graphite" ,
"url" : "http://mydatasource.com" ,
"access" : "proxy" ,
"basicAuth" : true ,
"basicAuthUser" : "basicuser" ,
"secureJsonData" : {
"basicAuthPassword" : "basicpassword"
}
}
```
2021-08-06 07:52:36 -06:00
2019-12-10 15:30:06 +02:00
**Example Response with basic auth enabled** :
```http
HTTP / 1.1 200
Content-Type : application/json
{
"datasource" : {
"id" : 1 ,
"orgId" : 1 ,
"name" : "test_datasource" ,
"type" : "graphite" ,
"typeLogoUrl" : "" ,
"access" : "proxy" ,
"url" : "http://mydatasource.com" ,
"password" : "" ,
"user" : "" ,
"database" : "" ,
"basicAuth" : true ,
"basicAuthUser" : "basicuser" ,
"basicAuthPassword" : "" ,
"withCredentials" : false ,
"isDefault" : false ,
"jsonData" : {},
"secureJsonFields" : {
"basicAuthPassword" : true
},
"version" : 1 ,
"readOnly" : false
},
"id" : 102 ,
"message" : "Datasource added" ,
"name" : "test_datasource"
}
```
2017-08-10 16:03:42 -07:00
**Example CloudWatch Request** :
2017-10-05 19:01:03 +02:00
```http
POST /api/datasources HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
{
"name" : "test_datasource" ,
"type" : "cloudwatch" ,
"url" : "http://monitoring.us-west-1.amazonaws.com" ,
"access" : "proxy" ,
"jsonData" : {
"authType" : "keys" ,
"defaultRegion" : "us-west-1"
},
"secureJsonData" : {
2019-01-18 16:37:43 +01:00
"accessKey" : "Ol4pIDpeKSA6XikgOl4p" ,
"secretKey" : "dGVzdCBrZXkgYmxlYXNlIGRvbid0IHN0ZWFs"
2017-08-10 16:03:42 -07:00
}
2017-10-05 19:01:03 +02:00
}
```
2016-02-02 22:59:22 -08:00
2016-02-05 01:47:34 -08:00
## Update an existing data source
2016-02-02 22:59:22 -08:00
`PUT /api/datasources/:datasourceId`
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ----------------- | ---------------------------------------------------------------------------- |
| datasources:write | datasources:\*<br>datasources:id:\*<br>datasources:id:1 (single data source) |
### Examples
2016-02-02 22:59:22 -08:00
**Example Request** :
2017-10-05 19:01:03 +02:00
```http
PUT /api/datasources/1 HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
{
"id" : 1 ,
"orgId" : 1 ,
"name" : "test_datasource" ,
"type" : "graphite" ,
"access" : "proxy" ,
"url" : "http://mydatasource.com" ,
"password" : "" ,
"user" : "" ,
"database" : "" ,
"basicAuth" : true ,
"basicAuthUser" : "basicuser" ,
2019-12-10 15:30:06 +02:00
"secureJsonData" : {
"basicAuthPassword" : "basicpassword"
},
2017-10-05 19:01:03 +02:00
"isDefault" : false ,
"jsonData" : null
}
```
2016-02-02 22:59:22 -08:00
**Example Response** :
2017-10-05 19:01:03 +02:00
```http
HTTP / 1.1 200
Content-Type : application/json
2016-02-02 22:59:22 -08:00
2019-12-10 15:30:06 +02:00
{
"datasource" : {
"id" : 1 ,
"orgId" : 1 ,
"name" : "test_datasource" ,
"type" : "graphite" ,
"typeLogoUrl" : "" ,
"access" : "proxy" ,
"url" : "http://mydatasource.com" ,
"password" : "" ,
"user" : "" ,
"database" : "" ,
"basicAuth" : true ,
"basicAuthUser" : "basicuser" ,
"basicAuthPassword" : "" ,
"withCredentials" : false ,
"isDefault" : false ,
"jsonData" : {},
"secureJsonFields" : {
"basicAuthPassword" : true
},
"version" : 1 ,
"readOnly" : false
},
"id" : 102 ,
"message" : "Datasource updated" ,
"name" : "test_datasource"
}
2017-10-05 19:01:03 +02:00
```
2016-02-02 22:59:22 -08:00
2020-06-11 12:12:27 +02:00
> **Note:** Similar to [creating a data source](#create-a-data-source), `password` and `basicAuthPassword` should be defined under `secureJsonData` in order to be stored securely as an encrypted blob in the database. Then, the encrypted fields are listed under `secureJsonFields` section in the response.
2019-12-10 15:30:06 +02:00
2017-02-14 10:40:55 +01:00
## Delete an existing data source by id
2016-02-02 22:59:22 -08:00
`DELETE /api/datasources/:datasourceId`
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ------------------ | ---------------------------------------------------------------------------- |
| datasources:delete | datasources:\*<br>datasources:id:\*<br>datasources:id:1 (single data source) |
### Examples
2016-02-02 22:59:22 -08:00
**Example Request** :
2017-10-05 19:01:03 +02:00
```http
DELETE /api/datasources/1 HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
```
2016-02-02 22:59:22 -08:00
**Example Response** :
2017-10-05 19:01:03 +02:00
```http
HTTP / 1.1 200
Content-Type : application/json
2016-02-02 22:59:22 -08:00
2017-10-05 19:01:03 +02:00
{ "message" : "Data source deleted" }
```
2016-02-02 22:59:22 -08:00
2021-01-13 13:16:27 -05:00
## Delete an existing data source by UID
`DELETE /api/datasources/uid/:uid`
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ------------------ | -------------------------------------------------------------------------------------- |
| datasources:delete | datasources:\*<br>datasources:uid:\*<br>datasources:uid:kLtEtcRGk (single data source) |
### Examples
2021-01-13 13:16:27 -05:00
**Example request:**
```http
DELETE /api/datasources/uid/kLtEtcRGk HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
```
**Example response** :
```http
HTTP / 1.1 200
Content-Type : application/json
2021-11-05 15:06:14 +01:00
{
"message" : "Data source deleted" ,
"id" : 1
}
2021-01-13 13:16:27 -05:00
```
2017-02-14 10:40:55 +01:00
## Delete an existing data source by name
`DELETE /api/datasources/name/:datasourceName`
2021-09-29 17:45:27 +02:00
### Required permissions
See note in the [introduction ]({{< ref "#data-source-api" >}} ) for an explanation.
| Action | Scope |
| ------------------ | ---------------------------------------------------------------------------------------------- |
| datasources:delete | datasources:\*<br>datasources:name:\*<br>datasources:name:test_datasource (single data source) |
### Examples
2017-02-14 10:40:55 +01:00
**Example Request** :
2017-10-05 19:01:03 +02:00
```http
DELETE /api/datasources/name/test_datasource HTTP / 1.1
Accept : application/json
Content-Type : application/json
Authorization : Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
```
2017-02-14 10:40:55 +01:00
**Example Response** :
2017-10-05 19:01:03 +02:00
```http
HTTP / 1.1 200
Content-Type : application/json
2017-02-14 10:40:55 +01:00
2020-07-31 08:22:09 +02:00
{
"message" : "Data source deleted" ,
"id" : 1
}
2017-10-05 19:01:03 +02:00
```
2017-02-14 10:40:55 +01:00
2016-02-02 22:59:22 -08:00
## Data source proxy calls
`GET /api/datasources/proxy/:datasourceId/*`
2019-09-19 15:04:56 -07:00
Proxies all calls to the actual data source.
2021-02-22 13:44:24 -05:00
## Query a data source by ID
Queries a data source having backend implementation.
`POST /api/tsdb/query`
> **Note:** Most of Grafana's builtin data sources have backend implementation.
**Example Request** :
```http
POST /api/tsdb/query HTTP / 1.1
Accept : application/json
Content-Type : application/json
{
"from" : "1420066800000" ,
"to" : "1575845999999" ,
"queries" : [
{
"refId" : "A" ,
"intervalMs" : 86400000 ,
"maxDataPoints" : 1092 ,
"datasourceId" : 86 ,
"rawSql" : "SELECT 1 as valueOne, 2 as valueTwo" ,
"format" : "table"
}
]
}
```
2021-08-06 07:52:36 -06:00
2021-02-22 13:44:24 -05:00
> **Note:** The `from`, `to`, and `queries` properties are required.
JSON Body schema:
- **from/to** – Should be either absolute in epoch timestamps in milliseconds or relative using Grafana time units. For example, `now-1h` .
- **queries.refId** – Specifies an identifier of the query. Is optional and default to "A".
- **queries.datasourceId** – Specifies the data source to be queried. Each `query` in the request must have an unique `datasourceId` .
- **queries.maxDataPoints** - Species maximum amount of data points that dashboard panel can render. Is optional and default to 100.
- **queries.intervalMs** - Specifies the time interval in milliseconds of time series. Is optional and defaults to 1000.
In addition, each data source has its own specific properties that should be added in a request.
**Example request for the MySQL data source:**
```http
POST /api/tsdb/query HTTP / 1.1
Accept : application/json
Content-Type : application/json
{
"from" : "1420066800000" ,
"to" : "1575845999999" ,
"queries" : [
{
"refId" : "A" ,
"intervalMs" : 86400000 ,
"maxDataPoints" : 1092 ,
"datasourceId" : 86 ,
"rawSql" : "SELECT\n time,\n sum(opened) AS \"Opened\",\n sum(closed) AS \"Closed\"\nFROM\n issues_activity\nWHERE\n $__unixEpochFilter(time) AND\n period = 'm' AND\n repo IN('grafana/grafana') AND\n opened_by IN('Contributor','Grafana Labs')\nGROUP BY 1\nORDER BY 1\n" ,
"format" : "time_series"
}
]
}
```
**Example MySQL time series query response:**
2021-08-06 07:52:36 -06:00
2021-02-22 13:44:24 -05:00
```http
HTTP / 1.1 200
Content-Type : application/json
{
"results" : {
"A" : {
"refId" : "A" ,
"meta" : {
"rowCount" : 0 ,
"sql" : "SELECT\n time,\n sum(opened) AS \"Opened\",\n sum(closed) AS \"Closed\"\nFROM\n issues_activity\nWHERE\n time >= 1420066800 AND time <= 1575845999 AND\n period = 'm' AND\n repo IN('grafana/grafana') AND\n opened_by IN('Contributor','Grafana Labs')\nGROUP BY 1\nORDER BY 1\n"
},
"series" : [
{
"name" : "Opened" ,
"points" : [
[
109 ,
1420070400000
],
[
122 ,
1422748800000
]
]
},
{
"name" : "Closed" ,
"points" : [
[
89 ,
1420070400000
]
]
}
]
}
}
}
```