I am trying to get a list of users in jira i am using the api
@Ankit kharola
The /rest/api/3/users/search endpoint in Jira is paginated, meaning it only returns a limited number of users per request (default maxResults is 50). To retrieve a full list of users, you'll need to paginate through the results by incrementing the startAt parameter in each request until all users are fetched.
/rest/api/3/users/search
maxResults
startAt
Here’s the general approach to do this:
<em><strong>startAt</strong> </em>= 0
Here’s a pseudo-code example:
startAt = 0 maxResults = 50 allUsers = [] do { response = makeRequest("/rest/api/3/users/search?startAt=" + startAt + "&maxResults=" + maxResults) users = response.get("values") // Extract the list of users allUsers.addAll(users) startAt += maxResults } while (users.size() == maxResults) // allUsers now contains the complete list of users
Let me know if you need further clarification! 😊
hello @Akash Singh when we are fetching issues we also get a total field which tells us the total number of issues there so we can run a while loop to get all the issues at once and in this case we are setting the maxResults and then fetching users based on that but is it a possibility that the number of users will be more than the maxresults we have set. how can we make sure we fetched all the users that exist? like in issues we get a total count so we know how many we have to fetch but in this case we dont have that
@Ankit kharola My previous response did not account for the total attribute when determining if the API has stopped returning results. Below, I’ve outlined two approaches to handle pagination—both are correct, but it's worth noting that you may need to update your logic in the future as Atlassian plans to transition from index-based to cursor-based pagination.
total
Pagination Logic Without total:
Using total Field :
total_pages = total / maxResults
Here is an example Python script that implements the logic with total field.
import requests from requests.auth import HTTPBasicAuth import json # Parameters endpoint = "https://mysite.atlassian.com/rest/api/3/users/search" # API endpoint auth = HTTPBasicAuth("email@example.com", "<api_token>") # Authentication details headers = {"Accept": "application/json"} # Request headers max_results = 50 # Set maxResults value startAt = 0 # Initialize list to store all users all_users = [] # Make the first API request to get users and total response = requests.request( "GET", endpoint, headers=headers, auth=auth, params={"maxResults": max_results, "startAt": startAt} ) # Parse the response response_data = json.loads(response.text) users = response_data # Adjust this if the user data is inside a key like "values" all_users.extend(users) # If the total field exists, use it to calculate total pages total = response_data.get("total", 0) # Fetch total users if available if total > 0: total_pages = (total // max_results) + (1 if total % max_results != 0 else 0) # Fetch remaining pages for page in range(1, total_pages): # Start from 1 as first page is already fetched startAt = page * max_results response = requests.request( "GET", endpoint, headers=headers, auth=auth, params={"maxResults": max_results, "startAt": startAt} ) users = json.loads(response.text) all_users.extend(users) # Output the total users fetched print(f"Total users fetched: {len(all_users)}")
It looks like you're new here. Sign in or register to get started.