# Problems with simple python Elastic connection. Documentation Wrong?

**URL:** <https://discuss.elastic.co/t/problems-with-simple-python-elastic-connection-documentation-wrong/349400>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 14, 2023, 11:38pm UTC](https://discuss.elastic.co/t/problems-with-simple-python-elastic-connection-documentation-wrong/349400 "2023-12-14T23:38:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![allan.silverstein](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@allan.silverstein](https://discuss.elastic.co/u/allan.silverstein)\
**Post date:** [December 14, 2023, 11:38pm UTC](https://discuss.elastic.co/t/problems-with-simple-python-elastic-connection-documentation-wrong/349400/1 "2023-12-14T23:38:33Z")

</div>

I spent about an hour on what seems like a simple getting started exercise. Just connecting to the elastic stack with the python client. Leaving out all of the trial and error detail. The documentation shows that the api\_key parameter should be a tuple consisting of the api\_key\_id and the api\_key. However, the only way I was able to get this to work was to not include the api\_key\_id.  
This does not work due to authentication error:

```auto
es_client = Elasticsearch(es_url, ssl_assert_fingerprint=es_ca_fingerprint, api_key=(es_api_key_id, es_api_key), verify_certs=True) 

```

This works:

```auto
es_client = Elasticsearch(es_url, ssl_assert_fingerprint=es_ca_fingerprint, api_key=es_api_key, verify_certs=True)

```

Here is a link to the documentation that the api\_key\_id must be used:

> **[Connecting | Elasticsearch Python Client \[8.10\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/python-api/8.10/connecting.html#auth-apikey)**

What is the correct way to connect?

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [December 15, 2023, 2:39am UTC](https://discuss.elastic.co/t/problems-with-simple-python-elastic-connection-documentation-wrong/349400/2 "2023-12-15T02:39:20Z")

</div>

> [@allan.silverstein](#):
>
> This does not work due to authentication error

Can you share the error you got?

> [@allan.silverstein](#):
>
> What is the correct way to connect?

It accepts both a tuple with the id and the secret and a string with the encoded id and secret.

How did you created the API Key? Using the [Create API endoint](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html#security-api-create-api-key) or using the Create API UI in Kibana?

When you use the endpoint you get something like this as the response:

```auto
{
  "id": "vDtLa4wB9Bp7LZL4mBx9",        
  "name": "my-api-key",
  "expiration": 1544068612110,         
  "api_key": "Ky9gr42jSFC7sTuaS3gMNw", 
  "encoded": "dkR0TGE0d0I5QnA3TFpMNG1CeDk6S3k5Z3I0MmpTRkM3c1R1YVMzZ01Odw=="  
}

```

And if you use the Create API Kibana UI, you will get the encoded value only and need to manually select to see the `id:key` pair by choosing Beats or Logstash in the format drop down menu.

But it should work no matter if you are using the encoded value or the `id:key` pair.

On the example above you could use:

```auto
api_key=('vDtLa4wB9Bp7LZL4mBx9', 'Ky9gr42jSFC7sTuaS3gMNw')

```

or

```auto
api_key='dkR0TGE0d0I5QnA3TFpMNG1CeDk6S3k5Z3I0MmpTRkM3c1R1YVMzZ01Odw=='

```

---

<div class="post-metadata">

**Author:** ![allan.silverstein](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@allan.silverstein](https://discuss.elastic.co/u/allan.silverstein)\
**Post date:** [December 15, 2023, 6:49am UTC](https://discuss.elastic.co/t/problems-with-simple-python-elastic-connection-documentation-wrong/349400/3 "2023-12-15T06:49:00Z")

</div>

Ok. Thank you for the clarification. I created the API key via the UI and then queried the api\_key via curl (using the encoded value which worked) to get the api\_key\_id. When I set this up in python I was using the api\_key\_id for the api\_key\_id but using the encoded value (combination of the api\_key\_id and the api\_key) as the api\_key.

---

<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:** [January 12, 2024, 6:49am UTC](https://discuss.elastic.co/t/problems-with-simple-python-elastic-connection-documentation-wrong/349400/4 "2024-01-12T06:49:49Z")

</div>

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