API Price Sheets returning Error 404

Gustavo Assis 20 Reputation points
2024-07-25T16:48:52.26+00:00

Hi,

I want to retrieve the pricing list for my subscription plan using the Price Sheets API (https://learn.microsoft.com/en-us/rest/api/consumption/price-sheet/get-by-billing-period?view=rest-consumption-2023-05-01&tabs=HTTP).

I developed a Python code using requests lib, and authenticating at oauth2 to fetch the token, and passing it to the get method header. However, I am constantly getting error 404, as if the endpoint couldn`t understand the request. The role assignment was made to the client, and proper API access was granted, since I am the owner. So I am assuming the authentication part is ok.

I have as endpoint the url:

https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Billing/billingPeriods/{billingPeriodName}/providers/Microsoft.Consumption/pricesheets/default?api-version=2023-05-01

And as billingperiodname parameter I have "202407"

What am I doing wrong?

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
3,029 questions
{count} votes

1 answer

Sort by: Most helpful
  1. SadiqhAhmed-MSFT 48,351 Reputation points Microsoft Employee
    2024-07-31T15:00:54.6666667+00:00

    @Gustavo Assis Greetings!

    Thank you for providing your Python code. Based on the code you provided, it looks like you are using the correct API endpoint and version, and you are authenticating correctly using Azure AD.

    The issue may be related to the subscription ID or billing period name you are using. Make sure that the subscription ID and billing period name you are using are correct. You can find the subscription ID in the Azure portal, and the billing period name can be found in the Azure documentation. Reference link - https://learn.microsoft.com/en-us/rest/api/consumption/price-sheet/get-by-billing-period?view=rest-consumption-2023-05-01&tabs=HTTP&tryIt=true&source=docs#code-try-0

    Also, make sure that the account you are using to authenticate has the necessary permissions to access the Price Sheets API endpoint. You can check the permissions in the Azure portal.

    Confirm that the access token is valid and has not expired. An invalid token can lead to a 404 error.

    Make sure that the necessary resource providers, such as Microsoft.Billing and Microsoft.Consumption, are registered in your Azure subscription.

    Finally, you are printing token_data and pricesheet_data, but pricesheet_data is not defined in your code. You may want to change the last line to print(retail_prices_data) to print the retail prices data.

    Here's a slightly adjusted version of your Python code with some modifications:

    import requests

    # Define your Azure AD and API parameters

    tenant_id = 'xxx'

    client_id = 'yyy'

    client_secret = 'zzz'

    billingPeriodName = "202407" # Ensure this is a valid billing period

    # OAuth2 endpoint for token retrieval

    token_url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token'

    # Request headers and body for token retrieval

    token_data = {

    'grant_type': 'client_credentials',

    'client_id': client_id,

    'client_secret': client_secret,

    'scope': 'https://management.azure.com/.default'

    }

    # Get the access token

    response = requests.post(token_url, data=token_data)

    response.raise_for_status()

    access_token = response.json().get('access_token')

    # Endpoint to retrieve the current retail prices

    retail_prices_url = f'https://management.azure.com/providers/Microsoft.Billing/billingPeriods/{billingPeriodName}/providers/Microsoft.Consumption/pricesheets/default?api-version=2023-05-01'

    # Request headers for the API call

    headers = {

    'Authorization': f'Bearer {access_token}',

    'Content-Type': 'application/json'

    }

    # Get the current retail prices

    response = requests.get(retail_prices_url, headers=headers)

    # Check for errors

    if response.status_code == 404:

    print(f"Error 404: Resource not found. Verify the API endpoint and billing period.")

    else:

    response.raise_for_status()

    retail_prices_data = response.json()

    print(retail_prices_data)

    EDIT: Product team has confirmed that this API - https://learn.microsoft.com/en-us/rest/api/consumption/price-sheet/get-by-billing-period?view=rest-consumption-2023-05-01&tabs=HTTP&tryIt=true&source=docs#code-try-0 does not support MCA and only works with Enterprise Agreements.

    Hope this helps!


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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.