# Unable to create url using generated API Key

**URL:** https://discuss.elastic.co/t/unable-to-create-url-using-generated-api-key/258844
**Category:** Kibana
**Created:** [December 16, 2020, 10:54am UTC](https://discuss.elastic.co/t/unable-to-create-url-using-generated-api-key/258844 "2020-12-16T10:54:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Kfir\_Stri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kfir_stri/32/80964_2.png) [@Kfir\_Stri](https://discuss.elastic.co/u/Kfir_Stri)
#### Post date: [December 16, 2020, 10:54am UTC](https://discuss.elastic.co/t/unable-to-create-url-using-generated-api-key/258844/1 "2020-12-16T10:54:15Z")

</div>

Hello 🙂

I'm trying to generate a short url using the REST api, and trying to authenticate using an API key i've generated before, no matter which cluster roles or indices priviliges I specify in the API key generation, I keep getting the error -

```auto
{
    "statusCode": 403,
    "error": "Forbidden",
    "message": "Unable to create url"
}

```

I'm using Elastic 7.8.1.

How I generated the API Key -

```auto
POST /_security/api_key
{
  "name": "kfir-url-shorten",
  "expiration": "1d", 
  "role_descriptors": { 
    "role-a": {
      "cluster": ["all"],
      "indices": [
        {
          "names": ["*"],
          "privileges": ["all"]
        }
      ]
    }
  }
}

```

My http request -

```auto
url = "<elastic cluter>:<port>/api/shorten_url"

payload = "{'url':'<some long url here>'}"
headers = {
    'Authorization': "ApiKey <my base64 api key>",
    'Content-Type': "application/json",
    'kbn-xsrf': "true"
    }

response = requests.request("POST", url, data=payload, headers=headers)

```

Not sure what other permissions I can give or even if this is possible using an API key..

Hope someone encountered this and can help me 🙏  
thanks!

---

<div class="post-metadata">

### Author: ![Larry\_Gregory](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/larry_gregory/32/34969_2.png) [@Larry\_Gregory](https://discuss.elastic.co/u/Larry_Gregory)
#### Post date: [December 16, 2020, 12:31pm UTC](https://discuss.elastic.co/t/unable-to-create-url-using-generated-api-key/258844/2 "2020-12-16T12:31:03Z")

</div>

Hey @Kfir_Stri,

Welcome to the discussion boards!

The ability to create short urls is controlled by what we call "[Kibana Privileges](https://www.elastic.co/guide/en/kibana/current/kibana-privileges.html)", as opposed to the cluster/index privileges that you may be accustomed to.

The good news is that we can adapt your API Key to grant this privilege. The bad news is that this has the potential to be fragile, and may not work on future versions of the stack. The API for creating API Keys is controlled by Elasticsearch, but the way that Kibana implements its privilege system is rather obtuse from that perspective.

Assuming you haven't changed Kibana's index (the `kibana.index` setting within `kibana.yml`), your API Key should look something like this:

```auto
POST /_security/api_key
{
  "name": "kfir-url-shorten",
  "expiration": "1d",
  "role_descriptors": {
    "role-a": {
      "cluster": [],
      "indices": [],
      "applications": [
        {
          "application": "kibana-.kibana",
          "privileges": [
            "feature_discover.all"
          ],
          "resources": [
            "*"
          ]
        }
      ]
    }
  }
}

```

Now for some details:

The `privileges` field tells Kibana which privileges you want this API Key to have. There are several privileges which grant the ability to create short URLs - the Discover privilege is one of them. I picked that one arbitrarily, but we could also create one with the visualize or dashboard feature as well.

The `resources` field tells Kibana which spaces this privilege should apply to. I chose the `*` resource, which means that this API Key will be able to create short urls in every space. If you want to restrict this to a specific space, then you can do something like:

```auto
POST /_security/api_key
{
  "name": "kfir-url-shorten",
  "expiration": "1d",
  "role_descriptors": {
    "role-a": {
      "cluster": [],
      "indices": [],
      "applications": [
        {
          "application": "kibana-.kibana",
          "privileges": [
            "feature_discover.all"
          ],
          "resources": [
            "space:default"
          ]
        }
      ]
    }
  }
}

```

This will grant access to just the `default` space, and no other spaces.

For short urls, there isn't a need to grant any cluster or index privileges, so I've omitted them from this example. Feel free to add those privileges back in if you need them for another purpose.

I hope this has helped - let me know if that works for you, or if you have any other questions.

---

<div class="post-metadata">

### Author: ![Kfir\_Stri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kfir_stri/32/80964_2.png) [@Kfir\_Stri](https://discuss.elastic.co/u/Kfir_Stri)
#### Post date: [December 16, 2020, 2:03pm UTC](https://discuss.elastic.co/t/unable-to-create-url-using-generated-api-key/258844/3 "2020-12-16T14:03:48Z")

</div>

Awesome, it works!  
Thanks for the explanation @Larry_Gregory 🙂

I just checked out `GET /_security/privilege` and can see the whole possible privileges and how to use them, makes more sense now 👍

How do I find out which other `resources` I have? or is this specified in the `kibana.index` file?

---

<div class="post-metadata">

### Author: ![Larry\_Gregory](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/larry_gregory/32/34969_2.png) [@Larry\_Gregory](https://discuss.elastic.co/u/Larry_Gregory)
#### Post date: [December 16, 2020, 3:00pm UTC](https://discuss.elastic.co/t/unable-to-create-url-using-generated-api-key/258844/4 "2020-12-16T15:00:43Z")

</div>

> How do I find out which other `resources` I have? or is this specified in the `kibana.index` file?

The `resources` field is currently just used to denote which [spaces](https://www.elastic.co/guide/en/kibana/current/xpack-spaces.html) the privilege should be applied to - so a [query for the current set of spaces](https://www.elastic.co/guide/en/kibana/current/spaces-api-get-all.html) would tell you which other resources are available. Note that the resource name is `space:` followed by the space `id` as returned from the [spaces API](https://www.elastic.co/guide/en/kibana/current/spaces-api-get-all.html).

---

<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 13, 2021, 3:00pm UTC](https://discuss.elastic.co/t/unable-to-create-url-using-generated-api-key/258844/5 "2021-01-13T15:00:43Z")

</div>

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