User API keys specification

(Sam Saffron) #1

Discourse contains a system for generating API keys per user if a very specific protocol is followed:

This feature facilitates “application” access to Discourse instances without needing to involve moderators.

High level description

At a high level:

  1. Client (desktop app, browser plugin, mobile app) generates a private/public key pair and return url

  2. Client redirects to a route on discourse giving discourse its public key

  3. Discourse gets approval from user to use app

  4. Discourse generates a user api key

  5. Discourse redirects back to return url with an encrypted payload using public api key containing the user api key


Use cases:

  1. Desktop applications that poll Discourse sites on behalf of end users to get notification counts across multiple sites.

  2. Mobile applications that poll Discourse sites on behalf of end users and handles push notifications

  3. Web applications that provide a dashboard for end users about various Discourse sites.

  4. Custom integrations with 3rd party apps that consume Discourse as part of a general company app. Eg: integrate Discourse community notifications into hopscotch app.

The design:

Site Settings

  • allow_user_api_keys: default on (allow users to generate api keys)

  • allow_user_api_key_scopes: allowed access scopes for user api keys. Defined here

  • min_trust_level_for_api_keys: 0 (require this trust level for api key access)

  • allowed_user_api_push_urls: list of sites that can be targets for push notifications

  • allowed_user_api_auth_redirects: allowed redirect destinations after user api key generation

Global Settings

  • max_user_api_reqs_per_minute: 20
  • max_user_api_reqs_per_day: 2880

UX changes

User page changes

If any user api keys were granted we will display a new apps tab in the user page.

The apps tab will list:

  • The name of the application eg: (“Discourse Notifier on Sam’s iPhone”)
  • Last use date
  • Level of access (read or read/write)
  • A revoke button so you can easily revoke any keys

API Key authorization UI

Every key will have to be explicitly authorized by end users in a page that clearly explains what is going on, for example:

The application, Discourse Notifier, would like read access to Meta Discourse.
Push notification will be published to

[Grant Access] [Cancel]

API key generation flow

API only requires a single GET request on the users end.




redirect_url: url to redirect back to with the generated token
push_url: url to push notifications to (required and valid only if push access level is requested)
client_id: a unique identifier for the client
access_level: comma delimited list with either: read, write, push

After /api_key/generate is called with correct params 2 things may happen

  1. If user is not logged on, we will redirect to login (after login we will resume authorization)
  2. Once a user is logged on they will be presented with the authorization UI

After authorization is allowed system will redirect back to the redirect_url, this will only include one param which is the api_key that was generated. client_id is not echoed back for extra security.

This is a draft and we are still fleshing out all details.

Consuming the API

Consuming the client API will be somewhat different that the current admin API.

Client can specify 2 headers:

User-Api-Key (required): the key that was generated


User-Api-Client-Id (optional): supply this to update the ‘client id’ stored for this api_key in the database.

Once those headers are specified client can perform requests against the API just as they would normally.

REST API and security
Discourse Login & Registeration
Beta testing the iOS mobile app
Custom Push Notifications: allowed user api push urls
Is there any Discourse Notification API?
Generating User Api Keys with REST API
Generating User Api Keys with REST API
Generating User Api Keys with REST API
Automatic Login from iOS/Android app
Automatic login from Windows Application
Discourse sso login using Rest API
Request header field Content-Type is not allowed by Access-Control-Allow-Headers
Request header field User-Api-Key is not allowed by Access-Control-Allow-Headers
Request header field User-Api-Key is not allowed by Access-Control-Allow-Headers
Discourse index
(Eli the Bearded) #2

There are places I’ve strongly considered writing a delayed post tool, so I can compose a bunch of “word of the day” type things to keep going while I’m on vacation. Doing it without proper API access and pretending to be a browser seems somewhat more complicated than on other forum software.

(Just as an example of another use case.)

(Sam Saffron) #3

That is a good example, but keep in mind, our default will be only to enable read tokens, not write ones. Site admins will have to opt to allow write tokens.

Completely agree that the _t cookie hacks are a huge problem.

(Jesse Perry) #4

Does max_user_api_calls_per_key_day setting apply to admin-created keys at /admin/api?

(Sam Saffron) #5

Nope only to user api keys, we can look at adding extra limits for standard Api keys, it is a good safeguard

(Sckott) #9

To be clear, this is not implemented yet, correct?

(Richard - #10

No, it’s there (for almost 2,5 years now) - Admin - Settings - User API.

(Jeff Atwood) #11

As of Discourse 2.1 these user api keys now auto-expire if left unused for long periods of time, correct @sam?

(Sam Saffron) #12

Correct the site setting: expire user api keys days is set to 180 out of the box.

(Sckott) #13

Okay, but you have to have admin access to see that page yes? And as an admin, it seems I can only create 1 key, so can’t create many keys for different users. confused.

(Blake Erickson) #14

What are you trying to accomplish? It’s not clear because there is this (user api keys) and regular API tokens (which is what I think you are after) that admins can create for multiple users, but you have to do that from the individual user’s page inside of the admin dashboard by clicking on the “generate” button on the “API Key” field.

(Sckott) #15

Thanks! Yes, “regular API tokens” for users is what i was after. I’m sure I’d seen that a million times, and never realized it was there. :heavy_check_mark:

(Vanessasaurus) #16

Hey everyone I’m not sure if this was resolved? I see that it’s possible to create an “All Users” and (per user) level API token as an admin, but I’m interested in giving a user the power to generate his/her own token. Has there been work done on this? It sounds like the original thread was getting at this idea, and then it was lost. Thanks!

(Sam Saffron) #17

Download the mobile App and then add a site, that uses the user api key system you have to follow a very strict workflow, no plans to expose arbitrary generation of keys in user prefs

(Vanessasaurus) #18

Thank you! I downloaded the app, and then realized that the sites I belong to don’t have this enabled (I’ve been testing on my local machine with the Dockerized discourse). Just to make sure we are talking about the same thing - I’m interested in generating tokens to use just a scoped subset of functions (and not global API keys to do all the things). Is this what you are talking about?

It seems like, given that there is an endpoint to generate tokens, it would be logical to provide this function (to users with a certain trust level, for example) from within the site. Otherwise, it would need to be the case that the site generates some external page (with a server to hide an admin token) to generate the keys for the user. Is it the case that 1. there is no internal generation of tokens for the user (and why not?) and 2. Nobody has created some external app (javascript, flask, anything really) to perform this action?

(Sam Saffron) #19


Possibly, conceptually

  1. This is tricky to consume cause you need to use custom HTTP headers (by design)

  2. It would be very hard to educate even TL3 users about what the point is of this thing.

I prefer not to ship UI for this in core, but maybe you should write a plugin for your specific use case?

Can you explain a bit more about the “why” here?

The design of the system centers around “I have an external dedicated program”, “this program knows how to follow Discourse protocol”, “It asks for token”

API keys (non user ones) are much easier to consume cause you just append them after the ?

(ymi) #22

Can I pass the API key generated from the user page on this? I was trying to use that API key and seeing ‘You are not permitted to view the requested resource.’ error on /categories.json API request