Search

PowerShell GET from REST API

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!

Share the Post:

Related Posts

PNG Press – Convert all those annoying .webp files quickly

PNG Press is a lightweight desktop application built using .NET Core 8. It features a simple drag-and-drop interface designed to convert one or multiple images to PNG format with ease. This project was born out of my own necessity for a straightforward image conversion tool, and I’m excited to finally share it with you.

Read More

Yu-Gi-Oh! Rarity Collection 2 Set Overview

The Yu-Gi-Oh! TCG set Rarity Collection 2 is getting mixed reviews to say the least. I’ve consulted with the Millennium items and searched the labyrinth of Atem’s mind to find this Yu-Gi-Oh! set’s stats, metrics, and other neat insights, let’s go!

Read More