Skip to content

Azure API Reference

This section provides the API reference documentation for Azure cost export endpoints, auto-generated from docstrings.

Interactive Documentation

Swagger UI

  • URL: /docs
  • Features: Request/response testing, parameter exploration

ReDoc

  • URL: /redoc
  • Features: Sidebar navigation, endpoint discovery

Azure Endpoints Reference

Export Azure cost data using POST request with JSON body.

Request Body Parameters: - start_date (str, optional): Start date in YYYY-MM-DD format - end_date (str, optional): End date in YYYY-MM-DD format - granularity (str, optional): Time granularity - "Monthly" or "Daily" (default: "Monthly") - group_by (str, optional): Group by dimension - "subscription", "ServiceName", or "ResourceGroupName" (default: "subscription")

Assumes Azure credentials are already configured in the environment.

Source code in api/main.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@app.post("/azure/cost/export")
async def export_azure_costs_post(request: Request):
    """
    Export Azure cost data using POST request with JSON body.

    Request Body Parameters:
    - start_date (str, optional): Start date in YYYY-MM-DD format
    - end_date (str, optional): End date in YYYY-MM-DD format
    - granularity (str, optional): Time granularity - "Monthly" or "Daily" (default: "Monthly")
    - group_by (str, optional): Group by dimension - "subscription", "ServiceName", or "ResourceGroupName" (default: "subscription")

    Assumes Azure credentials are already configured in the environment.
    """
    # Check if Azure module is available
    if not AZURE_AVAILABLE:
        return JSONResponse(
            {
                "error": True,
                "message": "Azure functionality not available. Please install Azure SDK: "
                "pip install azure-identity azure-mgmt-costmanagement azure-mgmt-resource",
            },
            status_code=503,
        )
    try:
        # Parse JSON body if present
        try:
            body = await request.json()
        except Exception:
            body = {}

        # Extract parameters with defaults
        start_date = body.get("start_date")
        end_date = body.get("end_date")
        granularity = body.get("granularity", "Monthly")
        group_by = body.get("group_by", "subscription")

        # Set default dates if not provided (Azure format: YYYY,MM,DD)
        if not start_date:
            default_start = (datetime.now() - timedelta(days=90)).strftime("%Y,%m,%d")
            start_date = default_start
        else:
            # Convert from YYYY-MM-DD to YYYY,MM,DD format for Azure
            start_date = start_date.replace("-", ",")

        if not end_date:
            default_end = datetime.now().strftime("%Y,%m,%d")
            end_date = default_end
        else:
            # Convert from YYYY-MM-DD to YYYY,MM,DD format for Azure
            end_date = end_date.replace("-", ",")

        print(f"Fetching Azure cost data from {start_date} to {end_date}")
        print(f"Granularity: {granularity}, Group by: {group_by}")

        # Fetch subscriptions first (required because eraXplor_azure doesn't handle None)
        subscriptions_list_detailed = list_subs()
        if not subscriptions_list_detailed:
            return JSONResponse(
                {"error": True, "message": "No Azure subscriptions found or accessible"},
                status_code=404,
            )

        # Call eraXplor Azure cost_export function
        cost_data = azure_cost_export(
            group_by=group_by,
            subscriptions_list_detailed=subscriptions_list_detailed,
            start_date=start_date,
            end_date=end_date,
            granularity=granularity,
        )

        # Handle case where Azure function returns None
        if cost_data is None:
            cost_data = []
            print("No cost data returned from Azure function")

        print(f"Retrieved {len(cost_data)} cost records")

        response_data = {
            "success": True,
            "message": "Azure cost data exported successfully",
            "total_records": len(cost_data),
            "cost_data": cost_data,
            "request_parameters": {
                "start_date": start_date.replace(",", "-"),  # Convert back for response
                "end_date": end_date.replace(",", "-"),  # Convert back for response
                "granularity": granularity,
                "group_by": group_by,
            },
        }

        return response_data

    except Exception as e:
        print(f"Error exporting Azure costs: {str(e)}")
        return JSONResponse(
            {"error": True, "message": f"Error exporting Azure costs: {str(e)}"},
            status_code=500,
        )

