# Generating User Api Keys with REST API

**URL:** https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916
**Category:** Development
**Tags:** rest-api
**Created:** [May 1, 2017, 10:55pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916 "2017-05-01T22:55:27Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Umer\_Mirza](https://avatars.discourse-cdn.com/v4/letter/u/4bbf92/32.png) [@Umer\_Mirza](https://meta.discourse.org/u/Umer_Mirza)
#### Post date: [May 1, 2017, 10:55pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/1 "2017-05-01T22:55:27Z")

</div>

Hi  
I am trying to generate User api keys. I know I can do it from admin dashboard. But instead of there I want to generate by making REST calls. I am following this post [User API keys specification](https://meta.discourse.org/t/user-api-keys-specification/48536)  
I am getting error “THIS PAGE IS PRIVATE OR DOES NOT EXISTS” when I try to make a GET request to this [https://unityforum.discoursehosting.net/api\_key/generate?redirect\_url=www.google.com.pk&client\_id=2&access\_level=read](https://unityforum.discoursehosting.net/api_key/generate?redirect_url=www.google.com.pk&client_id=2&access_level=read)

Can someone please show me how my url should look like with the paramters. I am also confused what exactly is client\_id here? Is it the id of the user whose api needs to created or is the username? Also can I use any url as redirect\_url? For the record I have added this redirect url in my admin settings.

---

<div class="post-metadata">

### Author: ![KunalKamble](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kunalkamble/32/120906_2.png) [@KunalKamble](https://meta.discourse.org/u/KunalKamble)
#### Post date: [June 23, 2017, 8:20pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/2 "2017-06-23T20:20:48Z")

</div>

I am also looking for same solution, can anyone help in this?

---

<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: [June 26, 2017, 9:51am UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/3 "2017-06-26T09:51:29Z")

</div>

Hello,

to generate an api key for a user:

```plaintext
curl -X "POST" "http://localhost:4000/admin/users/1/generate_api_key?api_key=10efbf2c9a84dfb3b6f60ffa117c029bda7fc9fb8f861ccd0dc8e8fbfa86968d&api_username=joffreyjaffeux"

```

Learn more about it:

- [Discourse API Docs](http://docs.discourse.org/#tag/Admin%2Fpaths%2F~1admin~1users~1%7Bid%7D~1generate_api_key%2Fpost)
- [discourse\_api/lib/discourse\_api/api/api\_key.rb at main · discourse/discourse\_api · GitHub](https://github.com/discourse/discourse_api/blob/master/lib/discourse_api/api/api_key.rb)

---

<div class="post-metadata">

### Author: ![djensen47](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/djensen47/32/120547_2.png) [@djensen47](https://meta.discourse.org/u/djensen47)
#### Post date: [November 9, 2017, 6:29am UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/4 "2017-11-09T06:29:25Z")

</div>

@j.jaffeux That’s not the same “API” that @Umer_Mirza was asking about. I put “API” in quote because it may not exist?

The [User API keys specification](https://meta.discourse.org/t/user-api-keys-specification/48536) RFC talks about the use case of allowing users _themselves_ the ability to request and API key so that a third party developed app may perform actions on their behalf. You would never want to give a third party your site API key because they would have full access.

I would ask this question in [User API keys specification](https://meta.discourse.org/t/user-api-keys-specification/48536) but that topic is closed. Does the API that @sam proposed in that post actually exist or not?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [November 9, 2017, 6:31am UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/5 "2017-11-09T06:31:02Z")

</div>

> [@djensen47](#):
>
> Does the API that @sam proposed in that post actually exist or not?

Yes user api keys exists and are consumed by the mobile app, look at the source code of the mobile app for exact specifications for now.

I would love to have this better documented so if you want to take that project on ❤

---

<div class="post-metadata">

### Author: ![djensen47](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/djensen47/32/120547_2.png) [@djensen47](https://meta.discourse.org/u/djensen47)
#### Post date: [November 9, 2017, 6:34am UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/7 "2017-11-09T06:34:24Z")

</div>

Awesome, thanks for the quick response.

Well, maybe I can get something started for documentation. I have to do _something_ on this front anyway so I can post what I come up with here.

---

<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: [November 9, 2017, 4:25pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/8 "2017-11-09T16:25:19Z")

</div>

This is the endpoint sam is talking about I think:

> <https://github.com/discourse/DiscourseMobile/blob/main/js/site_manager.js#L474>

> <https://github.com/discourse/discourse/blob/main/config/routes.rb#L739>

> <https://github.com/discourse/discourse/blob/main/app/controllers/user_api_keys_controller.rb>

---

<div class="post-metadata">

### Author: ![djensen47](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/djensen47/32/120547_2.png) [@djensen47](https://meta.discourse.org/u/djensen47)
#### Post date: [November 9, 2017, 6:35pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/9 "2017-11-09T18:35:48Z")

</div>

Thanks @j.jaffeux. I was drafting a similar message but I only had the code from the mobile app. These other code snippets are very useful.

When I tried this API against my own server using Postman, I kept getting a 400 error with no details. My Ruby is a little rusty but I’ll dig into the code more _but_ if you happen to know why a 400 might happen that would be helpful.

---

<div class="post-metadata">

### Author: ![djensen47](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/djensen47/32/120547_2.png) [@djensen47](https://meta.discourse.org/u/djensen47)
#### Post date: [November 9, 2017, 11:12pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/10 "2017-11-09T23:12:20Z")

</div>

I just tried adding my site to the official Discourse app, and it worked so I must be doing something wrong with regard to using the API in Postman.

_Aside_: I thought the app only worked for officially hosted Discourse sites. 🤷‍♂️

---

<div class="post-metadata">

### Author: ![sajattack](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sajattack/32/85732_2.png) [@sajattack](https://meta.discourse.org/u/sajattack)
#### Post date: [December 28, 2017, 10:18pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/11 "2017-12-28T22:18:37Z")

</div>

I’m getting error code 400 when I visit [https://community.frontrowcrew.com/user-api-key/new](https://community.frontrowcrew.com/user-api-key/new)

---

<div class="post-metadata">

### Author: ![sajattack](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sajattack/32/85732_2.png) [@sajattack](https://meta.discourse.org/u/sajattack)
#### Post date: [January 12, 2018, 8:17pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/12 "2018-01-12T20:17:48Z")

</div>

Can I get some assistance on this @sam?

---

<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: [January 13, 2018, 2:58am UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/13 "2018-01-13T02:58:52Z")

</div>

Did you want to purchase a hosting contract with us?

---

<div class="post-metadata">

### Author: ![sajattack](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sajattack/32/85732_2.png) [@sajattack](https://meta.discourse.org/u/sajattack)
#### Post date: [January 13, 2018, 3:09am UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/14 "2018-01-13T03:09:47Z")

</div>

No, but if you can point me in the right direction I’d be happy to document it for others.

---

<div class="post-metadata">

### Author: ![sajattack](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sajattack/32/85732_2.png) [@sajattack](https://meta.discourse.org/u/sajattack)
#### Post date: [April 2, 2018, 1:01am UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/15 "2018-04-02T01:01:25Z")

</div>

By looking at the URL generated by the discourse android app, I’ve uncovered the fields required for  
/user-api-key/new

- scopes - these are the permissions of the api key, in the case of the official app, to read notifications and to read session info
- client-id - hex string, dunno
- nonce - another hex string, used as a cryptographic nonce
- auth\_redirect - url to redirect to after permission is given
- application\_name - the name to display to the user of the application using the api
- public\_key - a public RSA key

Still a few questions:

- What are all the valid values for scopes?
- Where does the client-id come from?
- Are all api responses encrypted with the public key or what is it’s purpose?

---

<div class="post-metadata">

### Author: ![sajattack](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sajattack/32/85732_2.png) [@sajattack](https://meta.discourse.org/u/sajattack)
#### Post date: [April 2, 2018, 4:21am UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/16 "2018-04-02T04:21:33Z")

</div>

So, looking at the source code, client-id is 32 random bytes and nonce is 16 random bytes.  
Edit: It’s actually 32 & 16 nybbles, but the code generating it is called randomBytes(32)

---

<div class="post-metadata">

### Author: ![sajattack](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sajattack/32/85732_2.png) [@sajattack](https://meta.discourse.org/u/sajattack)
#### Post date: [April 2, 2018, 10:37pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/17 "2018-04-02T22:37:13Z")

</div>

Found the scopes!

> <https://github.com/discourse/discourse/blob/226ace16431607938594c4f0cf54f46fc094b019/app/models/user_api_key.rb#L3>

---

<div class="post-metadata">

### Author: ![sajattack](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sajattack/32/85732_2.png) [@sajattack](https://meta.discourse.org/u/sajattack)
#### Post date: [April 2, 2018, 11:23pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/18 "2018-04-02T23:23:18Z")

</div>

I’ve written a program that generates URLs of this specification, enough to get me to this screen:

 ![2018-04-02-162159_516x297_scrot](https://global.discourse-cdn.com/meta/original/3X/d/d/dd889c043c20173f3f3ab215de2a28b86c6671d8.png)  
But when I press Authorize I get a 403.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [April 2, 2018, 11:26pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/19 "2018-04-02T23:26:37Z")

</div>

After authorize you will be redirected, so the redirect URL needs to go somewhere where you can handle decrypting the payload it redirects with.

Also there is a site setting that lists where the redirect is allowed to go.

---

<div class="post-metadata">

### Author: ![sajattack](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sajattack/32/85732_2.png) [@sajattack](https://meta.discourse.org/u/sajattack)
#### Post date: [April 3, 2018, 7:29am UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/20 "2018-04-03T07:29:53Z")

</div>

Ok, got my api key. Is this a different API than the admin one documented at [http://docs.discourse.org](http://docs.discourse.org) ?

---

<div class="post-metadata">

### Author: ![sajattack](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sajattack/32/85732_2.png) [@sajattack](https://meta.discourse.org/u/sajattack)
#### Post date: [April 8, 2018, 11:41pm UTC](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916/21 "2018-04-08T23:41:16Z")

</div>

When I try to make a post with the api documented at [http://docs.discourse.org](http://docs.discourse.org) using a user api key, it says

> You are not permitted to view the requested resource. The API username or key is invalid.

So I think it’s a different API or needs different fields (client\_id?) for authentication.

[Next page](https://meta.discourse.org/t/generating-user-api-keys-with-rest-api/61916.md?page=2)
