Sub topic 3
Monitoring Salesforce API Usage
This chapter assumes you know Salesforce orgs, REST APIs, basic SOQL, and basic Apex. Monitoring tells you which clients consume requests before an integration fails.
Monitor usage in Setup
In Setup, open the API usage information for your org. Depending on your Salesforce edition and permissions, this appears under Company Information or API Usage Notifications. Review the daily request total, the organization limit, and recent usage. Configure notification recipients and thresholds when the page supports them.
Use Setup for a quick operational check. Use the REST Limits resource for automated monitoring because a script can collect the value on a schedule.
Find API consumers with Event Monitoring
Event Monitoring records detailed activity such as the user, connected application, client type, and request count. Query recent ApiTotalUsage event log files with SOQL:
SELECT Id, EventType, LogDate, LogFileLength
FROM EventLogFile
WHERE EventType = 'ApiTotalUsage'
AND LogDate = LAST_N_DAYS:7
ORDER BY LogDate DESCDownload and analyze the log files to identify noisy integrations. Event Monitoring requires the appropriate license and permissions.
Handle limit errors
REQUEST_LIMIT_EXCEEDED means Salesforce rejected a request because a limit was reached. First identify the limit. A short burst or concurrent-request limit may clear quickly. A daily API limit may require waiting for the rolling window to release capacity.
Retry only transient failures. Use exponential backoff with jitter, cap the number of attempts, and preserve the request so it can be replayed safely.
import random
import time
import requests
def get_with_retry(url, headers, attempts=5):
for attempt in range(attempts):
response = requests.get(url, headers=headers, timeout=30)
if response.status_code != 403 or "REQUEST_LIMIT_EXCEEDED" not in response.text:
response.raise_for_status()
return response
if attempt == attempts - 1:
raise RuntimeError("Salesforce request limit remained exceeded")
time.sleep(min(60, 2 ** attempt) + random.random())
raise RuntimeError("unreachable")Reduce consumption
- Batch records and use Bulk API for large data movement.
- Request only needed fields and records.
- Cache stable reference data.
- Prefer event-driven synchronization over frequent polling.
- Combine related reads where a supported composite resource can replace several requests.
- Schedule heavy jobs instead of running many workers at once.
Build threshold alerts
Calculate usage as Max - Remaining, then alert at levels such as 70%, 85%, and 95%. Send the alert to the integration owner and include the client, current usage, limit, and time. Add a recovery notification when usage falls below the warning threshold.
Exercise
An integration reaches 95% of its daily API limit. What should you do first?
Show answer
Identify the consuming client, notify the owner, reduce or pause nonessential traffic, and avoid blind retries while checking whether the daily limit can recover.