你可以使用 Azure 帳單 與 消費 API 程式化取得帳單帳戶的信用餘額。
下列範例會使用 REST API。 目前,PowerShell 和 Azure CLI 都不被支援。
尋找您可以存取的帳單檔案
GET https://management.azure.com/providers/Microsoft.Billing/billingAccounts?$expand=billingProfiles&api-version=2019-10-01-preview
API 回應會返回一份帳單帳號及其帳單設定檔。
{
"value": [
{
"id": "/providers/Microsoft.Billing/billingAccounts/5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx",
"name": "5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx",
"properties": {
"accountId": "5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"accountStatus": "Active",
"accountType": "Enterprise",
"agreementType": "MicrosoftCustomerAgreement",
"billingProfiles": [
{
"id": "/providers/Microsoft.Billing/billingAccounts/5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx/billingProfiles/PBFV-xxxx-xxx-xxx",
"name": "PBFV-xxxx-xxx-xxx",
"properties": {
"address": {
"addressLine1": "AddressLine1",
"city": "City",
"companyName": "CompanyName",
"country": "Country",
"postalCode": "xxxxx",
"region": "Region"
},
"currency": "USD",
"displayName": "Development",
"hasReadAccess": true,
"invoiceDay": 5,
"invoiceEmailOptIn": true
},
"type": "Microsoft.Billing/billingAccounts/billingProfiles"
}
],
"displayName": "Contoso",
"hasReadAccess": true,
},
"type": "Microsoft.Billing/billingAccounts"
}
]
}
使用 displayName 帳單檔案的屬性來識別你想查詢信用餘額的帳單檔案。 複製帳單設定檔的 id 值。 例如,如果您想查詢帳單檔案的 Development 信用餘額,請複製 /providers/Microsoft.Billing/billingAccounts/5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx/billingProfiles/PBFV-xxxx-xxx-xxx。 將此值貼在某處,以便您在下一個步驟中使用。
取得一份信用名單
請提出以下請求。 將 <billingProfileId> 取代為您複製的 id 值 (/providers/Microsoft.Billing/billingAccounts/5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx/billingProfiles/PBFV-xxxx-xxx-xxx)。
GET https://management.azure.com/providers/Microsoft.Billing/billingAccounts/<billingAccountId>/billingProfiles/<billingProfileId>/providers/Microsoft.Consumption/lots?api-version=2023-03-01
API 回應會回傳原始信用額度及所有帳單批次的可用餘額。 這些資訊包括有效抵免、過期抵免,以及有未來起始日期的抵免。 你可以透過將 closedBalance 中所有活躍信用額度的值相加來計算可用餘額。
{
"value": [
{
"id": "/providers/Microsoft.Billing/billingAccounts/5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx/billingProfiles/PBFV-xxxx-xxx-xxx/providers/Microsoft.Consumption/lots/f2ecfd94-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"name": "f2ecfd94-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"type": "Microsoft.Consumption/lots",
"eTag": null,
"properties": {
"originalAmount": {
"currency": "USD",
"value": 500.0
},
"closedBalance": {
"currency": "USD",
"value": 500.0
},
"source": "Azure Promotional Credit",
"startDate": "09/18/2019 21:47:31",
"expirationDate": "09/18/2020 21:47:30",
"poNumber": ""
}
},
{
"id": "/providers/Microsoft.Billing/billingAccounts/5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx/billingProfiles/xxxx-xxxx-xxx-xxx/providers/Microsoft.Consumption/lots/4ea40eb5-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"name": "4ea40eb5-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"type": "Microsoft.Consumption/lots",
"eTag": null,
"properties": {
"originalAmount": {
"currency": "USD",
"value": 500.0
},
"closedBalance": {
"currency": "USD",
"value": 497.87
},
"source": "Azure Promotional Credit",
"startDate": "09/18/2019 21:47:31",
"expirationDate": "09/18/2020 21:47:30",
"poNumber": ""
}
}
]
}
| 元素名稱 |
Description |
creditCurrency |
信用貨幣。 |
billingCurrency |
用於帳單的貨幣。 |
originalAmount |
信用額度的原始金額,以美元計算。 |
originalAmountInBillingCurrency |
信用的原始金額,以計費時的貨幣和匯率計算。 |
expirationDate |
信用到期日期。 |
closedBalance |
截至最後一張發票,餘額以美元計算。 |
closedBalanceInBillingCurrency |
截至上一張發票的餘額,以帳單貨幣表示,並包含匯率。 |
reseller |
關於經銷商的資訊(如果有的話)。 |
source |
信用的來源,例如,Azure 促銷信用額度。 |
startDate |
信用啟動的日期。 |
expirationDate |
信用到期日期。 |
poNumber |
點數所計入之發票的採購單號碼。 |
isEstimatedBalance |
表示餘額是否被估算。 |
取得影響信用餘額的交易紀錄
請提出以下請求。 將<billingProfileId>替換為你之前複製的id值(providers/Microsoft.Billing/billingAccounts/5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx/billingProfiles/PBFV-xxxx-xxx-xxx)。 你需要傳遞一個 startDate 值和一個 endDate 值,才能讓交易持續時間達到你的需求。
GET https://management.azure.com/providers/Microsoft.Billing/billingAccounts/<billingAccountID>/billingProfiles/<billingProfileID>/providers/Microsoft.Consumption/events?api-version=2023-03-01&startDate=<date>&endDate=<date>
API 回應會回傳所有影響你帳單帳戶餘額的交易。
{
"value": [
{
"id": "/providers/Microsoft.Billing/billingAccounts/5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx/billingProfiles/PBFV-xxxx-xxx-xxx`/providers/Microsoft.Consumption/events/e2032eb5-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"name": "e2032eb5-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"type": "Microsoft.Consumption/events",
"eTag": null,
"properties": {
"transactionDate": "10/11/2019",
"description": "Credit eligible charges as of 10/11/2019",
"newCredit": {
"currency": "USD",
"value": 0.0
},
"adjustments": {
"currency": "USD",
"value": 0.0
},
"creditExpired": {
"currency": "USD",
"value": 0.0
},
"charges": {
"currency": "USD",
"value": -1.74
},
"closedBalance": {
"currency": "USD",
"value": 998.26
},
"eventType": "PendingCharges",
"invoiceNumber": ""
}
},
{
"id": "/providers/Microsoft.Billing/billingAccounts/5e98e158-xxxx-xxxx-xxxx-xxxxxxxxxxxx:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx_xxxx-xx-xx/billingProfiles/PBFV-xxxx-xxx-xxx/providers/Microsoft.Consumption/events/381efd80-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"name": "381efd80-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"type": "Microsoft.Consumption/events",
"eTag": null,
"properties": {
"transactionDate": "09/18/2019",
"description": "New credit added on 09/18/2019",
"newCredit": {
"currency": "USD",
"value": 500.0
},
"adjustments": {
"currency": "USD",
"value": 0.0
},
"creditExpired": {
"currency": "USD",
"value": 0.0
},
"charges": {
"currency": "USD",
"value": 0.0
},
"closedBalance": {
"currency": "USD",
"value": 1000.0
},
"eventType": "PendingNewCredit",
"invoiceNumber": ""
}
}
]
}
| 元素名稱 |
Description |
transactionDate |
交易發生的日期。 |
description |
交易說明。 |
adjustments |
交易中的信貸調整。 |
creditExpired |
已過期的信用額度。 |
charges |
交易費用。 |
closedBalance |
交易後的餘額。 |
eventType |
交易類型。 |
invoiceNumber |
該筆交易所計入之發票的發票號碼。 因為有待處理的交易,那個欄位是空的。 |