Public API: Example data retrieval script

To give you a more physical example of how using the API calls actually works we have created a PowerShell script as an example. The logic presented in the script can also be used in other scripting / programming languages like Python. 

The script is built to retrieve a list of available / configured environments. To achieve this yourself, using this script, we need the following items:

The script has been configured to ignore certificate errors in the event that you are using a self signed certificate. 

The authentication mechanisms within the script can be utilised for any API end point of your desire. Depending on the API call you can modify or retrieve data. For more info please see our API documentation.

The powershell file is attached to this article below.

# basic information
# role based access API
[string]$publicApiSecret = 'Mrayu0uASFiMR_sXf1Lcsz9VJbZB3vsl9erban8yvMk'

#url without https
$baseUrl = ''

# if something goes wrong (e.g. authentication) we stop processing this script
$ErrorActionPreference = 'Stop'

# WARNING: ignoring SSL/TLS certificate errors is a security risk
$code = @"
public class SSLHandler
{public static System.Net.Security.RemoteCertificateValidationCallback GetSSLHandler()
{return new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });}
Add-Type -TypeDefinition $code

# WARNING: ignoring SSL/TLS certificate errors is a security risk
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = [SSLHandler]::GetSSLHandler()

# this is only required for older version of PowerShell/.NET
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

# Define authorization header
$Header = @{
"Accept" = "application/json"
"Authorization" = "Bearer $publicApiSecret"

# Define body contents
$Body = @{
"orderBy" = "Name"
"direction" = "desc"
"count" = "20"

#Parameters of the API command
$Parameters = @{
Uri = 'https://' + $baseUrl + '/publicApi/v5/tests/'
Headers = $Header
Method = 'GET'
body = $Body
ContentType = 'application/json'

#request data with call to the public api environments URL
$Results = Invoke-RestMethod @Parameters

#display each environment in the results
ForEach ($item in $Results.items)
Write-Host $item