# Unable to create "Single User" level API key, always defaults to "All Users"

**URL:** https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907
**Category:** Bug
**Created:** [February 18, 2020, 3:23pm UTC](https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907 "2020-02-18T15:23:28Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![joshfriend](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joshfriend/32/170290_2.png) [@joshfriend](https://meta.discourse.org/u/joshfriend)
#### Post date: [February 18, 2020, 3:23pm UTC](https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907/1 "2020-02-18T15:23:29Z")

</div>

Replication steps:

1. open `https://yourdiscoursedomain.com/admin/api/keys`
2. select new key
3. choose “single user” as the user level
4. Create the key
5. Open key details after creation
6. The user level will be “All Users”

It is very unclear to me what the difference between the two user level options is, but this seems to pretty clearly be a bug.

(I am using the hosted discourse service)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 18, 2020, 4:56pm UTC](https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907/2 "2020-02-18T16:56:32Z")

</div>

> [@joshfriend](#):
>
> choose “single user” as the user level

Did you also select a user to create the API key for?

> [@joshfriend](#):
>
> It is very unclear to me what the difference between the two user level options is,

Mostly an “all-user” api key is what you want. For some things, most notably using `discourse_theme`, a user key is required.

What are you trying to do with the API?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [February 18, 2020, 5:44pm UTC](https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907/3 "2020-02-18T17:44:11Z")

</div>

I can reproduce the issue if I follow the steps you have listed. When creating an API key for a single user, Discourse expects a username to be added to the form’s User field:

 ![image](https://global.discourse-cdn.com/meta/original/3X/0/5/055cb5c7e9a80247dffb8fddad1f79bfc5879e13.png)

If ‘Single User’ is selected as the User Level, but no username is added to the User field, an All Users API key is created. I would expect an error message to be displayed instead. Similar to what happens when the form is submitted without a description.

---

<div class="post-metadata">

### Author: ![joshfriend](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joshfriend/32/170290_2.png) [@joshfriend](https://meta.discourse.org/u/joshfriend)
#### Post date: [February 18, 2020, 7:20pm UTC](https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907/4 "2020-02-18T19:20:34Z")

</div>

> [@pfaffman](#):
>
> Did you also select a user to create the API key for?

I wanted to create a sort of “bot” user for retrieving posts from specific categories. This discourse instance has authentication from GSuite only, so there isn’t a way of creating arbitrary users anymore. I had hoped that for a “Single User” key it would let me enter the new username I wanted to assign bot, but now I see that the username must be an existing user. The username form field has no validation so it was not clear that I could not enter in just any name (like I would do for slack incoming webhooks for example). It then silently fails on submission when looking up the username I entered and creates an All Users key instead.

I suppose this is more of a usability bug than functional. I found the [documentation here](https://meta.discourse.org/t/user-api-keys-specification/48536) confusing as it did not explain the User Level field at all and there was no mention of an “All Users” type besides one instance in a comment farther down the thread.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 18, 2020, 7:24pm UTC](https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907/5 "2020-02-18T19:24:27Z")

</div>

> [@joshfriend](#):
>
> I wanted to create a sort of “bot” user for retrieving posts from specific categories.

An all user API key would work unless you want the “bot” not to be able to read some categories.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 19, 2020, 5:27am UTC](https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907/6 "2020-02-19T05:27:50Z")

</div>

Is this your username selector component @j.jaffeux?

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [February 19, 2020, 7:15am UTC](https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907/7 "2020-02-19T07:15:25Z")

</div>

No this is not a select-kit component. The issue is that we check only on description being present not on username. The userMode (single/all) is only a client side value, so when the model is validated server side, all it knows is that we requested a key and that we provided a user or not.

That should make it better, will ask david to review:  
[https://github.com/discourse/discourse/pull/9000](https://github.com/discourse/discourse/pull/9000)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 22, 2020, 3:59am UTC](https://meta.discourse.org/t/unable-to-create-single-user-level-api-key-always-defaults-to-all-users/141907/8 "2020-02-22T03:59:23Z")

</div>


