Query the Connect API Explorer from Powershell

Review examples on how to query the API using Windows PowerShell.

The examples in this topic illustrate how to query the API using Windows PowerShell.

Before you start

Before you start, do the following:

  • Obtain your application API token. Your token authenticates you. For more information, see Obtain an API token.
  • Obtain the URL of the API service for the portal from your system administrator. The URL of the API service is available in the Portal Management area, in the Settings section, on the Portal Options page. For more information, see Connect API Explorer URL.

Sample PowerShell GET request

Use the following sample PowerShell GET request as a starting point to perform operations in PowerShell.

$uriPrefix = "URL of the API service for the portal?q="

$token = "Your API token"
$query = "{ users { userName } }"

$uri = $uriPrefix + [uri]::EscapeDataString($query)
$request = [System.Net.WebRequest]::Create($uri)
$request.Method = "GET"
$request.Headers["Authorization"] = "Bearer $token"
 
    $result = $request.GetResponse()
    
    $stream = $result.GetResponseStream()
    
    $reader = New-Object IO.StreamReader($stream)
    
    $reader.readToEnd()

In the request, provide values for the following elements:

  • The URL of the API service for the portal, such as http://ringtail.com/Ringtail-Svc-Portal/api/query. Make sure that the URL is followed by ?q=. For more information, see Before you start.
  • Your API token.
  • The operation to run, formatted as valid JSON syntax. The sample request encodes the JSON query into valid syntax that can be passed as part of a URL.

Sample PowerShell POST request

Use the following sample PowerShell POST request as a starting point to perform operations in PowerShell.

$uri = "URL of the API service for the portal"

$token = "Your API token"
$body = "{""query"":""{ users { userName }}""}"

$request = [System.Net.WebRequest]::Create($uri)
$request.ContentType = "application/json"
 
  $request.Method = "POST"
$request.Headers["Authorization"] = "Bearer $token"
 
  $stream = $request.GetRequestStream()
  
  $stream.Write([byte[]][char[]]$body, 0, $body.Length)
  
  $result = $request.GetResponse()
  
  $stream = $result.GetResponseStream()
  
  $reader = New-Object IO.StreamReader($stream)
  
  $reader.readToEnd()

In the request, provide values for the following elements:

For information about how to create and test queries in the Connect API Explorer before running queries in a third-party application, see Access and use the Connect API Explorer.