Kaseya Community

Example code BMS Rest API

  • Has someone some (Powershell) example code that handles the authentication in combination with the BMS Rest API?

    We want to retrieve some ticket statistics for a dashboard page. For example nr. of open tickets, nr. of tickets that are almost over due etc.

  • This function should return an object containing the authentication details:

    function Get-BMSAuthentication

    {

     param(

       [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]

       [string]$Username,

       [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]

       [string]$Password,

       [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]

       [string]$Tenant,

       $URL = 'https://bms.kaseya.com/api/token'

     )

     $body = @{

       grant_type = 'password'

       username = $Username

       password = $Password

       tenant = $Tenant

     }

     $response = Invoke-WebRequest -Uri $URL -Method Post -Body $body

     Write-Debug $response

     $respJson = $response.Content | ConvertFrom-Json

     $BMSAuthentication = New-Object -TypeName PSObject

     $BMSAuthentication | Add-Member -MemberType NoteProperty -Name access_token -Value $respJson.access_token

     $BMSAuthentication | Add-Member -MemberType NoteProperty -Name token_type -Value $respJson.token_type

     $BMSAuthentication | Add-Member -MemberType NoteProperty -Name expires_in -Value $respJson.access_token

     $BMSAuthentication | Add-Member -MemberType NoteProperty -Name Bearer -Value ('Bearer {0}' -f $respJson.access_token)

     return $BMSAuthentication

    }

    The most important element of the BMSAuthentication object returned is the Bearer property. Use it as your authentication header value when invoking a REST method. For example, to get a list of employees:

    $URL = 'https://bms.kaseya.com/api/hr/employees'

    $headers = @{

    'authorization' = $BMSAuthentication.Bearer

    'accept' = 'application/json'

    'content-type' = 'application/json'

    }

    $result = Invoke-RestMethod -Uri $URL -Method Get -Headers $headers

    When doing PUT and POST requests, it seems that it is very important that you have the 'content-type' header present and set to 'application/json'.