# Create a user using api

**URL:** <https://community.grafana.com/t/create-a-user-using-api/2578>\
**Category:** Developers & API\
**Tags:** api, users, provisioning\
**Created:** [September 4, 2017, 2:43pm UTC](https://community.grafana.com/t/create-a-user-using-api/2578 "2017-09-04T14:43:04Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonkytoaster](https://avatars.discourse-cdn.com/v4/letter/w/53a042/32.png) [@wonkytoaster](https://community.grafana.com/u/wonkytoaster)\
**Post date:** [September 4, 2017, 2:43pm UTC](https://community.grafana.com/t/create-a-user-using-api/2578/1 "2017-09-04T14:43:04Z")

</div>

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](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 !

---

<div class="post-metadata">

**Author:** ![daniellee](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/daniellee/32/41_2.png) [@daniellee](https://community.grafana.com/u/daniellee)\
**Post date:** [September 5, 2017, 8:06am UTC](https://community.grafana.com/t/create-a-user-using-api/2578/2 "2017-09-05T08:06:34Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![wonkytoaster](https://avatars.discourse-cdn.com/v4/letter/w/53a042/32.png) [@wonkytoaster](https://community.grafana.com/u/wonkytoaster)\
**Post date:** [September 5, 2017, 11:37am UTC](https://community.grafana.com/t/create-a-user-using-api/2578/3 "2017-09-05T11:37:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mahadev](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/mahadev/32/4173_2.png) [@mahadev](https://community.grafana.com/u/mahadev)\
**Post date:** [May 24, 2018, 9:16am UTC](https://community.grafana.com/t/create-a-user-using-api/2578/4 "2018-05-24T09:16:07Z")

</div>

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](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”}]

---

<div class="post-metadata">

**Author:** ![daniellee](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/daniellee/32/41_2.png) [@daniellee](https://community.grafana.com/u/daniellee)\
**Post date:** [May 24, 2018, 11:55am UTC](https://community.grafana.com/t/create-a-user-using-api/2578/5 "2018-05-24T11:55:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mahadev](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/mahadev/32/4173_2.png) [@mahadev](https://community.grafana.com/u/mahadev)\
**Post date:** [May 24, 2018, 1:17pm UTC](https://community.grafana.com/t/create-a-user-using-api/2578/6 "2018-05-24T13:17:09Z")

</div>

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/](http://docs.grafana.org/http_api/)  
but i don’t know how to use API’s

---

<div class="post-metadata">

**Author:** ![daniellee](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/daniellee/32/41_2.png) [@daniellee](https://community.grafana.com/u/daniellee)\
**Post date:** [May 24, 2018, 3:03pm UTC](https://community.grafana.com/t/create-a-user-using-api/2578/7 "2018-05-24T15:03:25Z")

</div>

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](http://docs.grafana.org/tutorials/api_org_token_howto/) for creating api tokens and dashboards via the api.

---

<div class="post-metadata">

**Author:** ![mahadev](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/mahadev/32/4173_2.png) [@mahadev](https://community.grafana.com/u/mahadev)\
**Post date:** [May 28, 2018, 9:10am UTC](https://community.grafana.com/t/create-a-user-using-api/2578/9 "2018-05-28T09:10:37Z")

</div>

Thanks API worked fine with POSTMAN

---

<div class="post-metadata">

**Author:** ![mahadev](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/mahadev/32/4173_2.png) [@mahadev](https://community.grafana.com/u/mahadev)\
**Post date:** [June 7, 2018, 6:19am UTC](https://community.grafana.com/t/create-a-user-using-api/2578/10 "2018-06-07T06:19:43Z")

</div>

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)
> 
> ```

---

<div class="post-metadata">

**Author:** ![allomorphy](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/allomorphy/32/50483_2.png) [@allomorphy](https://community.grafana.com/u/allomorphy)\
**Post date:** [January 15, 2019, 8:49pm UTC](https://community.grafana.com/t/create-a-user-using-api/2578/11 "2019-01-15T20:49:20Z")

</div>

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](mailto:name=&username=sam&email=sam@example.com)&password=bar&active=true,approved=true” [http://admin:admin@community.grafana.com/api/admin/users](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 😉

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

Anything I can copy-paste to make this happen?

Many thanks

---

<div class="post-metadata">

**Author:** ![allomorphy](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/allomorphy/32/50483_2.png) [@allomorphy](https://community.grafana.com/u/allomorphy)\
**Post date:** [January 23, 2019, 1:33pm UTC](https://community.grafana.com/t/create-a-user-using-api/2578/12 "2019-01-23T13:33:48Z")

</div>

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 ☹  
PS Thanks for Grafana 😄

---

<div class="post-metadata">

**Author:** ![pratapsingh13](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@pratapsingh13](https://community.grafana.com/u/pratapsingh13)\
**Post date:** [September 21, 2023, 8:22am UTC](https://community.grafana.com/t/create-a-user-using-api/2578/14 "2023-09-21T08:22:07Z")

</div>

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.

```auto
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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![sraghavendra1512](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@sraghavendra1512](https://community.grafana.com/u/sraghavendra1512)\
**Post date:** [March 29, 2024, 12:55pm UTC](https://community.grafana.com/t/create-a-user-using-api/2578/16 "2024-03-29T12:55:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![isaqueprofeta](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/isaqueprofeta/32/38142_2.png) [@isaqueprofeta](https://community.grafana.com/u/isaqueprofeta)\
**Post date:** [March 29, 2024, 1:25pm UTC](https://community.grafana.com/t/create-a-user-using-api/2578/17 "2024-03-29T13:25:20Z")

</div>

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](https://community.grafana.com/t/unable-to-authenticate-admin-and-check-user-existence-using-python/118201)

---

<div class="post-metadata">

**Author:** ![isaqueprofeta](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/isaqueprofeta/32/38142_2.png) [@isaqueprofeta](https://community.grafana.com/u/isaqueprofeta)\
**Post date:** [March 29, 2024, 1:25pm UTC](https://community.grafana.com/t/create-a-user-using-api/2578/18 "2024-03-29T13:25:27Z")

</div>


