title: How to connect properly to Elasticsearch cloud using API key and python lib?
body: <p>Created API key under <a href="https://www.elastic.co/cloud/" rel="nofollow noreferrer">Elastic cloud</a> (hosted on aws).</p>
  <p>Doc <a href="https://www.elastic.co/guide/en/cloud/current/ec-api-authentication.html" rel="nofollow noreferrer">declare</a> a proper auth header should be attached:</p>
  <pre><code>Authorization: ApiKey $EC_API_KEY
  </code></pre>
  <p>Using <code>curl</code> it works:</p>
  <pre><code>curl -i -H &quot;authorization: ApiKey LONG_KEY_STRING==&quot;  https://12345.us-east-2.aws.elastic-cloud.com:9243/_health
  
  HTTP/2 200 
  x-cloud-request-id: 1234abc
  content-type: text/plain; charset=utf-8
  content-length: 34
  date: Thu, 22 Oct 2020 12:36:54 GMT
  
  {
    &quot;ok&quot;: true,
    &quot;status&quot;: 200
  }
  
  </code></pre>
  <p>But the following python code (<code>elasticsearch-7.9.1</code>) fail:</p>
  <pre class="lang-py prettyprint-override"><code>es = Elasticsearch(['https://12345.us-east-2.aws.elastic-cloud.com:9243'], api_key=&quot;LONG_KEY_STRING==&quot;)
  print(es.cluster.health())
  </code></pre>
  <p>With error:</p>
  <pre><code>{
      &quot;errorMessage&quot;: &quot;AuthenticationException(401, 'security_exception', 'missing authentication credentials for REST request [/_cluster/health]')&quot;,
      &quot;stackTrace&quot;: [
          &quot;  File \&quot;/path/venv/lib64/python3.7/site-packages/lambda_local/main.py\&quot;, line 153, in execute\n    result = func(event, context._activate())\n&quot;,
          &quot;  File \&quot;main.py\&quot;, line 35, in handler\n    post_ping_doc(ping, msg[KEY_ID])\n&quot;,
          &quot;  File \&quot;main.py\&quot;, line 45, in post_ping_doc\n    print(es.cluster.health())\n&quot;,
          &quot;  File \&quot;/path/elasticsearch/client/utils.py\&quot;, line 152, in _wrapped\n    return func(*args, params=params, headers=headers, **kwargs)\n&quot;,
          &quot;  File \&quot;/path/elasticsearch/client/cluster.py\&quot;, line 69, in health\n    headers=headers,\n&quot;,
          &quot;  File \&quot;/path/elasticsearch/transport.py\&quot;, line 392, in perform_request\n    raise e\n&quot;,
          &quot;  File \&quot;/path/elasticsearch/transport.py\&quot;, line 365, in perform_request\n    timeout=timeout,\n&quot;,
          &quot;  File \&quot;/path/elasticsearch/connection/http_urllib3.py\&quot;, line 269, in perform_request\n    self._raise_error(response.status, raw_data)\n&quot;,
          &quot;  File \&quot;/path/elasticsearch/connection/base.py\&quot;, line 301, in _raise_error\n    status_code, error_message, additional_info\n&quot;
      ],
      &quot;errorType&quot;: &quot;AuthenticationException&quot;
  }
  </code></pre>
  <p>Tried other variations while browsing the net, all with the same result.</p>
  <p>Fired up a local <code>ncat -l 1234</code> and redirected the request to it. request seems legit...</p>
  <pre><code>GET / HTTP/1.1
  Host: 127.0.0.1:1234
  Accept-Encoding: identity
  connection: keep-alive
  content-type: application/json
  user-agent: elasticsearch-py/7.9.1 (Python 3.7.5)
  authorization: ApiKey LONG_KEY_STRING==
  </code></pre>
  <p>Python code does work with local unsecured ES on docker (not requiring any key).</p>
  <p>Currently out of idea, any help is appreciated.</p>