How to get Bunny CDN Billing and Stats via API

If you want to build a custom dashboard, you need to fetch the data from BunnyCDN API

Adjust code params as needed.

import json

import requests
import csv

url = "https://api.bunny.net/billing/summary"

headers = {
    "accept": "application/json",
    "AccessKey": "" # Your Key
}

response = requests.get(url, headers=headers)

data = response.json()

# Load the pull zone names from the JSON file
with open("pull_zones.json", "r") as f:
    pull_zone_names = json.load(f)

# Prepare the data for the CSV
csv_data = []
for pull_zone in data['PullZones']:
    pull_zone_id = str(pull_zone['PullZoneId'])  # PullZoneId needs to be string for JSON mapping
    monthly_usage = pull_zone['MonthlyUsage']
    bandwidth_used = pull_zone['MonthlyBandwidthUsed']

    # Get the name from the mapping file, default to "Unknown" if not found
    name = pull_zone_names.get(pull_zone_id, "Unknown")

    csv_data.append({
        "PullZone ID": pull_zone_id,
        "Name": name,
        "Monthly Usage (USD)": f"{monthly_usage:.2f}",
        "Monthly Bandwidth Used (Bytes)": f"{bandwidth_used:,}"
    })

# Define the CSV file path
csv_file_path = "pull_zones_billing_summary.csv"

# Write to CSV file
with open(csv_file_path, mode='w', newline='') as file:
    writer = csv.DictWriter(file,
                            fieldnames=["PullZone ID", "Name", "Monthly Usage (USD)", "Monthly Bandwidth Used (Bytes)"])
    writer.writeheader()
    writer.writerows(csv_data)

print(f"CSV file has been created: {csv_file_path}")

Pull Zone JSON

{
  "2448873": "PZ",
  "1334333423": "AQ",
  "132342343439": "Zone B",
  "23212343242649": "Zone C",
  "23423234": "Zone y",
  "13216023424": "Zone x"
}

Ready to simplify your digital operations?