![]()
Dataverse API calls are limited based on capacity and entitlements. Monitoring API usage is crucial to:
Prevent throttling issues
Ensure compliance with allocated limits
Identify high-consuming users or apps
Optimize Power Platform performance
This guide explains how to monitor Dataverse API Usage using PowerShell step by step.
Step 1: Prerequisites
1. Required Permissions
- You must be a Power Platform admin or System Administrator.
- Ensure Power Platform API logging is enabled.
2. Install and Import PowerShell Modules
Ensure the Microsoft.PowerPlatform.Cds.Client module is installed.
# Install the required module
Install-Module -Name Microsoft.PowerPlatform.Cds.Client -Scope CurrentUser -Force
# Import the module
Import-Module Microsoft.PowerPlatform.Cds.Client
Step 2: Connecting to Dataverse Using PowerShell
Option 1: Interactive Login
Use this method for manual execution.
# Connect to Dataverse interactively
$connection = Connect-CdsService -ConnectionString "AuthType=OAuth;Url=https://yourorg.crm.dynamics.com;Prompt=Login"
Option 2: Using Service Principal (For Automation)
For scheduled monitoring, use an Azure AD App Registration.
# Define credentials
$clientId = "your-app-client-id"
$clientSecret = "your-app-client-secret"
$tenantId = "your-tenant-id"
$orgUrl = "https://yourorg.crm.dynamics.com"
# Convert secret to secure string
$secureSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($clientId, $secureSecret)
# Connect to Dataverse
$connection = Connect-CdsService -Url $orgUrl -ClientId $clientId -ClientSecret $secureSecret -TenantId $tenantId
Step 3: Fetching API Usage Data
Get Overall API Usage for an Environment
# Retrieve API usage for the current environment
$apiUsage = Get-CdsRecord -Connection $connection -EntityLogicalName "organization" -Fields "apiusage"
# Display API usage
Write-Host "Current API Usage: " $apiUsage.apiusage
This returns the total number of API calls consumed.
Step 4: Monitoring API Usage Per User or Application
Fetch API Usage by Users
# Get API consumption details by user
$users = Get-CdsRecord -Connection $connection -EntityLogicalName "systemuser" -Fields "fullname", "apiusage"
# Display results
$users | ForEach-Object {
Write-Host "User:" $_.fullname " | API Calls Used: " $_.apiusage
}
This lists API usage per user.
Fetch API Usage by Application
# Get API consumption details by application
$applications = Get-CdsRecord -Connection $connection -EntityLogicalName "applicationuser" -Fields "applicationid", "apiusage"
# Display results
$applications | ForEach-Object {
Write-Host "Application ID:" $_.applicationid " | API Calls Used: " $_.apiusage
}
This lists API usage per registered application.
Step 5: Fetching API Limits and Capacity
Retrieve API Call Limits
# Get API limits for the environment
$limits = Get-CdsRecord -Connection $connection -EntityLogicalName "organization" -Fields "apicalllimit"
Write-Host "Maximum API Calls Allowed: " $limits.apicalllimit
This shows the allocated API calls for the environment.
Calculate Remaining API Quota
# Get used and available API limits
$apiUsage = Get-CdsRecord -Connection $connection -EntityLogicalName "organization" -Fields "apiusage", "apicalllimit"
$remainingCalls = $apiUsage.apicalllimit - $apiUsage.apiusage
Write-Host "API Calls Used: " $apiUsage.apiusage
Write-Host "API Calls Remaining: " $remainingCalls
This helps track remaining API quota.
Step 6: Exporting API Usage Report to CSV
To analyze API usage trends, export the data to a CSV file.
# Get API usage details
$apiData = Get-CdsRecord -Connection $connection -EntityLogicalName "systemuser" -Fields "fullname", "apiusage"
# Convert to CSV format
$apiData | Select-Object fullname, apiusage | Export-Csv -Path "C:\DataverseAPIUsageReport.csv" -NoTypeInformation
Write-Host "API Usage Report saved at C:\DataverseAPIUsageReport.csv"
You can now open the CSV file for analysis.
Step 7: Setting Up an Automated Alert for High API Usage
Trigger Email Alert if Usage Exceeds 80%
# Get API usage data
$apiUsage = Get-CdsRecord -Connection $connection -EntityLogicalName "organization" -Fields "apiusage", "apicalllimit"
# Calculate API usage percentage
$usagePercentage = ($apiUsage.apiusage / $apiUsage.apicalllimit) * 100
# Define alert threshold
$threshold = 80
# Send an alert if usage exceeds 80%
if ($usagePercentage -gt $threshold) {
Send-MailMessage -To "admin@example.com" -From "alerts@example.com" -Subject "Dataverse API Limit Warning" -Body "Warning: API usage has exceeded 80%." -SmtpServer "smtp.example.com"
Write-Host "Alert sent to admin!"
} else {
Write-Host "API usage is within safe limits."
}
This script sends an email alert when usage exceeds 80%.
Step 8: Scheduling the Monitoring Script (Optional)
To automate monitoring, schedule this script in Task Scheduler or Azure Automation.
Using Windows Task Scheduler
1️⃣ Open Task Scheduler
2️⃣ Click Create Task
3️⃣ Go to the Triggers tab → Set Daily or Hourly
4️⃣ Go to Actions tab → Click New → Choose Start a Program
5️⃣ Enter:
- Program:
powershell.exe - Arguments:
-File "C:\Path\To\MonitorDataverseAPI.ps1"
6️⃣ Click OK and enable the task
The script will now run automatically.
