How to use REST API for Grafana Datasource

background
Reading Time: 3 minutes

Hello Readers! In this blog we will see how we can use the REST API for the grafana datasource. As we all know, grafana is a tool for visualization and we use REST API for the interaction. Basically it is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. We will see in this blog how to add and delete datasource using the REST API in grafana.

Prerequisites:

Grafana should be installed in your system or in your cluster. If not then please refer to this documentation: https://grafana.com/docs/grafana/latest/setup-grafana/installation/

My grafana is up and running as you can see below:

grafana

You must have Prometheus installed and should be in running state for adding endpoints in datasource in grafana.

Let’s start!

Creating Datasource using REST API:

For creating a datasource in grafana using api we have to use the POST method. And for the authentication purpose we need to create a Bearer token in grafana that we will use finally in the REST API. For this move to Configuration > API keys. Click on the New API key button. 

grafana api key

Provide these values and Click on Add.

grafana api key

It will create the key. Copy this key and paste it somewhere because you will only be able to view this key here once.

grafana api key

Inside my Postman I will select Authorization > Bearer token and paste this token there.

I have given http://localhost:3000/api/datasources as the URL. Inside the body select raw (json). And pass the following payload there:

{
 "name":"my_datasource",
 "type":"prometheus",
 "url":"http://mydatasource.com",
 "access":"proxy",
 "basicAuth":false
}

Inside the name give your datasource name as per your wish. Inside type give the type of datasource which you want to create and give the url for the datasource.

Click on send. In the response body you will get this:

{
   "datasource": {
       "id": 2,
       "uid": "x2PeNkR4k",
       "orgId": 1,
       "name": "my_datasource",
       "type": "prometheus",
       "typeLogoUrl": "",
       "access": "proxy",
       "url": "http://mydatasource.com",
       "user": "",
       "database": "",
       "basicAuth": false,
       "basicAuthUser": "",
       "withCredentials": false,
       "isDefault": false,
       "jsonData": {},
       "secureJsonFields": {},
       "version": 1,
       "readOnly": false
   },
   "id": 2,
   "message": "Datasource added",
   "name": "my_datasource"
}
create datasource
grafana datasource

So, My datasource is added successfully. In the grafana you can verify this.

Deleting Datasource using REST API:

Now, let’s see how we can delete any datasource in grafana using REST API. We will now use the DELETE method for this. Again give your bearer token in the authorization. Give the following URL and in this give your datasource uid which you want to delete:

http://localhost:3000/api/datasources/uid/x2PeNkR4k

Click on send. I found the response body as:

{
   "id": 2,
   "message": "Data source deleted"
}
delete datasource

So, it’s successfully deleted. We are done now with creating and deleting datasource using the REST API in grafana.

Conclusion:

Thank you for sticking to the end. In this blog we have seen how easily we can use the REST API for adding and deleting datasource in grafana. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.

HAPPY LEARNING!

Written by 

Naincy Kumari is a DevOps Consultant at Knoldus Inc. She is always ready to learn new technologies and tools. She loves painting and dancing.