PowerShell has a great command called Invoke-RestMethod. With this command, you can:
Delete
Get
Head
Merge
Options
Patch
Post
Put
Trace
For this guide, we will focus on Get
.
In this script, we are going to make a call to Coinbase’s free and public REST API for currency and cryptocurrencies’ exchange rates, since that’s a current topic. Basically, we are asking the Coinbase API what 1 USD is in all of their tracked currencies.
# Set the currency we want to convert to $Currency = "usd" # Set the endpoint URL the API is on, useing the $Currency variable to fill in our currency $RestURL = "https://api.coinbase.com/v2/exchange-rates?currency=$Currency" # Use Invoke-RestMethod with 'Get' to "get" the data and save it in memory to the $Response variable $Response = Invoke-RestMethod -Method Get -Uri $RestURL # Show the data for $Response in the PowerShell console $Response.data.rates
If everything goes well you will get a response like this (note I truncated for length)
AED : 3.6730001432470056 AFN : 86.9884427155008186 ALL : 115.4575756911001837 AMD : 481.6158500000022236 ANG : 1.8027240000000001 AOA : 409.2007999999973372 ARS : 117.859344301323914 AUD : 1.4345145999999999 AWG : 1.7999999999999999 AZN : 1.6999999999999999 BAM : 1.874639 BBD : 2.018492622005768 BDT : 87.5406845331367753 BGN : 1.8741739999999999 BHD : 0.3770022 BIF : 2042.9009193054136874 BMD : 1 BND : 1.3935169133934812 BOB : 6.8815911449887255 ...
In this case, at this time of writing, 1 USD = 3.6730001432470056 AED (crypto is near the bottom of the list in your output). That’s it! From here you can do whatever you want with that data (like convert it to a CSV). Pretty neat!