Skip to content

Azure Cost Export API

This section documents the Azure cost export endpoints for the eraXplor API.


How to Run Locally

Prerequisites

  • Python 3.12+
  • pip
  • Git
  • Azure CLI

Setup Instructions

  1. Clone the repository:
git clone https://github.com/Mohamed-Eleraki/eraXplor.git
cd eraXplor
  1. Create a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
cd api
pip install -r requirements.txt
  1. Configure Azure credentials:
az login
  1. Run the API server:
cd api
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
  1. Access the API:
  2. Swagger UI: http://localhost:8000/docs
  3. ReDoc: http://localhost:8000/redoc
  4. Health check: http://localhost:8000/

Docker (Optional)

docker build -t eraxplor-api ./api
docker run -p 8000:8000 eraxplor-api

API Information

  • API Name: eraXplor API
  • Version: 2.0.0
  • Base URL: http://localhost:8000

Root Endpoint

  • GET / - Welcome message

Interactive Documentation

  • GET /docs - Swagger UI
  • GET /redoc - ReDoc

Azure Cost Export Endpoints

POST /azure/cost/export

Export Azure cost data using JSON request body.

Request Body:

{
  "start_date": "2025-08-01",
  "end_date": "2025-10-30",
  "granularity": "Monthly",
  "group_by": "subscription"
}

Parameters: - start_date (string, optional): Start date in YYYY-MM-DD format. Defaults to 90 days ago. - end_date (string, optional): End date in YYYY-MM-DD format. Defaults to today. - granularity (string, optional): "Monthly" or "Daily". Default: "Monthly" - group_by (string, optional): Group by dimension. Options: "subscription", "ServiceName", "ResourceGroupName". Default: "subscription"

GET /azure/cost/export

Export Azure cost data using query parameters.

Query Parameters: - start_date (optional): YYYY-MM-DD format - end_date (optional): YYYY-MM-DD format - granularity (optional): Monthly or Daily - group_by (optional): subscription, ServiceName, or ResourceGroupName

Example (Specific Subscription):

curl "http://localhost:8000/azure/cost/export?start_date=2025-08-01&end_date=2025-10-30&granularity=Monthly"

Example (All Subscriptions):

curl "http://localhost:8000/azure/cost/export?granularity=Monthly"

Response Format

{
  "success": true,
  "message": "Azure cost data exported successfully",
  "total_records": 31,
  "cost_data": [
    {
      "TIME_PERIOD": {"start": "2025-08-01", "end": "2025-09-01"},
      "GROUP_BY": "SUBSCRIPTION_ID",
      "SUBSCRIPTION_ID": "12345678-1234-1234-1234-123456789abc",
      "DISPLAY_NAME": "My Subscription",
      "PreTaxCost": "123.45 USD",
      "TAGS": {"environment": "production"}
    }
  ],
  "request_parameters": {
    "start_date": "2025-08-01",
    "end_date": "2025-10-30",
    "granularity": "Monthly",
    "group_by": "subscription"
  }
}

Error Responses

Azure Module Not Available (503 Service Unavailable)

{
  "error": true,
  "message": "Azure functionality not available. Please install Azure SDK: pip install azure-identity azure-mgmt-costmanagement azure-mgmt-resource"
}

General Error (500 Internal Server Error)

{
  "error": true,
  "message": "Error exporting Azure costs: [detailed error message]"
}

No Subscriptions Found (404)

{
  "error": true,
  "message": "No Azure subscriptions found or accessible"
}

Testing Azure Endpoint

Test Azure Endpoint (Specific Subscription)

curl -X POST "http://localhost:8000/azure/cost/export" \
  -H "Content-Type: application/json" \
  -d '{
    "subscription_id": "your-subscription-id",
    "start_date": "2025-08-01",
    "end_date": "2025-10-30",
    "granularity": "Monthly"
  }'

Test Azure Endpoint (All Subscriptions)

curl -X POST "http://localhost:8000/azure/cost/export" \
  -H "Content-Type: application/json" \
  -d '{
    "start_date": "2025-08-01",
    "end_date": "2025-10-30",
    "granularity": "Monthly"
  }'

Authentication Requirements

  • Azure CLI logged in (az login)
  • Or environment variables:
  • AZURE_CLIENT_ID
  • AZURE_CLIENT_SECRET
  • AZURE_TENANT_ID
  • Appropriate RBAC permissions for Cost Management API

Dependencies

fastapi>=0.68.0
uvicorn>=0.15.0
azure-identity>=1.15.0
azure-mgmt-costmanagement>=1.0.0
azure-mgmt-resource>=23.0.0

Features

  • Azure Cost Export - Subscription-based cost analysis
  • Multi-subscription support
  • Multiple grouping dimensions (subscription, ServiceName, ResourceGroupName)
  • Flexible date ranges
  • Subscription tags support