Sub topic 10
Finding who uses the API with Event Monitoring
This chapter assumes you can call the Salesforce REST API, write basic SOQL, and read the Limits resource output from earlier chapters. You know your org's daily API request count. Now you need to find out who is spending it.
Why the counters are not enough
The Limits resource and the Sforce-Limit-Info header tell you how much of the daily limit is used. They do not tell you which user, connected app, or client made the calls. When usage spikes, that attribution is what you need.
Event Monitoring answers that question. It records org activity as event log files. The ApiTotalUsage event type has one row per API call, with fields that identify the caller.
Event Monitoring is a paid add-on in most editions. You also need the "View Event Log Files" permission. If your queries return no rows, check access first.
How the pieces fit together
Salesforce generates log files after the activity happens. Files are not real time. Daily files usually appear the next day. Each file is a CSV stored as a record of the EventLogFile object, and the CSV itself sits in a LogFile field you download separately.
Step 1: List the available files
Use SOQL to list recent ApiTotalUsage files.
SELECT Id, EventType, LogDate, LogFileLength
FROM EventLogFile
WHERE EventType = 'ApiTotalUsage'
AND LogDate = LAST_N_DAYS:7
ORDER BY LogDate DESCEach row is one daily file. LogFileLength is the size in bytes. A large file means many API calls that day.
Step 2: Download a file
The log content is served from the LogFile field of a single record. Replace RECORD_ID with an Id from step 1.
curl -s -H "Authorization: Bearer $ACCESS_TOKEN" \
"$INSTANCE_URL/services/data/v60.0/sobjects/EventLogFile/RECORD_ID/LogFile" \
-o api_total_usage.csvStep 3: Group calls by caller
The CSV header lists the available columns. Column names vary by file version, so inspect the header before you rely on a name. Commonly useful columns include USER_ID, CLIENT_NAME, CONNECTED_APP_NAME, and API_FAMILY. This script counts calls per user and connected app. It reads the columns by name and stops with a clear message if one is missing.
import csv
from collections import Counter
counts = Counter()
with open("api_total_usage.csv", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
needed = {"USER_ID", "CONNECTED_APP_NAME"}
missing = needed - set(reader.fieldnames or [])
if missing:
raise SystemExit(f"Columns not found: {sorted(missing)}. Header: {reader.fieldnames}")
for row in reader:
key = (row["USER_ID"], row["CONNECTED_APP_NAME"] or "(none)")
counts[key] += 1
for (user_id, app), n in counts.most_common(10):
print(f"{n:>8} {user_id} {app}")The top rows are your largest consumers. A single integration user with a large count is a common finding. A connected app named (none) often means a user logged in with username and password and no connected app. Resolve the user ID to a name with a SOQL query on User.
Common errors
- No rows from the
EventLogFilequery. The add-on or permission is missing, or the files for the days you want are not generated yet. - Missing today's traffic. Logs lag. For current-day counts, use the Limits resource and the header instead.
- Column name mismatch. The script above fails fast with the real header, so you can adjust the names.
- Treating one call as one row without checking. Confirm with a day you know. Compare the row count with the used count from the Limits resource, because some calls may be logged under different event types.
Exercise
Your daily API usage hit 90% yesterday, and the Limits resource only shows totals. Which event type do you query in EventLogFile to find which connected app caused it, and why can't you use it to check today's usage?
Show answer
Query EventType = 'ApiTotalUsage', download the LogFile CSV, and group rows by user and connected app. You can't use it for today's usage because log files are generated after the activity, usually the next day. Use the Limits resource or the Sforce-Limit-Info header for current usage.