How can I get all users for an application using the API?
-
I want to retrieve all the users for an application configured in FusionAuth. How can I do so?
-
You can use the Search API to find all users that have a registration for a particular Application. You can search for users in a variety of ways. More info here: https://fusionauth.io/docs/v1/tech/apis/users#search-for-users
You'll want to use the
queryString
parameter. To generate it, the easiest way is to start in the admin UI. (If you know ElasticSearch querying by heart, feel free to just write the query ). Open up the advanced search controls on the Users page. Once you use the select boxes to query by registration, you can expand the "show search query" to see the query and use that on the API.The API search query would be something like this, where c53a95bc-c082-4243-b5df-d4408d5dd92b is the Id of the application you're looking for.
{ "bool" : { "must" : [ [ { "nested" : { "path" : "registrations", "query" : { "bool" : { "must" : [ { "match" : { "registrations.applicationId" : "c53a95bc-c082-4243-b5df-d4408d5dd92b" } } ] } } } } ] ] } }
If you are using the API directly, you need to make sure you urlencode the query string. So a curl command might look like this:
API_KEY=... QUERY='{ "bool" : { "must" : [ [ { "nested" : { "path" : "registrations", "query" : { "bool" : { "must" : [ { "match" : { "registrations.applicationId" : "c53a95bc-c082-4243-b5df-d4408d5dd92b" } } ] } } } } ] ] } }' ENC_QUERY=$(python -c "import urllib; print urllib.quote('''$QUERY''')") curl -H "Authorization: $API_KEY" "http://localhost:9011/api/user/search?query=$ENC_QUERY"
Note you won't be able to search for users based on their application registration when using the database engine. See the API docs for more details.
-
@dan said in How can I get all users for an application using the API?:
Note you won't be able to search for users based on their application registration when using the database engine. See the API docs for more details.
Regarding your note above, I'd like to know what is the correct way to perform the exact same search above but using a python client against a database, unfortunately the documentation does not explain how the API works from the view of the official python client
-
but using a python client against a database,
Do you mean using the database search, @jarpri08 ? I'm not sure I understand. Can you please explain more?
-
@dan yes correct, what is the appropriate syntax for the database search?
How would I do this kind of search for users within a Postgresql database, I'm not using ElasticSearch.
-
How would I do this kind of search for users within a Postgresql database, I'm not using ElasticSearch.
Sorry, you cannot.
Unfortunately you can't search beyond fuzzy matching on the parameters listed in the database search documentation. If you have advanced search needs, we recommend running elasticsearch, which is extremely powerful and flexible.
Here's a tutorial on switching search engines: https://fusionauth.io/docs/v1/tech/tutorials/switch-search-engines/