I am trying to get a list of users in jira i am using the api
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.
Here’s the general approach to do this:
startAt = 0
./rest/api/3/users/search
.maxResults
parameter to control how many users you retrieve per page (up to 100).maxResults
. Each time, increase startAt
by maxResults
.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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
Pagination Logic Without total
:
maxResults
.maxResults
items, it means you've reached the end of the dataset.maxResults
, an additional request will be made. This will return an empty result, confirming you've fetched all users.Using total
Field :
total
field, it simplifies pagination.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)}")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.