A comprehensive TypeScript library for interacting with Azure's pricing APIs and calculating costs for Azure services.
Based on reverse-engineering the Azure Pricing Calculator web application.
- Full API Client: Fetch pricing data for any Azure service
- Price Calculator: Calculate costs with currency conversion, discounts, and billing options
- Estimate Management: Create, save, and manage pricing estimates
- Support Pricing: Calculate support tier costs including graduated premium pricing
- MCA/EA Support: Handle Enterprise Agreement and Microsoft Customer Agreement pricing
- Persistence: IndexedDB storage with localStorage fallback
npm install azure-pricing-calculatorimport {
AzurePricingApiClient,
AzurePricingCalculator,
HOURS_PER_MONTH
} from 'azure-pricing-calculator';
// Initialize the API client
const api = new AzurePricingApiClient();
// Fetch required data
const currencies = await api.fetchCurrencies();
const vmPricing = await api.fetchVmPricing('us-east');
// Create calculator with currency data
const calc = new AzurePricingCalculator(currencies);
// Get price for a VM
const price = calc.getPrice(vmPricing, 'standard-d2s-v3', {
region: 'us-east',
billingOption: 'payg',
quantity: HOURS_PER_MONTH,
});
console.log(`Monthly cost: ${calc.formatPrice(price.total)}`);import { AzurePricingApiClient } from 'azure-pricing-calculator';
const api = new AzurePricingApiClient({
baseUrl: 'https://azure.microsoft.com', // optional
culture: 'en-us', // optional
deploymentVersion: '1.0.0', // optional, for cache busting
});// Fetch pricing for a specific service
const storagePricing = await api.fetchServicePricing('storage');
// Fetch VM pricing for a region
const vmPricing = await api.fetchVmPricing('us-east');
// Fetch with discount options
const eaPricing = await api.fetchServicePricing('virtual-machines', {
discounts: { base: 'ea', additional: [] },
enrollment: 'your-enrollment-number',
showSkus: true,
showResourceIds: true,
});| Method | Description |
|---|---|
fetchServicePricing(slug, options?) |
Fetch pricing for any service |
fetchVmPricing(region, options?) |
Fetch VM pricing for a region |
fetchVmMetadata() |
Fetch VM series, sizes, OS options |
fetchRegions() |
Fetch available Azure regions |
fetchCurrencies() |
Fetch currency conversion rates |
fetchCategories() |
Fetch product categories |
fetchSupportPricing(options?) |
Fetch support tier pricing |
fetchUserInfo() |
Fetch authenticated user info |
initializeCalculator() |
Fetch all required initialization data |
import { AzurePricingCalculator } from 'azure-pricing-calculator';
const calc = new AzurePricingCalculator(currencies, {
currency: { value: 'USD', locked: false },
discounts: { base: 'payg', additional: [] },
});
// Get a single price
const price = calc.getPrice(pricingData, 'standard-d2s-v3', {
region: 'us-east',
billingOption: 'payg',
quantity: 730, // hours
});
console.log(price.value); // Unit price in USD
console.log(price.total); // Total in selected currency// Set currency
calc.setCurrency({ value: 'EUR', locked: false });
// Convert a price
const eurPrice = calc.convertToCurrency(100); // USD to EUR
// Format for display
const formatted = calc.formatPrice(eurPrice); // "€92.00"type BillingOption =
| 'payg' // Pay-as-you-go
| '1-year' // 1 Year Reserved
| '3-year' // 3 Year Reserved
| '1-year-savings' // 1 Year Savings Plan
| '3-year-savings' // 3 Year Savings Plan
// ... and more
// Get available billing options for a SKU
const options = calc.getAvailableBillingOptions(pricingData, 'standard-d2s-v3', 'us-east');
// Calculate savings
const savings = calc.calculateSavings(
pricingData,
'standard-d2s-v3',
'us-east',
730, // hours per month
'3-year'
);
console.log(`Save ${savings.percentage}% with 3-year reserved`);const supportCost = calc.calculateSupportCost(
supportPricing,
'premium', // tier: 'free' | 'developer' | 'standard' | 'prodirect' | 'premium'
5000, // monthly spend
10000 // upfront spend (reserved instances)
);const result = calc.calculateEstimate(
modules, // Array of CalculatorModule
supportPricing, // Support pricing data
'standard', // Support tier
10 // Markup percentage
);
console.log(result.moduleTotal); // Sum of all modules
console.log(result.supportTotal); // Support cost
console.log(result.markup); // Markup amount
console.log(result.estimatePrice); // Final monthly price
console.log(result.estimateUpfrontPrice); // Upfront costsimport { EstimateManager } from 'azure-pricing-calculator';
const manager = new EstimateManager(api, currencies);
await manager.initialize();
// Create a new estimate
const estimate = await manager.createEstimate('My Azure Estimate');
// Add a service
await manager.addModule(estimate.id, 'virtual-machines', 'v4', 'us-east');
// Get current totals
const totals = manager.getEstimateTotals(estimate.id);
console.log(`Monthly: ${totals.estimatePrice}`);import { storage } from 'azure-pricing-calculator';
// Get all saved estimates
const estimates = await storage.getEstimates();
// Save an estimate
await storage.upsertEstimate(estimate);
// Export all data
const backup = await storage.exportData();
// Import data
await storage.importData(backup);interface Estimate {
id: string;
estimateName: string;
modules: CalculatorModule[];
discounts: Discounts;
currentCurrency: CurrentCurrency;
supportLevel: SupportTier;
estimatePrice: number;
estimateUpfrontPrice: number;
// ... and more
}
interface PriceResult {
value: number; // Unit price in USD
total: number; // Total in selected currency
pricingType: string; // e.g., "Consumption"
sku: string[]; // Part numbers
resourceId: string[];// Resource GUIDs
region: string;
offerKind: 'v2-flat' | 'v2-graduated' | 'v3-flat' | 'v3-graduated';
}
interface ServicePricingResponse {
schema: ServiceSchema;
offers: Offers;
graduatedOffers?: GraduatedOffers;
skus?: SkuMappings;
regions?: Region[];
}| Discount | Description |
|---|---|
payg |
Pay-as-you-go (default, no discount) |
ea |
Enterprise Agreement (volume discounts) |
mca |
Microsoft Customer Agreement (negotiated rates) |
csp |
Cloud Solution Provider (partner pricing) |
devtest |
Dev/Test pricing (reduced rates) |
// Using EA pricing
const pricing = await api.fetchServicePricing('virtual-machines', {
discounts: { base: 'ea', additional: ['msdn'] },
enrollment: 'your-enrollment-number',
});The client interacts with these Azure endpoints:
| Endpoint | Purpose |
|---|---|
/api/v{2,3,4}/pricing/{service}/calculator/ |
Service pricing |
/api/v2/currencies/ |
Currency rates |
/api/v2/pricing/calculator/regions/ |
Available regions |
/api/v2/calculator/resources/ |
UI strings |
/api/v2/calculator/config/ |
Feature flags |
/user/info/ |
Authentication |
import {
HOURS_PER_MONTH, // 730
HOURS_PER_DAY, // 24
DAYS_PER_MONTH, // 31
BILLING_MONTHS, // { payg: 1, '1-year': 12, '3-year': 36, ... }
DEFAULT_REGION, // 'global'
} from 'azure-pricing-calculator';import { HttpError } from 'azure-pricing-calculator';
try {
const pricing = await api.fetchServicePricing('invalid-service');
} catch (error) {
if (error instanceof HttpError) {
console.log(`HTTP ${error.status}: ${error.message}`);
console.log(`URL: ${error.url}`);
}
}This library works in both environments:
- Browser: Uses native
fetchand IndexedDB - Node.js: Requires
node-fetchand uses localStorage fallback
// Node.js setup
import fetch from 'node-fetch';
globalThis.fetch = fetch;See the examples/ directory for complete examples:
basic-vm-pricing.ts- Simple VM price lookupfull-estimate.ts- Complete estimate with multiple servicescompare-billing.ts- Compare PAYG vs Reserved pricingexport-estimate.ts- Export estimate to JSON/Excel
MIT
This library is not officially supported by Microsoft. It is based on publicly accessible APIs from the Azure Pricing Calculator. Use at your own risk.