FusionAuth
    • Home
    • Categories
    • Recent
    • Popular
    • Pricing
    • Contact us
    • Docs
    • Login

    How can I get all users for an application using the API?

    Scheduled Pinned Locked Moved Solved
    Q&A
    user search user-api
    1
    6
    25.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • danD
      dan
      last edited by

      I want to retrieve all the users for an application configured in FusionAuth. How can I do so?

      --
      FusionAuth - Auth for devs, built by devs.
      https://fusionauth.io

      1 Reply Last reply Reply Quote 0
      • danD
        dan
        last edited by

        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.

        --
        FusionAuth - Auth for devs, built by devs.
        https://fusionauth.io

        1 Reply Last reply Reply Quote 0
        • P
          Pyroseza
          last edited by

          @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

          1 Reply Last reply Reply Quote 0
          • danD
            dan
            last edited by

            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?

            --
            FusionAuth - Auth for devs, built by devs.
            https://fusionauth.io

            P 1 Reply Last reply Reply Quote 0
            • P
              Pyroseza @dan
              last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • danD
                dan
                last edited by

                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/

                --
                FusionAuth - Auth for devs, built by devs.
                https://fusionauth.io

                1 Reply Last reply Reply Quote 0
                • W wesley referenced this topic on
                • First post
                  Last post