Azure cost management api
Skill fabioc-aloha/Alex_Skill_Mall/plugins/cloud-infrastructure/azure-cost-management-api
In PowerShell, inline JSON for Cost Management API causes "Unsupported Media Type" errors:From its SKILL.md
npx -y skills add fabioc-aloha/Alex_Skill_Mall --skill azure-cost-management-apiAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 4 stars4 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
2.2 KB, 518 tokens by cl100k_base, as published. Nobody here has run it
Azure Cost Management API
The Problem
In PowerShell, inline JSON for Cost Management API causes "Unsupported Media Type" errors:
# This fails silently or with 415 error
az rest --method POST --uri $uri --body '{"type":"Usage","timeframe":"MonthToDate"}'
The shell mangles the JSON.
The Solution
Write JSON to a file, then reference with @:
# 1. Create body file
$body = @{
type = "Usage"
timeframe = "MonthToDate"
dataset = @{
granularity = "Daily"
aggregation = @{
totalCost = @{
name = "Cost"
function = "Sum"
}
}
}
} | ConvertTo-Json -Depth 10
$bodyFile = "$env:TEMP\cost-query-body.json"
$body | Out-File -FilePath $bodyFile -Encoding utf8
# 2. Call with file reference
$uri = "/subscriptions/$subscriptionId/providers/Microsoft.CostManagement/query?api-version=2023-03-01"
az rest --method POST --uri $uri --body "@$bodyFile"
Common Queries
Monthly Cost by Resource Group
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": { "name": "Cost", "function": "Sum" }
},
"grouping": [
{ "type": "Dimension", "name": "ResourceGroup" }
]
}
}
Daily Cost Trend
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": { "name": "Cost", "function": "Sum" }
}
}
}
Verification
# Should return JSON with "rows" array
$result = az rest --method POST --uri $uri --body "@$bodyFile" | ConvertFrom-Json
$result.properties.rows | Format-Table
When to Apply
- Any Azure Cost Management API call from PowerShell
- Complex JSON bodies in az rest commands
- Budget alerts and cost analysis scripts
Tags
azure cost-management api powershell