Export Azure cost data using GET request with query parameters.

Query Parameters: - start_date (str, optional): Start date in YYYY-MM-DD format - end_date (str, optional): End date in YYYY-MM-DD format - granularity (str, optional): Time granularity - "Monthly" or "Daily" (default: "Monthly") - group_by (str, optional): Group by dimension - "subscription", "ServiceName", or "ResourceGroupName" (default: "subscription")

Assumes Azure credentials are already configured in the environment.

Source code in api/main.py
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
@app.get("/azure/cost/export")
async def export_azure_costs_get(
    start_date: str = None,
    end_date: str = None,
    granularity: str = "Monthly",
    group_by: str = "subscription",
):
    """
    Export Azure cost data using GET request with query parameters.

    Query Parameters:
    - start_date (str, optional): Start date in YYYY-MM-DD format
    - end_date (str, optional): End date in YYYY-MM-DD format
    - granularity (str, optional): Time granularity - "Monthly" or "Daily" (default: "Monthly")
    - group_by (str, optional): Group by dimension - "subscription", "ServiceName", or "ResourceGroupName" (default: "subscription")

    Assumes Azure credentials are already configured in the environment.
    """
    # Check if Azure module is available
    if not AZURE_AVAILABLE:
        return JSONResponse(
            {
                "error": True,
                "message": "Azure functionality not available. Please install Azure SDK: "
                "pip install azure-identity azure-mgmt-costmanagement azure-mgmt-resource",
            },
            status_code=503,
        )
    try:
        # Set default dates if not provided (Azure format: YYYY,MM,DD)
        if not start_date:
            default_start = (datetime.now() - timedelta(days=90)).strftime("%Y,%m,%d")
            start_date = default_start
        else:
            # Convert from YYYY-MM-DD to YYYY,MM,DD format for Azure
            start_date = start_date.replace("-", ",")

        if not end_date:
            default_end = datetime.now().strftime("%Y,%m,%d")
            end_date = default_end
        else:
            # Convert from YYYY-MM-DD to YYYY,MM,DD format for Azure
            end_date = end_date.replace("-", ",")

        print(f"Fetching Azure cost data from {start_date} to {end_date}")
        print(f"Granularity: {granularity}, Group by: {group_by}")

        # Fetch subscriptions first (required because eraXplor_azure doesn't handle None)
        subscriptions_list_detailed = list_subs()
        if not subscriptions_list_detailed:
            return JSONResponse(
                {"error": True, "message": "No Azure subscriptions found or accessible"},
                status_code=404,
            )

        # Call eraXplor Azure cost_export function
        cost_data = azure_cost_export(
            group_by=group_by,
            subscriptions_list_detailed=subscriptions_list_detailed,
            start_date=start_date,
            end_date=end_date,
            granularity=granularity,
        )

        # Handle case where Azure function returns None
        if cost_data is None:
            cost_data = []
            print("No cost data returned from Azure function")

        print(f"Retrieved {len(cost_data)} cost records")

        response_data = {
            "success": True,
            "message": "Azure cost data exported successfully",
            "total_records": len(cost_data),
            "cost_data": cost_data,
            "request_parameters": {
                "start_date": start_date.replace(",", "-"),  # Convert back for response
                "end_date": end_date.replace(",", "-"),  # Convert back for response
                "granularity": granularity,
                "group_by": group_by,
            },
        }

        return response_data

    except Exception as e:  # Pylint: disable=broad-except
        print(f"Error exporting Azure costs: {str(e)}")
        return JSONResponse(
            {"error": True, "message": f"Error exporting Azure costs: {str(e)}"},
            status_code=500,
        )

Authentication

  • Azure CLI authentication (az login)
  • Azure subscription access
  • Cost Management API permissions

Rate Limiting

Azure Cost Management has its own quotas - no built-in limiting.