What is API?

The API is not the database or even the server, it is the code that governs the access point(s) for the server. APIs allow developers to interact with online services without actually having to point and click their way through a UI.

Where we can have this scenario?

You can use REST APIs to manage GitHub, Azure, work with Microsoft 365, or use cognitive services in Azure.  Still, we have a lot of other use cases. In this post, we will explain with a very simple guide on how you can make REST API calls from PowerShell.

How to interact with APIs through Powershell?

The two most common PowerShell methods of interacting with REST API’s are to use either Invoke-RestMethod or Invoke-WebRequest.  To interact with a REST API the best option is to use Invoke-RestMethod. Invoke-RestMethod turns input JSON or XML into native PowerShell objects to make further interaction easy. In this post, we will continue our travel with Invoke-RestMethod to access the API.

Example

Let’s start with a simple example. We’ll need an existing REST API to work. Browsing around on the Internet, I come across a Dummy REST API called dummy.restapiexample.com/api/v1/. Every REST API has a base URI and an endpoint. Our API has a base URI of http://dummy.restapiexample.com/ and has an endpoint of /API/v1/employees, making the endpoint URI http://dummy.restapiexample.com/api/v1/employees.

$GetAPI_EMPDetails = @{
“URI” = ‘http://dummy.restapiexample.com/api/v1/employees’
}
Invoke-RestMethod @GetAPI_EMPDetails

Once the above code is executed the GetAPI_EMPDetails will have the object of employee details. Post the, we can start our future manipulation based on the data which we GET from the API.

Output: