# Use the API to find a username based on an email?

**URL:** https://meta.discourse.org/t/use-the-api-to-find-a-username-based-on-an-email/45424
**Category:** Development
**Tags:** rest-api
**Created:** [June 8, 2016, 9:50am UTC](https://meta.discourse.org/t/use-the-api-to-find-a-username-based-on-an-email/45424 "2016-06-08T09:50:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)
#### Post date: [June 8, 2016, 9:50am UTC](https://meta.discourse.org/t/use-the-api-to-find-a-username-based-on-an-email/45424/1 "2016-06-08T09:50:22Z")

</div>

I’m trying to integrate a Discourse forum into an existing site.

Is it possible via the API to find out what a user’s username is, if all I have is their email?

---

<div class="post-metadata">

### Author: ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)
#### Post date: [July 4, 2016, 1:36pm UTC](https://meta.discourse.org/t/use-the-api-to-find-a-username-based-on-an-email/45424/2 "2016-07-04T13:36:05Z")

</div>

In lieu of a bump, let me describe what I want to do.

On my CMS, I have a bunch of user profiles. On each profile I want to display that user’s activity on Discourse, which I would get via RSS.

For example, my activity on this Discourse is available here:  
[https://meta.discourse.org/users/alehandrof/activity.rss](https://meta.discourse.org/users/alehandrof/activity.rss)

To get this URL I need a username, but I don’t have this data at the CMS. All I have is users’ email, which is the same in both the CMS & Discourse. (I’ve made sure of this manually for now, since it’s a small community, but I will be implementing SSO in the near future.)

It’s a PHP CMS and I’ll write a small add-on that pings Discourse with the email and gets a username in return. I will then use the username to form the URL and give it to a plugin that will consume the RSS feed and allow me to output it with my own HTML.

Is there an API endpoint that allows me to find a user’s username through their email?

---

<div class="post-metadata">

### Author: ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)
#### Post date: [July 23, 2016, 6:05pm UTC](https://meta.discourse.org/t/use-the-api-to-find-a-username-based-on-an-email/45424/3 "2016-07-23T18:05:09Z")

</div>

There’s a function to do this in this PHP API: [GitHub - communiteq/discourse-api-php: PHP API client for Discourse · GitHub](https://github.com/communiteq/discourse-api-php)

(The PHP APIs are a bit all over the place. There are some newer forks out there, but I ran into trouble with the one I tried.)

---

<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: [July 23, 2016, 8:02pm UTC](https://meta.discourse.org/t/use-the-api-to-find-a-username-based-on-an-email/45424/4 "2016-07-23T20:02:27Z")

</div>

There is nothing magical here, just do the action in your admin web UI and watch the f12 network console in the browser. Those are the API calls necessary.

---

<div class="post-metadata">

### Author: ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)
#### Post date: [July 24, 2016, 9:30am UTC](https://meta.discourse.org/t/use-the-api-to-find-a-username-based-on-an-email/45424/5 "2016-07-24T09:30:10Z")

</div>

I don’t follow :frowning:

The API I linked to works by filtering (which I didn’t know what possible) the list of active users by their email. Not sure how I’d trigger this in thew web UI.

```php
function getUsernameByEmail($email)
{
    $users = $this->_getRequest("/admin/users/list/active.json?filter=".urlencode($email));
    foreach($users->apiresult as $user) {
        if($user->email === $email) {
            return $user->username;
        }
    }
    return false;
}

```

BTW, let me clarify something in my previous response: I did get the API I linked to to work just fine. I just had trouble with other forks of it.

One thing I’d like to update during SSO is the user’s website field. I tried changing it in the admin to see what happens in the Network tab, but I don’t understand what I’m looking for. (AFAICT, I can’t add the website to the SSO payload, but I should be able to add another step that changes the website right before submitting the payload.)

---

<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:10am UTC](https://meta.discourse.org/t/use-the-api-to-find-a-username-based-on-an-email/45424/6 "2017-11-09T06:10:21Z")

</div>

Per:

[https://github.com/discourse/discourse/commit/06365023c4f215bb6867efe7e41bf740c919586b](https://github.com/discourse/discourse/commit/06365023c4f215bb6867efe7e41bf740c919586b)

If you are searching by email use:

```plaintext
SITENAME.com/admin/users/list/all.json?email=some@email.com

```

Using filter is inefficient and not recommended for this use case!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [January 7, 2019, 12:23pm UTC](https://meta.discourse.org/t/use-the-api-to-find-a-username-based-on-an-email/45424/7 "2019-01-07T12:23:09Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
