Get back Username with API Key?

Hi There. When using the per-user api key process to get back a per-user api key from discourse, is it also possible to get back the user’s username at the same time?

1 Like

I’ve found a workaround: if you have the key, then when you make an authenticated call to the discourse api, it returns as a header “x-discourse-username: username”. Thanks to @RGJ for the tip.

So, for example, you can get back the key from discourse, and then use the key in an api call to an endpoint like /site.json.

Then parse the response headers, like:

....then((response) => {
          var headerResponseArray = (JSON.stringify(response.headers)).split(',')
          headerResponseArray.forEach((h) => {
             if (h.includes('x-discourse-username')) {
                 var userNameRaw = h.split(':')[1].trim()
             }
          })
    })

It would still be faster if I could just get back the username directly when discourse sends back the per-user api key itself. If anyone has a way to do that, please let me know.

3 Likes