API Access to Incidents using Requested By to Filter

Looking to use the API to get a user's most recent incidents. Using the web interface, I can see that you can filter by user using requested_by. When doing this it uses a numeric ID, that thus far I've been unable to figure out how to get. It isn't the user id or the group id for the user. Any idea on how I can get this ID or another way to filter?

Thanks

  • Howdy  ,

    You can get your user's IDs by starting with a GET Users query. Check the documentation here:
    https://apidoc.samanage.com/#tag/User/operation/getUsers

    Additionally/alternatively, you can see a user's ID in the URL when viewing their profile in the platform. For example: 
    app.solarwinds.com/.../8049635

    Another cool trick to learn more about the API is to add .json or .xml to the end of the URL for any record. Like an Incident or User, adding that to the end of the URL will display the app data in the respective language.

    Hope this helps!

  • Using JSON API requests, can add ?name=UsersName or another field, at the end of the url and the returned data will have what you need. Below is an example for Hardware (Computer) asset items.

    # Be sure to include the trailing slash (/)
    $UriBase = "https://api.samanage.com/"
    
    # the API's URI that we'll be connecting to
    # Be sure to OMIT the initial slash (/)
    $Uri = "hardwares.json?name=$BasePCName"
    
    #Combine the URI components to a single string
    $UriGet = $UriBase + $Uri
    
    # API Token for an account.
    # Things could be adjusted to use User/Pass if you prefer.
    $JsonWebToken = 'YourSuperSecretTokenHere'
    # Header is always the same when doing GET, PUT, POST.
    $Headers = @{ "X-Samanage-Authorization" = "Bearer $JsonWebToken";
                  "Accept" = "application/vnd.samanage.v2.1+json"
                  "Content-Type" = "application/json"
                  }
    
    $Response = ""
    try
    {
        $Response = Invoke-RestMethod -Uri $UriGet -Headers $Headers -Method GET
    } catch {
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Host "Failed to verify PC Name via API"
        Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
        Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
        Exit 1000
    }
    
    $PCid = $Response.id
    

  • Hi  

    I know how to get the user's ID. The issue is that the incident filter as best I can tell does not use the same ID. For example, I can see that one of my users ID is 8532955 using the /users/8532955 URL trick you indicate. Then if I go to an incident for that same user as the requestor, I'll see the following in the .json:

    "requester": {
    "id": 9220244,
    "account_id": 85721,
    "user_id": 8532955,

    The user_id matches there but whenever I try to filter the incidents using something like ?user_id=8532955 it doesn't work. If I build the filter with the GUI it will put in the filter requested_by=9220244, thus the question, where is this id coming from.

  • I understand how to filter and have used this successfully for other API calls. The issue is the API isn't working with the other filters and is using some other ID that I can't seem to get access to or is not exposed. 

  • Well I used a hack to get what I needed but it isn't pretty. If you load a user's profile in the web interface, there is a link like "See all X incidents" and that link include the requested_by id. The URL for this is basically: 

    /user_cards/{0}?klass=users&related=true where {0} is the user id of the user. 

    Extract out the link and pull out the requested_by_id and then post it over to the incidents API with the count that I want and I have the incident history for that user.

    API_URL = _configuration["SWSDApiUrl"] + "incidents.json?requested_by=" + requested_by_id + "&sort_by=number&sort_order=DESC&per_page=" + count;

    Edge cases:

    - If the user only has a singe incident, there will only be a link to the one incident. To solve this you can just use the that link and hit the incident API adding on the .json to the end of the link.

    - If the user doesn't have any tickets, there will be no links. 

    Hopefully there is a better way to do this, but for now I'm getting what I need.