Using Powershell to Pull API Information

Up until recently, I was familiar with the idea of client pulling down information from a database. A database requires updates, patching, and maintenance. Usually, I’d be the one doing all that care and feeding, additional to taking proper care of the client application. Recently I’ve been playing with api-ninjas.

API-ninjas requires a free account, and provides back to you an API key. Some of the things you can pull include:

  • Airline flight information
  • Cocktail recipes
  • Conversion of Currency
  • DNS lookup
  • Holidays
  • IP information

The list is quite exhaustive. An API is a great alternative to that pesky database maintenance above. Api-Ninja includes code on pulling the API’s in python, C#, ruby and so forth. However, it did not include anything about powershell.

Below, I’ve pasted code to get a random fact of the day:

$apikey  = "###supersecretapikey###"
$params = @{
    "Uri"  = "https://api.api-ninjas.com/v1/facts?limit=1"
    "method" = "get"
    "headers" = @{"X-API-Key" = $apikey
}
Invoke-RestMethod @Params | fl
##Sample output##
fact : A sneeze zooms out of your mouth at over 600 m.p.h

To use or change this code, change the Uri paramenter above to the value given by api-ninjas. Examples include:

https://api.api-ninjas.com/v1/cocktail?name=$input #for taking input for cocktail recipes

https://api.api-ninjas.com/v1/iplookup?address=$IP #for taking input for IP address