API - Working with assets assigned to a user

We are doing a lot of work with assets right now and I have a major pain point I wanted to see if anyone has a good solution for.

I am not able to view or act on assets assigned to a user in the WFA without doing an API call for the user to get the user ID, then do another API call for asset lookup to get the display_ids for the assets assigned to them. Am I missing something?

It would be so much easier to have an option to use include_assets=true to return the assets assigned to that user, all we would need it to return is the display_id for each asset assigned to them.

Something like this:
https://domain.freshservice.com/api/v2/requesters/12345&include_assets=true

I know I can setup a custom objects table to lookup the User and have it return the user id and all the asset display_ids assigned to them, but we have over 3000 CIs in our CMDB. I don’t want to have to manage a Custom Objects table that big.

Anyone have a way to deal with this conundrum?

So far I think you have to do 2 API calls. Here’s what I’m doing at the moment.

###Python###

import requests
import base64

# Encode API key for Authorization header
api_key = 'API_KEY:x'
encoded_key = base64.b64encode(api_key.encode('utf-8')).decode('utf-8')
headers = {
    'Authorization': f'Basic {encoded_key}',
    'Cookie': 'current_workspace_id=1'
}

def get_user_id_by_email(email, headers):
    base_url = "https://DOMAIN.freshservice.com/api/v2/"
    endpoints = ["requesters", "agents"]

    for endpoint in endpoints:
        url = f"{base_url}{endpoint}?email={email}"
        response = requests.get(url, headers=headers)
        data = response.json()

        if data.get(endpoint):
            return data[endpoint][0]["id"]
    return None

def get_user_assets(user_id, headers):
    url = f"https://DOMAIN.freshservice.com/api/v2/assets?filter=\"user_id%3A{user_id}\""
    response = requests.get(url, headers=headers)
    assets = response.json().get("assets", [])

    asset_tags = [asset["asset_tag"] for asset in assets]
    return " ".join(asset_tags) if asset_tags else "null"

def main(email):
    user_id = get_user_id_by_email(email, headers)
    if user_id:
        assets = get_user_assets(user_id, headers)
        print(assets)
    else:
        print("null")

# Example usage with a placeholder for the email option
main("EMAIL")

This topic was automatically closed after 365 days. New replies are no longer allowed.