PowerShell Script to Query YNAB API

Share on:

In a post last week, I talked about how I've begun using YNAB in 2019 to manage my finances. As soon as I learned there was an API, you know I had to dig in and see what it could do. Below I've posted a short PowerShell script to query all credit cards and lines of credit and then dump them out to the console. Nothing fancy, but might be a good jumping off point if there was something you were looking to write.

 1$base = "https://api.youneedabudget.com/v1"
 2$key = "enter_your_api_key_here"
 3$headers = @{"Authorization"="Bearer $key"}
 4
 5$budgetList = $(Invoke-WebRequest "$base/budgets" -Headers $headers -UseBasicParsing).Content | ConvertFrom-Json
 6
 7$firstBudget = $budgetList.data.budgets | Select -First 1
 8
 9if(-not $($firstBudget.id)) {
10    Write-Error "Couldn't find first budget"
11    Exit
12}
13
14$accounts = $(Invoke-WebRequest "$base/budgets/$($firstBudget.id)/accounts" -Headers $headers -UseBasicParsing).Content | ConvertFrom-Json
15$accounts = $accounts.data.accounts
16
17$creditCards = $accounts | Where-Object { $_.type -in @("creditCard","lineOfCredit") -and $_.closed -eq $false }
18$creditcards | Select Name,@{Name="Due";Expression={$_.Balance/1000}} | % {
19    $acctName = $_.name
20    "$acctName`t$($_.Due)"
21}


No comments