Get e-mail address for a user via API

AI-generated summary

The discussion revolves around retrieving a user’s email address via the Discourse API. paully21 asks how to get a user’s email, noting that the previously available users/{username}/emails endpoint no longer works. j.jaffeux clarifies that GET /users/:username.json can return the email if the API key holder is the user themselves or a staff member with the appropriate permissions. j.jaffeux confirms this works for their own user, and j.jaffeux adds that staff can view emails if moderators_view_emails is enabled, though it logs a staff action. paully21 confirms the solution works after adjusting the request to a GET with .json in the URL, resolving the issue.

Is there a way to get a current e-mail address of a user via the API?

Our use case is this:

We allow users to manage their Discourse information via our website. In general, Discourse is only used as a mailing list and nearly all users never visit the actual Discourse site. I want to be able to show the user the current e-mail address that Discourse is using so they can see if it needs to be changed.

I am able to do this with all other fields except email.

There used to be an API endpoint users/{username}/emails but that doesn’t seem to be available anymore.

Hi,

GET /users/:username.json

will display the email in two cases:

  • the user id of the API KEY holder is the same than the user id related to the requested username
  • the API KEY holder is staff and the requested user is staged

Read more about this in code:

AFAIK this codepath has not changed recently.

Yeah, but that doesn’t help me. All of the users are already active and the user API key is not going to match.

This request does work for me:

curl "http://example.com/users/joffreyjaffeux/emails.json" \
     -H 'Api-Username: joffreyjaffeux' \
     -H 'Api-Key: xxx' \
     -H 'Content-Type: application/json; charset=utf-8' \

But, does it work if the URL is http://example.com/users/paulapostolos/emails.json ?

Yes it should work for yourself and other users as long as this is true:

  def can_check_emails?(user)
    is_admin? || (is_staff? && SiteSetting.moderators_view_emails)
  end

Note that doing so will trigger a staff action log.

That worked…I had some old code that used to work (version 1.4 ish) that had the request as a PUT and without the .json at the end. I modified it to a GET and changed the URL and now it works.

Thanks for the help!