Azure Invoice API – download all invoices

Since end of April 2017, there is the new Azure Invoice API available. This API allows to download the Azure invoices for a subscription as PDF file. This does currently not work for Enterprise Agreements, but according to the blog post (https://azure.microsoft.com/en-us/blog/azure-billing-reader-role-and-preview-of-invoice-api/) it is planned.
The downloaded PDF is the invoice itself. The API does currently only support to create and download invoice pdfs. It does not support to access specific costs (e.g. per resources), because this is part of the billing API. I already blogged about the billing API here: Use the Azure Billing API and calculate the costs

Using the Invoice API

Usage of the Invoice API is quite simple as it is accessible via Powershell, but wait…some configuration is needed at first:

Go to the Azure portal, navigate to your subscription and select the Invoices option. There is a button “Access to invoice”, which configures that users/groups with subscription-level access are allowed to download invoices:

Give users access to download invoices

There is also a new role available – the Billing Reader role. This role is allowed to download invoices. It is configured per subscription, so navigate to the Access control (IAM) of your subscription and click the add button. Select the role “Billing Reader”, the members that should have access and save it:

Download the invoices

The easiest way to access the API is to use Powershell for it. If you want to download the latest invoice, then use the following code for it:

$subscriptionId = 'SUBSCRIPTIONID'
$downloadDirectory = "c:\data\invoices"
 
Login-AzureRmAccount
Get-AzureRmSubscription
Set-AzureRmContext -SubscriptionId $subscriptionId
$invoice = Get-AzureRmBillingInvoice -Latest
Invoke-WebRequest -Uri $invoice.DownloadUrl -OutFile ($downloadDirectory + '\' + $invoice.Name + '.pdf')

If you want to download all available invoices, use the following code for it:

$subscriptionId = 'SUBSCRIPTIONID'
$downloadDirectory = "c:\data\invoices"
 
Login-AzureRmAccount
Get-AzureRmSubscription
Set-AzureRmContext -SubscriptionId $subscriptionId
$invoices = Get-AzureRmBillingInvoice -GenerateDownloadUrl
foreach($invoice in $invoices)
{
  Invoke-WebRequest -Uri $invoice.DownloadUrl -OutFile ($downloadDirectory + '\' + $invoice.Name + '.pdf')
}

If you want to use C# to download your invoices, please check this GitHub repository: https://github.com/Azure-Samples/billing-dotnet-core-invoice-download

Issues and Troubleshooting

While trying the API, I got the following errors:

Get-AzureRmBillingInvoice : Operation returned an invalid status code ‘Unauthorized’
This happened when I didn’t active “Access to invoice” option. Please check the first configuration here.

Get-AzureRmBillingInvoice : Operation returned an invalid status code ‘BadRequest’
I got this error when I tried to access the invoice of a subscription which is part of an enterprise agreement. This is currently (May 2017) not supported.

Get-AzureRmBillingInvoice : The term ‘Get-AzureRmBillingInvoice’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
The Get-AzureRmBillingInvoice commandlet is available since April 2017, so you need to install or update the AzureRM module. Run the Powershell as administrator and execute: Install-Module AzureRM
If AzureRM is already installed, execute: Update-Module -Name AzureRM
If it doesn’t work, please check the following link: https://docs.microsoft.com/en-us/powershell/azure/install-azurerm-ps

Additional information

Azure Billing Reader role and preview of Invoice API: https://azure.microsoft.com/en-us/blog/azure-billing-reader-role-and-preview-of-invoice-api/
Azure Invoices API reference: https://docs.microsoft.com/en-us/rest/api/billing/invoices

Categories:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

About
about armin

Armin Reiter
Blockchain/Web3, IT-Security & Azure
Vienna, Austria

Reiter ITS Logo

Cryptix Logo

Legal information