Skip to main content

Examples of working with RESTful APIs in PowerShell.

# ----------------------------------------------------------------------------
# Reading RESTful APIs
# ----------------------------------------------------------------------------

#region - Basic query to Stack Exchange -

$baseURL = 'https://api.stackexchange.com'
$version = '2.2'
$method  = 'questions/unanswered'

#region - Parameters -

$parameters = @(
    'order=desc'
    'sort=activity'
    'site=stackoverflow'
) -join '&'

#endregion

#region - Building the URL and executing the query -

$url = "$baseURL/$version/$method`?$parameters"
$result = Invoke-RestMethod -Method Get -Uri $url

#endregion

#region - Looking at the results -

$result | Get-Member

$result.has_more
$result.quota_max
$result.quota_remaining
$result.items
$result.items.Count

#endregion

#region - Formatting the results -

$result.items | Format-Table Title,tags
$result.items | Where-Object tags -Contains 'powershell'

# Launches the web browser and navigate to the matched result
# Start-Process ($result.items | Where-Object tags -Contains 'sql')[0].link

#endregion

#endregion

#region - Adding other parameters -

$baseURL = 'https://api.stackexchange.com'
$version = '2.2'
$method  = 'questions/unanswered'

$parameters = @(
    'order=desc'
    'sort=activity'
    'site=stackoverflow'
    'tagged=powershell-core'
    'pagesize=50'
) -join '&'

$url = "$baseURL/$version/$method`?$parameters"
$result = Invoke-RestMethod -Method Get -Uri $url

#region - Looking at the results -

$result.quota_remaining
$result.items.count
$result.has_more
$result.items | Format-Table Title

# Launches the web browser and navigate to the matched result
# Start-Process ($result.items | Where-Object tags -Contains 'powershell')[0].link
# Start-Process $result.items[0].link

#endregion

#endregion

# ----------------------------------------------------------------------------
# Manipulating RESTful APIs
# ----------------------------------------------------------------------------

#
# Get App Key @ https://trello.com/app-key
# Auth string format: key=<APIKey>&token=<AccessToken>
# I've stored my auth string in $AuthString
# Docs: https://developers.trello.com/
#

$PSVersionTable

$baseUrl = 'https://api.trello.com/1'

#region - Get Board ID -

$BoardsIDURL = "$baseUrl/members/me/boards?$AuthString"
$boards = Invoke-RestMethod -Uri $BoardsIDURL -Method Get
$board = $boards | Where-Object Name -eq "REST API testing"
$board

#endregion

#region - Get List ID -

$listIDURL = "$baseUrl/board/$($board.id)/lists?$AuthString"
$lists = Invoke-RestMethod -Uri $listIDURL -Method Get
$list = $lists | Where-Object Name -eq "First List"
$list

#endregion

#region - Create card -

$createCardURL = "$baseURL/cards?$AuthSTring"
$body = @{
    'idList' = $list.id
    'name'   = 'PS Core card'
    'desc'   = 'This is the description'
}
$card = Invoke-RestMethod -Uri $createCardURL -Method Post -Body $body
$card

#endregion

#region - Move the card -

$list02 = $lists | Where-Object Name -eq 'Second list'
$list02
$moveCardURL = "$baseURL/cards/$($card.id)?idList=$($list02.id)&$AuthString"
Invoke-RestMethod -Uri $moveCardURL -Method Put

#endregion

#region - Add comment -

$commentURL = "$baseUrl/cards/$($card.id)/actions/comments?$AuthString"
$body = @{text = "This card's move was initiated from PS Core."}
Invoke-RestMethod -Uri $commentURL -Method Post -Body $body

#endregion