Created
December 15, 2024 16:29
-
-
Save sfmishra/326d926577293fe67a0ada27c2447330 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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