# API Filter users by emails, including secondary emails

**URL:** <https://meta.discourse.org/t/api-filter-users-by-emails-including-secondary-emails/296910>\
**Category:** Development\
**Tags:** rest-api\
**Created:** [February 27, 2024, 3:03pm UTC](https://meta.discourse.org/t/api-filter-users-by-emails-including-secondary-emails/296910 "2024-02-27T15:03:20Z")\
**Posts on this page:** 4\
**Page:** 1

## AI-generated summary

The original poster, [oca](/t/-/296910/1), is attempting to use the API to filter users by email, including secondary emails. They want to add users with matching emails to a group and remove those without matches. However, the `get(/admin/users/list/all.json?email={email})` command only returns users with the specified email as their primary address. 

[Arkshine](/t/-/296910/2) suggests using the Data Explorer plugin, which allows creating custom queries. [oca](/t/-/296910/3) successfully implements this solution by creating a query to join the `users` and `user_emails` tables and filter by email. They share their query and Python script to execute it using the Discourse API.

<div class="post-metadata">

**Author:** ![oca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/oca/32/156060_2.png) [@oca](https://meta.discourse.org/u/oca)\
**Post date:** [February 27, 2024, 3:03pm UTC](https://meta.discourse.org/t/api-filter-users-by-emails-including-secondary-emails/296910/1 "2024-02-27T15:03:20Z")

</div>

Hello,

I would like to match users from a list of email addresses coming from an other app, in order to add all the users to one group, and remove members that are not in the . I’ve got a script doing that from the API.

The removing part is easy. For each member, I extract all their emails with:  
` get(/u/{username}/emails.json)`  
and I try to match each one against the list. If there is none, the member is excluded.

The including one is trickier. For each email address, I want to see if there is a user with that address. If there is, I add it to the group. If there is not, I don’t want to send an invite. I use the command:  
`get(/admin/users/list/all.json?email={email})`  
but this command returns a list of users with that address as primary, an not the ones with that address as _secondary_, nor _unconfirmed\_emails_, nor _associated\_accounts_. Is there a way to filter from every possible email addresses, or should I export the list of all users and do that by hand externally ? I would prefer doing it internally so there would be less risks of data leaks.

Thanks !

---

<div class="post-metadata">

**Author:** ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)\
**Post date:** [February 27, 2024, 3:17pm UTC](https://meta.discourse.org/t/api-filter-users-by-emails-including-secondary-emails/296910/2 "2024-02-27T15:17:43Z")

</div>

Hello, welcome back! 👋

The [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin might be suitable for your use case.

> [@Discourse Data Explorer](https://meta.discourse.org/t/discourse-data-explorer/32566):
>
> discourse2Summary Discourse [Data Explorer](https://meta.discourse.org/t/32566?silent=true) allows you to make SQL queries against your live database, allowing for up-to-the-minute stats reporting.open_bookInstall Guide This plugin is bundled with Discourse core. There is no need to install the plugin separately.information_source If you’re looking for examples or support for any custom queries, you can find lots of topics in our #data-reporting category under the #sql-query tag. If there’s not one to suit your par…

You can use it as an API and create your query based on initial inputs.

> [@Run Data Explorer queries with the Discourse API](https://meta.discourse.org/t/run-data-explorer-queries-with-the-discourse-api/120063):
>
> bookmark This guide explains how to use the Discourse API to create, run, and manage queries with the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin. person_raising_hand Required user level: Administrator Virtually any action that can be performed through the Discourse user interface can also be triggered with the Discourse API. This document provides a comprehensive overview for utilizing the API specifically in conjunction with the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin. For a general overview of how to find the correct API …

---

<div class="post-metadata">

**Author:** ![oca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/oca/32/156060_2.png) [@oca](https://meta.discourse.org/u/oca)\
**Post date:** [February 29, 2024, 9:31am UTC](https://meta.discourse.org/t/api-filter-users-by-emails-including-secondary-emails/296910/3 "2024-02-29T09:31:50Z")

</div>

Thank you.  
I managed to do it with this plugin, using this query:

```sql
--[params]
-- string :q_email

SELECT u.username FROM users u JOIN user_emails e ON u.id=e.user_id WHERE e.email = :q_email

```

And calling it with the python script:

```python
import pydiscourse
client = DiscourseClient('https://forum.com', api_username='system', api_key='1234')
params = '{"q_email":"' + str(email) + '"}'
response = client._request('POST', '/admin/plugins/explorer/queries/{number}/run', params={"params":params}) # Yes, there are 3 params, each one representing something different. Good hacking practice !
username=response["rows"]

```

---

<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:** [March 30, 2024, 9:32am UTC](https://meta.discourse.org/t/api-filter-users-by-emails-including-secondary-emails/296910/4 "2024-03-30T09:32:22Z")

</div>

Closed on March 30, 2024, 9:32am UTC

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