Skip to main content
This example pulls 30 days of cost trend data, checks for cost anomalies, generates a 30-day spend forecast, and downloads a PDF report — all through the InfraAudit REST API.

Prerequisites

  • A connected AWS, GCP, or Azure provider with at least one completed billing sync
export TOKEN="eyJhbGciOi..."
export BASE_URL="https://api.infraaudit.dev"
export PROVIDER_ID=1

Step 1: Trigger a billing sync (if needed)

curl -s -X POST "$BASE_URL/api/v1/costs/sync" \
  -H "Authorization: Bearer $TOKEN"
# { "job_id": 55, "status": "running" }
Use the same polling pattern from the drift scan example to wait for it to finish.

Step 2: Get the cost overview

curl -s "$BASE_URL/api/v1/costs/overview" \
  -H "Authorization: Bearer $TOKEN" | jq .
Output:
{
  "current_month": {
    "amount": 4231.50,
    "currency": "USD",
    "from": "2024-01-01",
    "to": "2024-01-14"
  },
  "last_month": { "amount": 8450.00, "currency": "USD" },
  "mom_change_percent": -8.2
}

Step 3: Fetch 30-day trend by service

curl -s "$BASE_URL/api/v1/costs/trend?provider_id=$PROVIDER_ID&days=30&group_by=service" \
  -H "Authorization: Bearer $TOKEN" | jq '.data | last'
Output for the most recent day:
{
  "date": "2024-01-14",
  "amount": 285.40,
  "breakdown": { "EC2": 180.00, "S3": 45.00, "RDS": 60.40 }
}

Step 4: Check for anomalies

curl -s "$BASE_URL/api/v1/costs/anomalies?provider_id=$PROVIDER_ID&days=30" \
  -H "Authorization: Bearer $TOKEN" | jq '.data[] | {date, actual_amount, deviation_percent}'

Step 5: Get a 30-day forecast

curl -s "$BASE_URL/api/v1/costs/forecast?days=30&provider_id=$PROVIDER_ID" \
  -H "Authorization: Bearer $TOKEN" | jq '{projected_total, confidence_interval}'
Output:
{
  "projected_total": 8750.00,
  "confidence_interval": { "low": 7900.00, "high": 9600.00 }
}

Step 6: Generate and download a PDF report

# Request report generation
REPORT=$(curl -s -X POST "$BASE_URL/api/reports/generate" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"type\": \"cost_report\",
    \"provider_id\": $PROVIDER_ID,
    \"from\": \"2024-01-01\",
    \"to\": \"2024-01-31\",
    \"format\": \"pdf\"
  }")

REPORT_ID=$(echo $REPORT | jq -r '.report_id')

# Poll until ready
while true; do
  STATUS=$(curl -s "$BASE_URL/api/reports/$REPORT_ID" \
    -H "Authorization: Bearer $TOKEN" | jq -r '.status')
  if [ "$STATUS" = "ready" ]; then break; fi
  sleep 2
done

# Download
curl -s "$BASE_URL/api/reports/$REPORT_ID/download" \
  -H "Authorization: Bearer $TOKEN" \
  --output cost-report.pdf

echo "Downloaded cost-report.pdf"