Regarding monthly cost details report generation - Cost Details API

Test Admin 171 Reputation points
2024-04-18T08:54:20.7333333+00:00

We are using Azure subscription with the Standard Tier.

We have a requirement to fetch the monthly usage billing cost of the following resources from Azure.

  1. GPT-turbo-instruct Model
  2. Azure Speech-to-text

We referred the following Microsoft documentation to generate the cost details report using the Cost Details API. but there are no straight-forward procedures mentioned.

https://learn.microsoft.com/en-in/azure/cost-management-billing/automate/understand-usage-details-fields

https://learn.microsoft.com/en-us/rest/api/cost-management/generate-cost-details-report/create-operation?view=rest-cost-management-2023-11-01&tabs=HTTP#examples

Kindly let us know the step-by-step procedure to fetch the cost details of the above resources using the Cost Details API.

Also, while trying to generate the Cost Details report using our resource subscription-id, it returns "AuthenticationFailed" error. How can we overcome this?

REST API Request:

POST https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version=2023-11-01

{
  "metric": "ActualCost",
  "timePeriod": {
    "start": "YYYY-MM-DD",
    "end": "YYYY-MM-DD"
  }
}

Error:

{
    "error": {
        "code": "AuthenticationFailed",
        "message": "Authentication failed. The 'Authorization' header is missing."
    }
}
Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
2,042 questions
0 comments No comments
{count} votes

Accepted answer
  1. SadiqhAhmed-MSFT 37,846 Reputation points Microsoft Employee
    2024-04-18T21:38:29.3233333+00:00

    Hello @Test Admin Thank you for posting your question on the Microsoft Q&A platform. Happy to answer any questions you may!

    From the details shared in the original post, I understand that you have a requirement to fetch the monthly usage cost for GPT-turbo-instruct Model and Azure Speech-to-text. However, when tried to with tried with cost details API, it errored out with authentication failure message.

    Regarding the "AuthenticationFailed" error you are receiving, it indicates that the API request is not being authenticated properly. Make sure that you are including the access token in the Authorization header of the API request.

    To fetch the monthly usage billing cost of the GPT-turbo-instruct Model and Azure Speech-to-text resources from Azure using the Cost Details API, you can follow these steps:

    1. Obtain an Azure AD access token for authentication. You can use the Azure CLI or PowerShell to obtain the access token. Here is an example of how to obtain an access token using the Azure CLI: az account get-access-token --resource https://management.azure.com/
    2. Use the access token to authenticate the API request. Include the access token in the Authorization header of the API request.
    3. Use the Cost Details API to generate a cost details report for the specified time period and resource. Here is an example of how to generate a cost details report for the GPT-turbo-instruct Model resource:
    POST https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version=2023-11-01
    
    {
      "metric": "ActualCost",
      "timePeriod": {
        "start": "YYYY-MM-DD",
        "end": "YYYY-MM-DD"
      },
      "aggregation": {
        "totalCost": {
          "name": "TotalCost",
          "function": "Sum"
        }
      },
      "filter": {
        "and": [
          {
            "dimension": {
              "name": "ResourceGroup",
              "operator": "In",
              "values": [
                "resource-group-name"
              ]
            }
          },
          {
            "dimension": {
              "name": "Resource",
              "operator": "In",
              "values": [
                "gpt-turbo-instruct-model"
              ]
            }
          }
        ]
      }
    }
    
    
    1. Replace the {subscription-id}, "YYYY-MM-DD", "resource-group-name", and "gpt-turbo-instruct-model" placeholders with the appropriate values for your environment.
    2. Repeat the above steps for the Azure Speech-to-text resource, replacing the "gpt-turbo-instruct-model" value with "azure-speech-to-text".

    Hope this helps. Feel free to write back to us if you have any further queries.


    If the response helped, do "Accept Answer" and up-vote it

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful