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 : http://docs.grafana.org/http_api/admin/#global-users
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@localhost:3000/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@localhost:3000/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@localhost:3000/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)
1 Like

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@localhost:3000/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@localhost:3000/api/admin/users/$(curl --silent -X POST -d "email=$2@bar.com&password=null\&active=true" http://admin:0logical@localhost:3000/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: