# Unable to connect via token through the API

**URL:** <https://discuss.elastic.co/t/unable-to-connect-via-token-through-the-api/259597>\
**Category:** Elasticsearch\
**Created:** [December 24, 2020, 3:57pm UTC](https://discuss.elastic.co/t/unable-to-connect-via-token-through-the-api/259597 "2020-12-24T15:57:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JSmithOner](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@JSmithOner](https://discuss.elastic.co/u/JSmithOner)\
**Post date:** [December 24, 2020, 3:57pm UTC](https://discuss.elastic.co/t/unable-to-connect-via-token-through-the-api/259597/1 "2020-12-24T15:57:46Z")

</div>

Hello Elasticsearch community,

I'm using google cloud functions as backend and using a simple angular HttpClient to connect via the API to my google cloud elasticsearch endpoint.

At first when I tried to connect I got some CORS policies problems but was able to sole them adding

```
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers : Authorization, X-Requested-With,X-Auth-Token,Content-Type, Content-Length 

```

to my instance yaml config file

now I've made this code:

```
host = 'https://c...9e.us-east1.gcp.elastic-cloud.com:9243/_search'
const token = btoa(`${my_id}:${my_token}`);
const options = {
  headers: {
    'Authorization': `ApiKey ` + token
  },
  params: {
    q: '*4*'
  }
}
this.http.get(this.host, options).subscribe(
  (response) => {
    console.log(response)
  }
)

```

and get a 401 error code.

I've also tried to change `xpack.security.authc.api_key.enabled` to `true` but I get

> Elasticsearch - 'xpack.security.authc.api\_key.enabled': is not allowed

I don't really understand what is xpack or on which instance I should exactly put this line but I've tried putting it everywhere i could.

I'm using free trial of entrerprise search

Many thanks in advance

---

<div class="post-metadata">

**Author:** ![JSmithOner](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@JSmithOner](https://discuss.elastic.co/u/JSmithOner)\
**Post date:** [January 4, 2021, 5:56am UTC](https://discuss.elastic.co/t/unable-to-connect-via-token-through-the-api/259597/2 "2021-01-04T05:56:41Z")

</div>

The problem was that the chosen role to create the API key had insufficient privileges

---

<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:** [February 1, 2021, 5:56am UTC](https://discuss.elastic.co/t/unable-to-connect-via-token-through-the-api/259597/3 "2021-02-01T05:56:44Z")

</div>

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