API - Finding / listing users by email address

Hi,

I’m working on some integration between a community website and a Rocket.Chat setup for community discussion. When certain actions happen on the website I’m intending to trigger actions in Rocket.Chat (add / leave channels, change permissions etc). I’m relatively happy with how those actions can happen through the API. All the relevant API calls are based on knowing the username or ID of the user to act upon. That’s fine and in general I can store the Rocket Chat user ID in my website database too so I know who to make the changes to.

However, as a bit of error handling, I want to also be able to find the ID for a rocketchat user based on their email address, in case I don’t have a stored ID, or something has become unlinked, or the user got created on / imported to RC before appearing in my website database.
I think the only way to do that would be through the users.list API command, but whilst I can find based on many of the user attributes, it doesn’t seem to work on searching by email address.

curl -G -H "X-Auth-Token: " -H "X-User-Id: "
https://localhost/api/v1/users.list
–data-urlencode ‘query={ “email”: “name@domain.com” }’
(other more general methods like regexp searches for individual letters have also failed)
This all returns 0 results, rather than any reported syntax error

Am I getting the syntax wrong? Is this a known issue, or a deliberate design? Are there any other methods of achieving what I want (ie: getting a user ID from an email address).

Thanks in advance.

Hi there, i was (time ago) in the same boat and i’ve solved it downloaded ALL user’s email from db (500 user).
Then finding which i wish and than take id USER ID

In brief (here code in VB):

restapi = “/api/v1/users.list?fields={”“username”":1,"“emails”":1}&count=500"
dati = “”
result_json = rocketGET_user_token()
oJSON.loadJSON(rcjson)
If oJSON.data(“status”) = “success” Then
swok = 0
If Instr(cosa,"@") = 0 Then
For Each x In oJSON.data(“users”)
Set user = oJSON.data(“users”).item(x)
If Ucase(user.item(“username”)) = cosa Then
id = user.item("_id")
swok = 1
Exit For
End If
Next
Else
For Each x In oJSON.data(“users”)
Set user = oJSON.data(“users”).item(x)
id = user.item("_id")
on error resume next
For Each y In user.item(“emails”)
If err.number = 0 Then
Set email = user.item(“emails”).item(y)
If Ucase(email.item(“address”)) = cosa Then
swok = 1
Exit For
End If
End If
Next
on error goto 0
If swok = 1 Then Exit For End If
Next
End If
If swok = 1 Then
restapi = “api/v1/chat.postMessage”
Set oJSON = New aspJSON
With oJSON.data
.Add “roomId”, id

… //and so


End If

Yeah, thanks minix. I was thinking I might be facing a loop-through-all scenario, but I’ve potentially got quite a lot of members (thousands), and playing ‘hunt the email address’ myself seems very silly.
Anyone know a reason why the filtering on the original function can’t/doesn’t work this way? email address is a pretty good global unique indicator / foreign key to other systems etc.

I have exactly this problem. I want to connect an external app to a rocketchat instance, and want to see if a user is already has a rocketchat account. The only valid way to do this would be query by email.

I cannot figure out how to configure the query parameters, since the emails field in the user entry is an array, and the email address is in the address field.

So basically the structure looks like this:

{

“users”: [
{
“_id”: “xxx”,
“createdAt”: “DATE”,
“emails”: [
{
“address”: “email@domain.com”,
“verified”: false
}
],
“type”: “user”,
“name”: “NAME
}
],
“count”: 1,
“offset”: 0,
“total”: 1,
“success”: true
}

result is always empty. I tried to guess some stuff via a nested query, but never got it working. It seems absolutly mad to get all users, and then parse the result in the external app

Ok, I found a solution for this. MongoDBs array query needs to be used, see here https://docs.mongodb.com/manual/tutorial/query-arrays/

What worked for me then is the folowing
curl -s -G -H “X-Auth-Token: XXXX” -H “X-User-Id: XXX” rocketchat.instance.chat --data-urlencode ‘query={“emails”:{"$elemMatch": {“address” : {"$eq":"email@domain.com"}}}}’

If the email exists at a user this throws one result.

Brilliant - well done Phil. That works for me to. That’ll make my code more ‘elegant’ too!

(tip for anyone else copying that to test it out - beware that the forum software has converted some of the double quotes to ‘pretty double quotes’ - if you get json parse errors, look out for those and swap back…)