Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

I am trying to get list of users in jira through rest api

Ankit kharola
Contributor
January 27, 2025

I am trying to get a list of users in jira i am using the api 

 

/rest/api/3/users/search  but it doesnt return a total that is the total number of users  and it also has a maxCount of 50 so how do we get a list of all the users in jira if we want to?

1 answer

0 votes
Akash Singh
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 27, 2025 edited

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

Here’s the general approach to do this:

  1. Start with startAt = 0.
  2. Make a request to /rest/api/3/users/search.
  3. Use the maxResults parameter to control how many users you retrieve per page (up to 100).
  4. Continue making requests while the number of users in the response equals 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! 😊

Ankit kharola
Contributor
January 27, 2025

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

Akash Singh
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2025 edited

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

  1. Pagination Logic Without total:

    • Keep making requests and collecting results while the response size equals maxResults.
    • If the last response contains fewer than maxResults items, it means you've reached the end of the dataset.
    • In edge cases where the last page contains exactly maxResults, an additional request will be made. This will return an empty result, confirming you've fetched all users.
  2. Using total Field :

    • If the API provides a total field, it simplifies pagination.
    • You can calculate the total number of pages required as:
      total_pages = total​ / maxResults
    • Fetch data for exactly that many pages. This avoids the additional request with no results.

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)}")

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

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 Now
AUG Leaders

Atlassian Community Events