# Working with the Azure billing and cost management API

A big part of working with Azure, is understanding and managing your costs. In the cloud, this is especially important, since a lot of services are billed by usage and costs are made up by many aspects, like service cost, data storage and outgoing data. Azure provides a lot of mechanisms to gain insights into costs and billing though the Azure portal and external tools. You can also get these insights programmatically, by using the Azure Billing and Cost Management APIs. Let's take a look at these APIs to see what they are and how you can use them.

# Azure Billing APIs

The Billing APIs are a group of APIs that you can use to pull usage and resource data from Azure to use in your own analytic tools. They also allow you to predict and manage costs. There are several Azure Billing APIs:

  • Azure Invoice Download API
    • Allows you to download invoices
  • Azure Resource Usage API
    • You can use this to get your estimated Azure consumption data
  • Azure Resource RateCard API
    • Allows you to get the list of available Azure resources and estimated pricing information for those resources

Let's try out the Azure Invoice Download API. We'll use this to download invoices from our Azure subscription. This API is for instance useful to to automatically send the invoices to the accounting department on the 15th of the every month.

  1. Before you can download invoices with the Invoice API, you need to grant permissions to users to do so. You can do that in the Azure portal:
    1. In the Azure portal, navigate to your subscription (search for subscriptions and open the one you want to grant permissions to)
    2. In the subscription, click on te Invoices menu
    3. In the invoices blade, click on the Access to invoices button
    4. Now switch the button to On and click Save to enable users to download invoices with the API

(Enable access to download invoices in the Azure portal)

  1. We'll use the invoice API through PowerShell in the Azure Cloud Shell (https://shell.azure.com). Open the Cloud Shell
  2. In the Cloud Shell, use the following commands to get the latest invoice, download it to the Cloud Shell Home drive and download it to your local machine
$invoice = Get-AzureRmBillingInvoice -Latest
Invoke-WebRequest -Uri $invoice.DownloadUrl -OutFile ($home + '\' + $invoice.Name + '.pdf')

Cd $home
download 201901-418250150656335.pdf //make sure to use your specific invoice name. You can find this by looking through the home directory with the **dir** command
1
2
3
4
5
  1. You'll end up with the invoice as a PDF file on your local machine. The invoice will contain all of the detailed information that you need, like the snippet in the image below

(The invoice as PDF)

You can now use the URL in the output to download the API. The API has more operations (opens new window), like listing all invoices and getting a specific invoice.

# Azure Cost Management APIs

Azure Cost Management (opens new window) is a new service in Azure that allows you to identify and manage costs using things like budgets and cost recommendations. The service has accompanying APIs: The Azure Cost Management APIs (opens new window)">. There are several APIs:

  • Dimensions
    • Using the Dimensions API, you can retrieve a list of dimensions that can be used as inputs for generating queries with the Query API
  • Query
    • Provides operations to get aggregated cost and usage data based on the query you supply. Using the Query API, you can specify your desired filtering, sorting and grouping on all available dimensions

Note that these APIs are currently only available for Enterprise subscriptions.

Let's take a look at how to call the Query API to get the usage of a resource group for a certain period in time.

  1. We'll try the API from the documentation. This is a very cool feature that you see more and more in Microsoft docs. Go to https://docs.microsoft.com/rest/api/cost-management/query/usagebyscope and click the Try it button

(Try it button in Microsoft docs)

  1. Under parameters, fill in the name of the Resource group that you want to query the usage for

(Fill in the query parameters in Microsoft docs)

  1. In the Body, fill in something like this:
{
	timeframe: "MonthToDate",
	type: "Usage",
    "dataset": {
        "granularity": "Daily"
    }
}
1
2
3
4
5
6
7
  1. Click OK to execute the request against your subscription
  2. This should result in something like this:
{
  "value": [
    {
      "id": "subscriptions/55312978-ba1b-415c-9304-c4b9c43c0481/resourcegroups/Azureoverview/providers/Microsoft.CostManagement/Query/9af9459d-441d-4055-9ed0-83d4c4a363fb",
      "name": "9af9459d-441d-4055-9ed0-83d4c4a363fb",
      "type": "microsoft.costmanagement/Query",
      "properties": {
        "nextLink": null,
        "columns": [
          {
            "name": "PreTaxCost",
            "type": "Number"
          },
          {
            "name": "ResourceGroup",
            "type": "String"
          },
          {
            "name": "UsageDate",
            "type": "Number"
          },
          {
            "name": "Currency",
            "type": "String"
          }
        ],
        "rows": [
          [
            2.10333307059661,
            "Azureoverview",
            20180417,
            "USD"
          ],
          [
            20.103333070596609,
            "Azureoverview",
            20180418,
            "USD"
          ]
        ]
      }
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# Conclusion

Getting detailed insights in your Azure costs and being able to manage your costs is very important. Azure provides lots of ways to do that. Azure also provides many APIs like the Billing and Cost Management APIs, so that you can get even more control over your costs and analyze them in your favorite tools.

All of these APIs are REST APIs. This means that you can call them using your favorite tools and that you can sometimes call them using PowerShell cmdlets, if they are available for the specific service. Because the APIs are REST APIs, you can use them from any application, regardless of which programming language you use. Go and check them out!