# Missing authentication credentials for rest request - API KEY

**URL:** <https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651>\
**Category:** Kibana\
**Tags:** elastic-stack-security\
**Created:** [October 26, 2021, 7:35am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651 "2021-10-26T07:35:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjana1](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@anjana1](https://discuss.elastic.co/u/anjana1)\
**Post date:** [October 26, 2021, 7:35am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651/1 "2021-10-26T07:35:16Z")

</div>

Hi,

I am trying to authorize my application using API Key from a PHP application . I have the below code. What else am i missing below as its asking for credetails.

`$search_host = 'lux.es.eastus2.azure.elastic-cloud.com'; $search_port = '9243'; $index = 'testing12'; $doc_type = '_doc'; $doc_id = 1; $json_doc = array( "user" => "kimchy", "post_date" => "2012-11-15T14:12:12", "message" => "trying out Elastic Search021" ); $json_doc = json_encode($json_doc); $baseUri = 'https://'.$search_host.':'.$search_port.'/'.$index.'/'.$doc_type.'/'.$doc_id; $ci = curl_init(); curl_setopt($ci, CURLOPT_URL, $baseUri); // $headers = array(); // $headers[] = 'Content-Type: application/json, Authorization: WXV3dHUzd0JaUTFVSEYwVDBqZmI6MTVjSjl5bFdUdjZQWm1oZHJMaGhGZw=='; $apiKey = ''; curl_setopt($ci, CURLOPT_HTTPHEADER, array( 'Authorization: ' . $apiKey, 'Content-Type: application/json' )); //curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_PORT, $search_port); curl_setopt($ci, CURLOPT_TIMEOUT, 200); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_FORBID_REUSE, 0); curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ci, CURLOPT_POSTFIELDS, $json_doc); $response = curl_exec($ci); if ($response === false) { $info = curl_getinfo($ci); curl_close( $ci); die('error occured during curl exec. Additioanl info: ' . var_export($info)); } curl_close( $ci); $decoded1 = json_decode($response,true); if (isset($decoded1->response->status) && $decoded1->response->status == 'ERROR') { die('error occured: ' . $decoded1->response->errormessage); } echo 'response ok!'; var_export($decoded1);`

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 26, 2021, 8:06am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651/2 "2021-10-26T08:06:54Z")

</div>

Can you please reformat your code/logs/config using the `</>` button, or markdown style back ticks. It helps to make things easy to read which helps us help you 🙂

---

<div class="post-metadata">

**Author:** ![anjana1](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@anjana1](https://discuss.elastic.co/u/anjana1)\
**Post date:** [October 26, 2021, 9:00am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651/3 "2021-10-26T09:00:37Z")

</div>

Sorry .Pls check now

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [October 29, 2021, 5:08am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651/4 "2021-10-29T05:08:21Z")

</div>

Sorry, your code doesn't have any new lines in it, so it's impossible to read.  
And you've commented out some parts of the code with `//` so we need the newlines in order to know where the comments end and the code starts again.

---

<div class="post-metadata">

**Author:** ![anjana1](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@anjana1](https://discuss.elastic.co/u/anjana1)\
**Post date:** [November 1, 2021, 6:21am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651/5 "2021-11-01T06:21:28Z")

</div>

```auto
$search_host = 'lux.es.eastus2.azure.elastic-cloud.com'; 
$search_port = '9243'; 
$index = 'testing12'; 
$doc_type = '_doc';
 $doc_id = 1; 
$json_doc = array( "user" => "kimchy",
 "post_date" => "2012-11-15T14:12:12", 
"message" => "trying out Elastic Search021" );
 $json_doc = json_encode($json_doc);
 $baseUri ='https://'.$search_host.':'.$search_port.'/'.$index.'/'.$doc_type.'/'.$doc_id; $ci = curl_init();
 curl_setopt($ci, CURLOPT_URL, $baseUri); 
// $headers = array(); 
// $headers<ins>[</ins><ins>]</ins> = 'Content-Type: application/json, Authorization: WXV3dHUzd0JaUTFVSEYwVDBqZmI6MTVjSjl5bFdUdjZQWm1oZHJMaGhGZw=='; $apiKey = ''; 
curl_setopt($ci, CURLOPT_HTTPHEADER, array( 'Authorization: ' . $apiKey, 'Content-Type: application/json' ));
 //curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($ci, CURLOPT_PORT, $search_port); curl_setopt($ci, CURLOPT_TIMEOUT, 200); 
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ci, CURLOPT_FORBID_REUSE, 0); curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ci, CURLOPT_POSTFIELDS, $json_doc); 
$response = curl_exec($ci); 
if ($response === false) { $info = curl_getinfo($ci); 
<ins>curl_clo</ins><ins>se</ins>( $ci);
 die('error occured during curl exec. Additioanl info: ' . var_export($info)); } 
curl_close( $ci);
 $decoded1 = json_decode($response,true); 
if (isset($decoded1->response->status) && $decoded1->response->status == 'ERROR') { die('error occured: ' . $decoded1->response->errormessage); } 
echo 'response ok!'; 
var_export($decoded1);

```

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [November 1, 2021, 6:57am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651/6 "2021-11-01T06:57:09Z")

</div>

> [@anjana1](#):
>
> `curl_setopt($ci, CURLOPT_HTTPHEADER, array( 'Authorization: ' . $apiKey, 'Content-Type: application/json' ));`

What is `$apiKey` ?

---

<div class="post-metadata">

**Author:** ![anjana1](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@anjana1](https://discuss.elastic.co/u/anjana1)\
**Post date:** [November 1, 2021, 7:31am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651/7 "2021-11-01T07:31:07Z")

</div>

Its the API key genreated via Cloud-\> Features -\>API keys

We are trying to authorize using API Key .

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [November 2, 2021, 4:43am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651/8 "2021-11-02T04:43:12Z")

</div>

The API Keys that you generate in the Cloud UI will only provide access to the Elastic Cloud API (`api.elastic-cloud.com`). You cannot use them to access a deployment.

For access to individual deployments, you need to [create an API key](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html) within that deployment.

---

<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:** [November 30, 2021, 4:43am UTC](https://discuss.elastic.co/t/missing-authentication-credentials-for-rest-request-api-key/287651/9 "2021-11-30T04:43:43Z")

</div>

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