Salesforce not only allows external systems to call into it via REST API, but it also allows you to call external REST APIs from Apex. This is useful when Salesforce needs to send or fetch data from external platforms like payment gateways, CRMs, or web services in real time.
In this article, you’ll learn how to:
- Write an Apex class to make a callout to an external REST API.
- Trigger the callout from a Lightning Component or Button
- Use HTML + JavaScript for the front-end interaction
⚙️ Apex Code to Call External REST API
public class CallExternalAPI {@future(callout=TRUE)
public static void sendGetRequest() {
HttpRequest req = NEW HttpRequest();
req.setEndpoint('https://jsonplaceholder.typicode.com/posts/1');
req.setMethod('GET');
Http http = NEW Http();
HttpResponse res = http.send(req);
System.debug('Response: ' + res.getBody());
}}
📌 Don’t forget to add the external domain in Remote Site Settings.
🌐 Trigger Apex from HTML + JavaScript (LWC/UI)
<!DOCTYPE html>
<html>
<head>
<title>TRIGGER API FROM Apex</title>
</head>
<body>
<h2>CALL External API via Apex</h2>
<button onclick="triggerCall()">CALL API</button>
<script>
FUNCTION triggerCall() {
fetch('/services/apexrest/triggerApi', {
method: 'POST',
headers: {'Content-Type': 'application/json'
}})
.then(res => res.text())
.then(DATA => alert('API Triggered via Apex!'));
}</script>
</body>
</html>
✅ Above assumes you’ve created a custom Apex REST endpoint or invoked Apex using @AuraEnabled methods.
✅ Conclusion
Apex is a strong tool for calling REST APIs when your business logic calls for external communication. This method enables you to automate and integrate outside of Salesforce, whether you’re delivering notifications or retrieving real-time data. We’ll demonstrate how to submit data from Apex to external APIs in the upcoming post.
How do I call an API from Apex in Salesforce?
To call an API from Apex, use a named credential, which specifies the URL of a callout endpoint and its required authentication parameters. By security policy, sessions created by Lightning components aren't enabled for API access. This restriction prevents even your Apex code from making API calls to Salesforce.
How to call rest API in Salesforce?
This apex class is used to insert leads into your salesforce account with First name, Last name, and Company name using APEX REST API. Now to call this web service from outside we need to authenticate our call as Salesforce uses OAuth so without authentication we cannot call this web service.
What is @HttpPost in Apex?
The @HttpPost annotation is used at the method level and enables you to expose an Apex method as a REST resource. This method is called when an HTTP POST request is sent, and creates a new resource. To use this annotation, your Apex method must be defined as global static.
Related Topics | You May Also Like
|
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course → |
