# Endpoint for creating new user in python using Elasticsearch client

**URL:** <https://discuss.elastic.co/t/endpoint-for-creating-new-user-in-python-using-elasticsearch-client/311714>\
**Category:** Kibana\
**Tags:** elastic-stack-security\
**Created:** [August 9, 2022, 11:02am UTC](https://discuss.elastic.co/t/endpoint-for-creating-new-user-in-python-using-elasticsearch-client/311714 "2022-08-09T11:02:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azhar\_Uddin1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azhar_uddin1/32/98900_2.png) [@Azhar\_Uddin1](https://discuss.elastic.co/u/Azhar_Uddin1)\
**Post date:** [August 9, 2022, 11:02am UTC](https://discuss.elastic.co/t/endpoint-for-creating-new-user-in-python-using-elasticsearch-client/311714/1 "2022-08-09T11:02:06Z")

</div>

I am trying to create a new user by `API` in `flask`

```auto
@app.route('/create-new-user', methods=['GET','POST'])
def createUser():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        email = request.form.get('email')
        fullname = request.form.get('fullname')
        role = request.form.getlist('role')
        body ={"password":password, "username":username, "email":email, "fullname":fullname, "role":role}
        uri = f'https://localhost:9200/_security/user/{username}'
        es = Elasticsearch(hosts=uri, basic_auth=('elastic', 'zoU_Ec8JjbPnQNG4b8kY'), ca_certs="872ee6c0879fc0cfe73054c3ba7afb5902dbb171a2c215af35a5faab1206b924", verify_certs=False)
        return f'{es.info()}'
    return render_template('add_user.html')

```

But I don't understand how to pass the `submitted data` to this `localhost:9200/security/user/new-user-username` URL , using [documentation create or update a user api](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-put-user.html) but it's working fine in **Dev tools** don't know how to convert that into `python`

I also looked into [put\_user method in `ElasticSearch.client.SecurityClient`](https://elasticsearch-py.readthedocs.io/en/v8.3.3/api.html#elasticsearch.client.SecurityClient.put_user) but unable to get it how to use or implement in my code

---

<div class="post-metadata">

**Author:** ![JLeysens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jleysens/32/67404_2.png) [@JLeysens](https://discuss.elastic.co/u/JLeysens)\
**Post date:** [August 10, 2022, 8:41am UTC](https://discuss.elastic.co/t/endpoint-for-creating-new-user-in-python-using-elasticsearch-client/311714/2 "2022-08-10T08:41:20Z")

</div>

Hello! If I'm understanding correctly, it sounds like you're saying there is no method on the Elasticsearch interface to pass the arguments to?

You can use the `Transport` class to access lower-level APIs [Connection Layer API — Elasticsearch 7.16.0 documentation](https://elasticsearch-py.readthedocs.io/en/7.x/connection.html?highlight=perform_request#elasticsearch.Transport.perform_request)

Also make sure that you are correctly passing the host to the `Elasticsearach` class. You are passing the route to security as the host (see [API Documentation — Elasticsearch 7.16.0 documentation](https://elasticsearch-py.readthedocs.io/en/7.x/api.html#elasticsearch)).

---

<div class="post-metadata">

**Author:** ![Azhar\_Uddin1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azhar_uddin1/32/98900_2.png) [@Azhar\_Uddin1](https://discuss.elastic.co/u/Azhar_Uddin1)\
**Post date:** [August 11, 2022, 7:08am UTC](https://discuss.elastic.co/t/endpoint-for-creating-new-user-in-python-using-elasticsearch-client/311714/3 "2022-08-11T07:08:41Z")

</div>

Thanks, @JLeysens I did try to use the `perform_request` method I was getting an issue  
when setting the `headers` keyword argument I did try

```auto
headers = {'content-type':'application/json'}
headers = {'content-type':'application/mapbox-vector-tile'}
headers = {'content-type':'text/plain'}

```

but [put\_user](https://elasticsearch-py.readthedocs.io/en/v8.3.3/api.html#elasticsearch.client.SecurityClient.put_user) helped me out to create a `user`, update `user`, and many more

[Here is a code Example](https://stackoverflow.com/questions/73288016/how-to-create-user-of-elastic-using-flask) what I did is I just pass `instance` of `ElasticSearch` into the `SecurityClient`

**Edited Simple and better solution**

```auto
body ={"password":password, "username":username, "email":email, "full_name":fullname, 'enabled':True, 'roles':role}
uri = f'https://localhost:9200/'
client = Elasticsearch(hosts=uri, basic_auth=(ELASTIC_USER, 'ELASTIC_PASS'), ca_certs=CERTIFICATE, verify_certs=False)
client.perform_request(body=body, method='POST', path=f'/_security/user/{username}', headers={'content-type':'application/json', 'accept':'application/json'})

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 8, 2022, 7:09am UTC](https://discuss.elastic.co/t/endpoint-for-creating-new-user-in-python-using-elasticsearch-client/311714/4 "2022-09-08T07:09:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
