A little while ago I looked at using Grafana on a Raspberry Pi ( https://geektechstuff.com/2020/06/25/grafana-prometheus-and-node-exporter-on-raspberry-pi-via-ansible-raspberry-pi-and-linux/ ), today I created a quick Python script to create new Grafana user accounts using the Grafana API.
Why Use The API?
Grafana does have a graphical web based interface for creating users, and if creating a single user account it is quite quick. However, if you have multiple user accounts then it starts to become a little bit of a chore to create each account. To speed up this process the API can be used.
Grafana provides a list of API calls it accepts here: https://grafana.com/docs/grafana/latest/http_api/
A Note On The Admin API
As per https://grafana.com/docs/grafana/latest/http_api/admin/ , ” The Admin HTTP API does not currently work with an API Token. API Tokens are currently only linked to an organization and an organization role. They cannot be given the permission of server admin, only users can be given that permission. So in order to use these API calls you will have to use Basic Auth and the Grafana user must have the Grafana Admin permission. (The default admin user is called admin and has permission to use this API “.
With this in mind, my Python script asks for the user credentials to use when it is run.
Account Material
I’m storing the details for the accounts I want creating in a CSV (comma separated values) file that has a header run saying:
name,email,login,password
Name = The account holders name
Email = The account holders email address (must be unique)
Login = The username for the account (must be unique)
Password = An initial password for the account
I’m storing the CSV file in the same directory as the Python file.

The Python File
This Python file requires three imports:
csv – so that it can interact with the CSV file
json – to convert the Python dictionary of parameters into JSON which the API call requires
requests – to send the data to the API and receive the response

import csv
import json
import requests
def create_admin_url():
# grafana URL - does not need HTTP:// or slashes following the address
grafana_url = 'localhost:3000'
# using the user API path
api_path = '/api/admin/users'
# DO NOT STORE CREDENTIALS in the program
print("Requires an Grafana account with admin powers")
admin_username = input("Username: ")
admin_password = input("Password: ")
# final URL
url = 'http://'+admin_username+':'+admin_password+'@'+grafana_url+api_path
return(url)
def create_account():
url = create_admin_url()
with open ('csv_name.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
name = row['name']
email = row['email']
login = row['login']
password = row['password']
parameters = {"name":name,"email":email,"login":login,"password":password}
headers = {"content-type": "application/json"}
# data is a Python dictionary and needs explicitely converting to JSON
response = requests.post(url,headers=headers,data=json.dumps(parameters))
print("username: "+name+" status: ")
print(response.text)
print(" ")
create_account()
You must be logged in to post a comment.