This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

How can I use the API to search for departments where description = A070301?

Does the API allow me to search for all users where their department.description = A070301

An example of "">api.samanage.com/users.json" is below:

  • As far as I can see there's no way to filter on the department description from the users endpoint. 

    Filtering on sites and departments description isn't working for me either. So, you would need get a list of all the departments, then loop through all of them to find any that have that matching description, and then search for users with that department.

    Here's an example of what that might look like in python

    import requests
    import re
    
    # This reuses the TCP session for each request.
    samanage = requests.Session()
    auth_headers = {"X-Samanage-Authorization": "Bearer YourTokenHere"}
    samanage.headers.update(auth_headers)
    
    # Note this only grabs the first page. You'll need to decide how you want to handle pagination.
    response = samanage.get("https://api.samanage.com/departments.json")
    if not response.ok:
        response.raise_for_status()
    
    departments = response.json()
    matching_department_ids = []
    for department in departments:
        description = department.get("description", "")
        # This assumes the description is exactly "A070301". If you aren't familiar with rexex
        # use regex101's website to build the correct regex string
        match = re.search(r"A070301", description)
        if match:
            matching_department_ids.append(department["id"])
    
    # Again, this doesn't handle pagination. Python params argument would create a url like"
    # https://api.samanage.com/users.json?department%5B%5D=12345&department%5B%5D=67890
    response = samanage.get("https://api.samanage.com/users.json", params={"department": matching_department_ids})
    if not response.ok:
        response.raise_for_status()
    
    filtered_users = response.json()
    for user in filtered_users:
        print(user["id"], user["name"], user["email"])