Create a user using api

hello everyone, this topic maybe already has an answer but i can’t find it, i’m trying to create a user (simple viewer) and add it to an organization using the api, but don’t succeed.
i’ve seen this page : Admin HTTP API | Grafana documentation
i would like to do it with a curl request but it doesn’t work, can somebody show me the syntax, it would be really helpfull!!

thanks !

You have to use a Grafana Admin user to create users and the authentication has to be Basic Auth. (I updated the docs to be clearer on that point.

curl -XPOST -H "Content-Type: application/json" -d '{
  "name":"User",
  "email":"user@graf.com",
  "login":"user",
  "password":"userpassword"
}' http://admin:admin@community.grafana.com/api/admin/users
3 Likes

thanks!!! i’ll try this!!!

curl -XPOST -H “Content-Type: application/json” -d ‘{“name”:“User”,“email”:“user@graf.com”,“login”:“user”,“password”:“password”}’ http://admin:admin@community.grafana.com/api/admin/users

I tried above one but i got following error
[{“classification”:“DeserializationError”,“message”:“invalid character ‘\’’ looking for beginning of value”},{“fieldNames”:[“Password”],“classification”:“RequiredError”,“message”:“Required”}]

Maybe it is just Discourse that is messing up your curl script but those single and double quotes look strange and they do not work for me in Bash.

You are using “ and ‘ when I think you should be using " and ’

When I switch those out then it works for me.

Thanks ,

Can you share which curl script did you use??

I am new to API’s so i don’t have much knowledge about it.Can you please share me how can I use the api
I referred this -http://docs.grafana.org/http_api/
but i don’t know how to use API’s

I used the curl script in your question. I just changed the single and double quotes to be “straight” quotes instead of the “curly” quotes that you used.

Here is a guide for creating api tokens and dashboards via the api.

Thanks API worked fine with POSTMAN

This works for me

import requests
url='http://admin:admin@community.grafana.com/api/admin/users'
data='''{
  "name":"tester",
  "email":"test@graadff.com",
  "login":"tester",
  "password":"test@123"
}'''
headers={"Content-Type": 'application/json'}
response = requests.post(url, data=data,headers=headers)
print (response.text)
2 Likes

The following (and variants) works to add a user on Grαfana 5.4.2:

$ curl -X POST -d “name=&username=sam&email=sam@example.com&password=bar&active=true,approved=true” http://admin:admin@community.grafana.com/api/admin/users

However none of the (many) examples that I have seen and followed enable me to set a password through the web API. When I attempt logon through the web, it fails and I first need to set the password through the standard web UI before login with the user created with the curl statement is possible.

Trying to find out what it is doing by going as follows is no dice :wink:

sudo sqlite /var/lib/grafana/grafana.db

Anything I can copy-paste to make this happen?

Many thanks

1 Like

I solved it like this: (ver. 5.4.3)

$ cat bin/graft

> curl -X PUT -d password=$1 http://admin:admin@community.grafana.com/api/admin/users/$(curl --silent -X POST -d "email=$2@bar.com&password=null\&active=true" http://admin:0logical@community.grafana.com/api/admin/users|jq '.id' )/password

run it like this:

$ graft secretpsswd John.Doe

Could not get set home dashboard to work via the API :frowning:
PS Thanks for Grafana :smile:

Hello community,
This topic maybe already has an answer but i can’t find it, I’m trying to create a user and provide access to only specific dashboard. For now I’m able to create user but not succeed to provide access to only specific dashboard. Below line of code I’m using to create user.

curl -XPOST -H "Content-Type: application/json" -d '{
  "name":"User",
  "email":"user@graf.com",
  "login":"user",
  "password":"userpassword"
}' http://admin:admin@localhost:3000/api/admin/users

also using python script for the same

import requests
import json

grafana_url = "localhost:3000"
username = "admin"
password = "admin"

base_url = "http://{}:{}@{}".format(username, password, grafana_url)

data = {
  "name":"Viewer",
  "email":"viewer@xxxxxx.com",
  "login":"viewer",
  "password":"viewer@123",
  "OrgId": 1

}


resp = requests.post(base_url + "/api/admin/users", json=data, verify=False)
data = resp.json()
data

How to check the user existence in Grafana? I tried this:
def check_user_existence(username, grafana_url, headers):
users_url = f"{grafana_url}/api/users/search"
response = requests.get(users_url, headers=headers, params={‘query’: username})
print(“User Search Response Content:”, response.text) # Debug print
if response.status_code == 200:
users = response.json()
print(“Users Found:”, users) # Debug print
for user in users:
if user[‘login’] == username or user[‘email’].startswith(username):
if users:
return True
return False
It is not listing the user. Please suggest.

I’ll close this, since it’s already solved and @sraghavendra1512 opened a new topic about it Unable to authenticate admin and check user existence using python