API usage ideas

Options
rmiller
rmiller Member Posts: 13 ✭✭✭
edited January 24 in Scripting and automations

In similar spirit to the shared script library, I would love to see a central repository for sharing ideas on how the API is being used in various environments. I am good with technical, but sometimes my creativity is lacking.

What are some of your best API usage ideas?

Comments

  • tanderson
    tanderson Member Posts: 200 ✭✭✭✭
    Options

    @rmiller I would love to see this and real-world guides to configure those ideas. I have never really played around with any API connections, so this would be very intriguing to me.

  • mjones
    mjones Member Posts: 143 ✭✭✭✭
    Options

    I like and support this suggestion.

    I also started using it to circumvent some of the limitations in the system.

    Even just some basic example scriptlets for various languages like PHP and Powershell would have been an amazing leg up. I do think that the API help site was very helpful once I figured it out.

    I found the API fairly hard to get into (not a hardcore programmer here).
    The lack of simple examples made it hard for me and took forever with trial and error before I got some working code. I have posted a couple of examples in the forums so that others can build on them.
    Once I got some working code it was easier to play around with it.

    Would love to see what people can share so that we can all grow.

  • cjeffers
    cjeffers Member Posts: 28 ✭✭✭
    Options

    Messed around with Power BI for about 30 minutes today just trying to learn how to query and was able to come up with a query that returns all tickets. If anyone has ideas for wallboards or whatnot let me know :) @MJones if you want a walkthrough on how this works I would be happy to send you documentation on how to connect Atera and Power BI

  • mjones
    mjones Member Posts: 143 ✭✭✭✭
    Options

    Haven't gotten into PowerBI yet, but hear it's pretty great.

    Better yet make a post and we can all see and share :)

  • tanderson
    tanderson Member Posts: 200 ✭✭✭✭
    Options

    Found this if anyone wants examples: https://support.atera.com/hc/en-us/articles/219083397

  • mjones
    mjones Member Posts: 143 ✭✭✭✭
    Options

    Wow, looks like they spruced it up a little last time I looked!

    Here was my sample for PowerShell if anyone finds it useful:

    $api_Key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    
    #Standard Header
    $headers = @{
    'X-API-KEY' = $api_key
    } #Get All Agents
    $machine_info = Invoke-RestMethod -Headers $headers -Uri https://app.atera.com/api/v3/agents/ | Select-Object -ExpandProperty items Write-Output $machine_info #Get Local Agent Info By Name
    $machine_info = Invoke-RestMethod -Headers $headers -Uri https://app.atera.com/api/v3/agents/machine/$env:computername | Select-Object -ExpandProperty items Write-Output $machine_info #Get Agent Info by Agent ID
    $agentID = 4 $machine_info = Invoke-RestMethod -Headers $headers -Uri https://app.atera.com/api/v3/agents/$agentID Write-Output $machine_info #Create an alert from a local machine
    #Get the local agent information from the API
    $machine_info = Invoke-RestMethod -Headers $headers -Uri https://app.atera.com/api/v3/agents/machine/$env:computername | Select-Object -ExpandProperty items #Build the alert
    $title = "Incorrect Time"
    $additional_info = "The agents time is off by more than 5 minutes"
    $severity = "Warning" #Available Options: ['Information', 'Warning', 'Critical']
    $alert_category = "General" #Available Options: ['Hardware', 'Disk', 'Availability', 'Performance', 'Exchange', 'General'] $postParams = @{
    DeviceGuid = $machine_info.DeviceGuid;
    CustomerID = $machine_info.CustomerID;
    Title = $title;
    Severity = $severity;
    AdditionalInfo = $additional_info;
    AlertCategoryID = $alert_category
    } #Create the Alert
    Invoke-RestMethod -Headers $headers -Uri https://app.atera.com/api/v3/alerts -Method POST -Body $postParams #Create a ticket
    #Build the ticket
    $ticket_title = "API Test"
    $ticket_description = "Ticket to test the API"
    $end_user_id = "1" #Target a specific user $postParams = @{
    TicketTitle = $ticket_title;
    Description = $ticket_description;
    EndUserID = $end_user_id
    } #Create the ticket
    Invoke-RestMethod -Headers $headers -Uri https://app.atera.com/api/v3/tickets -Method POST -Body $postParams

  • rmiller
    rmiller Member Posts: 13 ✭✭✭
    Options

    @MJones thank you for this. This really helps give me a start with the API. This is uncharted territory for me and I’m trying to lay some groundwork for our new ERP implementation kicking off soon. I want to leverage the API to initiate tickets based on workflows in our new system and wasn’t sure where to start.

  • mjones
    mjones Member Posts: 143 ✭✭✭✭
    Options

    Between what I posted and the API site, you should be able to get whatever you need from the API. It's not too bad at all once you have some formats to work off of.

  • rmiller
    rmiller Member Posts: 13 ✭✭✭
    Options

    @mjones I played around with your script the other day. Works well! This should be an easy add into our ERP system, as well as coming up with some custom automations within our onboarding scripts and packages I have already created. Thanks again!

  • mjones
    mjones Member Posts: 143 ✭✭✭✭
    Options

    It's not super fancy, but at least you don't have to figure out the quirky syntax to get it talking.

  • support-info
    support-info Member Posts: 9
    Options

    I hope this will be usefull to you guys
    It's a Powershell function that get all of your Atera Agents (not just the first 20)
    The agent are returned as a list of object that you can easily use or export
    (It's pretty fast, arround 30 second to get 3000 agents, tested on Powershell 5 and 7)

    $ATERA_APIKEY = "YOUR_API_KEY"

    function Get-AllAteraAgents { param ( [Parameter(Mandatory)] [string]$apiKey ) Write-Host "FETCHING ATERA AGENTS" -f Blue $rawList = New-Object System.Collections.ArrayList $getParam = @{ Headers = @{ "X-API-KEY" = "$($apiKey)" "Accept" = "application/json" } Body = @{ page = 1 itemsInPage = 50 } Method = "GET" URI = "https://app.atera.com/api/v3/agents" } $totalPages = (Invoke-RestMethod @getParam).totalPages Write-Host "NUMBER OF PAGES TO GET: $($totalPages)" -f Blue for ($page = 1; $page -le $totalPages; $page++) { # sleep -Milliseconds 100 Write-Progress -Activity "Processing Agents" -Status "$page out of $totalPages pages processed" -PercentComplete (($page / $totalPages) * 100) $body = @{ page = $page itemsInPage = 50 }
    # ADDRANGE IS USED INSTEAD OF ADD TO FLATTEN THE ARRAY $rawList.AddRange((Invoke-RestMethod -Headers $getParam.Headers -Body $body -Uri "https://app.atera.com/api/v3/agents").items) 1>$null } Write-Progress -Activity "Processing Agents" -Completed Write-Host "TOTAL NUMBER OF ATERA AGENTS: $($rawList.Count)" -f Blue return $rawList } ### EXEMPLE USAGE $agentsList = Get-AllAteraAgents -apiKey $ATERA_APIKEY # VISUALIZE WITH OUT-GRIDVIEW $agentsList | Out-GridView # EXPORT ALL THE AGENTS TO A CSV FILE $agentsList | Export-csv -Path ".\AteraAgentExport_$(Get-Date -F "dd-MM-yyyy").csv" -Delimiter ";"

  • stuarthill
    stuarthill Member Posts: 5
    edited May 9
    Options

    Thank you to @mjones and @support-info for sharing these, very useful.

    My intention is to try and build a ticket export method using the API to pull tickets for all contacts within a company, plus ticket comments (perhaps!) to export out to CSV… wish me luck.

    Of course should @rmiller or anyone else happens to have this or something approaching this please let me know :)

  • mjones
    mjones Member Posts: 143 ✭✭✭✭
    Options

    Once you've got the data it's really easy to play with since it comes in JSON format.

  • stuarthill
    stuarthill Member Posts: 5
    Options

    Ticket exporting via API

    I found that I didn't need to obtain the contacts first as the API allows collection from a company. This code retrieves the tickets for a specific customerID

    We've been using Atera since October '23, so around 6 months and this script takes around 30 seconds to process those 180 pages of data stripping out the html and css tags from the comments before presenting and saving as CSV.

    This achieves what I needed so I will probably not continue to collect each ticket comment.

    Sharing here as it may be useful for someone else.

    $ATERA_APIKEY = "?????????????????"
    function Get-AllAteraTickets {
    param (
    [Parameter(Mandatory)]
    [string]$apiKey,
    [Parameter(Mandatory)]
    [string]$customerId
    )
    Write-Host "FETCHING ATERA TICKETS FOR CUSTOMER $customerId" -f Blue
    $rawList = New-Object System.Collections.ArrayList $getParam = @{
    Headers = @{
    "X-API-KEY" = "$($apiKey)"
    "Accept" = "application/json"
    }
    Method = "GET"
    URI = "https://app.atera.com/api/v3/tickets"
    }

    $response = Invoke-RestMethod @getParam
    $totalPages = $response.totalPages
    Write-Host "NUMBER OF PAGES TO GET: $($totalPages)" -f Blue

    for ($page = 1; $page -le $totalPages; $page++) {
    Write-Progress -Activity "Processing Tickets" -Status "$page out of $totalPages pages processed" -PercentComplete (($page / $totalPages) * 100)
    $body = @{
    page = $page
    itemsInPage = 50
    customerId = $customerId
    }

    $rawList.AddRange((Invoke-RestMethod -Headers $getParam.Headers -Body $body -Uri "https://app.atera.com/api/v3/tickets").items) 1>$null
    }
    Write-Progress -Activity "Processing Tickets" -Completed
    Write-Host "TOTAL NUMBER OF ATERA TICKETS: $($rawList.Count)" -f Blue
    return $rawList } function Clean-HTML {
    param (
    [string]$text
    )
    $text = [System.Text.RegularExpressions.Regex]::Replace($text, '<[^>]+>', '')
    $text = $text -replace '\s+', ' '
    $text = $text.Trim()
    return $text
    } EXAMPLE USAGE $ticketsList = Get-AllAteraTickets -apiKey $ATERA_APIKEY -customerId "4" Clean specified fields $ticketsList | ForEach-Object {
    $.FirstComment = Clean-HTML -text $.FirstComment
    $.LastEndUserComment = Clean-HTML -text $.LastEndUserComment
    $.LastTechnicianComment = Clean-HTML -text $.LastTechnicianComment
    } VISUALIZE TICKETS WITH OUT-GRIDVIEW $ticketsList | Out-GridView EXPORT TICKETS TO A CSV FILE $ticketsList | Export-csv -Path ".\AteraTicketExport_$(Get-Date -F "dd-MM-yyyy").csv" -Delimiter ";"

  • bahlquist
    bahlquist Member Posts: 17
    Options

    you guys are amazing - thanks for sharing your samples! I'm just getting into using the API today with powershell and these examples have been a huge help. Thanks!