In the previous article I discussed ways you can access Azure's billing and costing information. I discussed the advantages and shortcomings of each one. In this article I will show the basics for the route I took to access Azure billing through the REST API in cSharp. This will be concerned with Azure Enterprise accounts only.
All code can be found here on GitHub.
Before you begin utilizing the REST api you will need 3 things.
- Azure EA Enrollment Number: This can be found under your EA portal at https://ea.azure.com
- Azure API Key: This can be created under your EA portal at https://ea.azure.com
- Instructions to generate the key can be found at the below docs link.
- Your REST endpoint: This can be found here https://docs.microsoft.com/en-us/azure/billing/billing-enterprise-api
- I will be using "Usage Details by Custom Date" so I can get all the details.
To setup your C# environment you will need to include RestSharp and Newtonsoft-JSON into your project. These can be added via Nu-Get.
There are three basic steps to getting and reading your data.
Step One: Connection URL
Connection URL will look like this: https://consumption.azure.com/v3/enrollments/ + Enrollment Number + /Rest Endpoint + start date + end date
Example: https://consumption.azure.com/v3/enrollments/123456/usagedetailsbycustomdate?startTime=2018-01-01&endTime=2018-01-31
Step Two: GET Request
We now need to build a REST request utilizing the connection URL.
First a new RestClient is built, then a RestRequest is created. (Both of these commands require RestSharp library.)
var client = new RestClient(connection url);
var request = new RestRequest(Method.GET);
Then the headers are added to the RestRequest, this is for specifying data format and authentication.
request.AddHeader("Content-Type", "application/json");//Content Type
request.AddHeader("Authorization", "Bearer " + apiKey); //Auth Header
The REST request is then execute and it's data entered into a string to be parsed out. (IRestResponse is part of the RestSharp Library)
IRestResponse response = client.Execute(request); //Execute
string jsonResponse = response.Content; //Get content of the response.
The data now sits in the jsonResponse string waiting to be parsed out and be useful.
Step Three: Parse JSON
The content returned from our GET request needs to be parsed out to be readable and so functions can be performed against it. This is where teh Newtonsoft-JSON library comes into play.
First the response object needs to be deserialized. This requires a class that will flatten out all the returned variables. Refer to the GitHub for this class Data.cs.
Response costResponse = JsonConvert.DeserializeObject(jsonReponse);
Once the JSON is deserialized it can be looped through and you can perform whatever function you would like. Here is a small example
foreach (var item in costReponse.data) {Console.WriteLine(item.cost); }
All of the items in the JSON object are exposed now so it is a matter of saying item.fieldname and you will get it.
Note: You will most likely run into a time when your results return multiple pages. You will then have to take the next page URL that is given it to you and run it through GET and Parse commands again. Refer to the code in GitHub as there is an example of handling it there.
Once you have all your data sorted out, you can do anything with it whether that is dump it to a spreadsheet, database, json file, or even create alerts based on your results.