Querying Steam Web API with PowerShell

Can we query it? YES WE CAN! One of the things I love about PowerShell is that it puts many tools that were previously only available to Linux admins more in the reach of Windows admins. One of those, to me, was “CURL”. Back in the day I heard “curl this, curl that” and Windows cmd just couldn’t do. I was too intermediated by PowerShell at the time to even try!

I wanted to query Steam as a part of a project to see how much time I’ve spent on gaming in that platform. I know you can get all your stats from the GUI but I wasn’t about to type all the down. Steam API to the rescue! Steam offers an API key if you have a custom domain.

Go straight to my Github repo if you don’t want to read the “how to”

Request an API Key

Go to https://steamcommunity.com/dev/apikey and login to your steam account.

Steam 3.png

Enter your domain, agree to the Steam Web API Terms and and click Register

Steam 1.png

Copy your steam key and save it somewhere safe!

Steam 2.png

Open your favorite code editor. We’ll use VS Code for this project. We will create a new file called “Get-SteamInfo” which will be the name of the PowerShell module.

We create a new variable called $key and give it the value of the API Key we were provided from Steam.

Reading through the Steam Web API Overview we learn that the API allows you to make regular API calls just using the URI. The Web API will also accept JSON blobs within the regular API call, however we don’t be using that feature just yet. For now our PowerShell module and functions will simply use regular calls.

All of the player-specific API calls all require that you have the Steam ID. so the first thing we want to do is figure out what the Steam ID of a given user is. Searching through the Web API documentation, we can see a method under the “ISteamUser” service, named “ResolveVanityURL”.

https://partner.steamgames.com/doc/webapi/ISteamUser#ResolveVanityURL

https://partner.steamgames.com/doc/webapi/ISteamUser#ResolveVanityURL

This method will take your authentication key, and “vanityURL” aka the player’s username, and return the Steam ID.

We’re going to use the PowerShell Invoke-RestMethod to call the ISteamUser interface, the ResolveVanityURL method on version 1. That translates to an API call:

Get-SteamInfo 4png.png

We have to provide the API key and the player’s vanityurl in the call, so, we’ll add those as arguments, with variables for their values:

Get-SteamInfo 5.png

When we use Invoke-RestMethod, on the URI we built above, we get a response! Next we can capture that returned data nto a variable, $response. We can now use dot notation to expand the response PowerShell object.

Get-SteamInfo 7.png

Now we want to see what games this player has! We can use the method GetOwnedGames.

Get-SteamInfo 3.png

This method requires the API key and steamid to work. We’ll create a new variable $steamID to capture the steamid from the previous API call. When submitting the call with the two most basic items, (using a new variable $responseOwnedGames to capture the response) we can get an array of items for this player., when again using dot-notation to expand the object.

Get-SteamInfo 8.png

But we are humans and we want to know what games this player plays! The method allows us to pass an argument include_appinfo to the API call, which will give us the actual game names:

Lastly, we plan to re-use this code so we’ll create functions to re-use that same code, so we can get the same information for other players! In this example I’ve created a function to get the player’s steamID from their username (aka vanity URL) and another function to get their owned games/playtime and export that to a csv in documents with their steamID as the beginning of the filename!

Get-SteamInfo 10.png

Please check my Github for a full module of PowerShell Steam commands!

Happy logging!

Edit: 12/22/2020 - 9:21AM - Spelling and grammar mistakes.