Skip to content

Instantly share code, notes, and snippets.

@sfmishra
Created December 15, 2024 16:29
Show Gist options
  • Save sfmishra/326d926577293fe67a0ada27c2447330 to your computer and use it in GitHub Desktop.
Save sfmishra/326d926577293fe67a0ada27c2447330 to your computer and use it in GitHub Desktop.
public class ApexAPIUsageMonitor {
public static void fetchApiUsage() {
String endpoint = System.Url.getOrgDomainUrl().toExternalForm() + '/services/data/v62.0/limits';
// Set up HTTP request
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
try {
HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) {
// Parse the API response
Map<String, Object> result = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
System.debug('API Usage Details: ' + result.get('DailyApiRequests'));
} else {
System.debug('Failed to fetch API usage. Status: ' + res.getStatusCode());
}
} catch (Exception e) {
System.debug('Error fetching API usage: ' + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment