To call an external REST API from Apex, configure an External Credential for authentication, connect it to a Named Credential for the endpoint, grant users access to the credential principal, then send an Apex HTTP request through that Named Credential. Use Salesforce’s modern extensible Named Credentials rather than the legacy model, which Salesforce says is deprecated and will be discontinued in a future release.
Decide whether Apex is the right layer
For common Salesforce record and metadata access, first check whether Lightning Data Service (LDS) supports the entity and operation. LDS covers many typical use cases; Apex is appropriate when you need Salesforce APIs or entities outside LDS’s supported subset. For calls from Apex to external services, Salesforce’s Call APIs from Apex guidance explains the available approach and security considerations.
As an Amazon Associate I earn from qualifying purchases.
Be precise about which API you are calling. Salesforce advises using its standard REST or SOAP APIs—not Connect REST API—for sObject extraction, migrations, synchronization, analytics, and record queries. See Connect REST API Limits for the distinction and related limits guidance.
Plan the integration before configuring it
Before building the callout, establish the target API’s base endpoint, required HTTP method, resource path, request and response formats, authentication scheme, and expected error behavior. These choices determine the credential configuration and the shape of the Apex request. Salesforce’s REST API Quick Start is a starting point for understanding REST API requests.
#1 Best Overall
- Identify the service and the exact resource the callout must reach.
- Confirm whether the service expects JSON, another payload format, or no request body.
- Find out how the service authenticates requests and which access it grants.
- Agree on how your code should handle unsuccessful responses, throttling, and malformed payloads.
Configure modern Named Credentials
Salesforce separates the endpoint from authentication and access control. A Named Credential identifies the endpoint and transport configuration. An External Credential defines the authentication configuration; its principal maps access to permissions. User external credentials store tokens encrypted. Follow Salesforce’s Get Started with Named Credentials documentation for the current setup flow.
- Create an External Credential. Choose the authentication protocol and configure the principal appropriate for the external API.
- Create a Named Credential. Set its endpoint to the API host and associate it with the External Credential.
- Grant access selectively. Give the relevant users permission to use the External Credential principal. Avoid granting the principal more broadly than the integration requires.
- Verify in a non-production org. Test the configured credentials in a sandbox or testing org before using them in production.
Named Credentials let Apex make authenticated calls without embedding endpoint authentication details directly in the callout code. Salesforce cautions developers to review this capability carefully: sessions created through Lightning are not generally enabled for API access. Do not assume that a user’s Lightning session can stand in for the API authentication configured for an integration.
Rank #2
Send the request from Apex
In Apex, construct an HTTP request and direct it through the configured Named Credential, then inspect the HTTP response. The Named Credential supplies the configured endpoint and authentication context; your code is responsible for the resource path, method, any required headers or body, and interpreting the response.
The current Apex syntax for HttpRequest, HttpResponse, Http.send, and JSON parsing should be taken from the current Apex Developer Guide. The Salesforce sources cited here establish the credential architecture but do not verify a complete current Apex code example, so this guide does not provide unverified syntax.
Rank #3
Validate responses and plan for failures
Do not treat a completed HTTP exchange as proof that the operation succeeded. Check the returned status code, confirm that the payload has the shape your code expects, and define how failures are surfaced or recovered. Retry only when the operation and API semantics make a retry safe; avoid blindly repeating writes that could create duplicate effects.
For Connect REST API, Salesforce documents HTTP 503 responses when a rate limit is exceeded and recommends handling them gracefully. Most Connect REST API requests share Salesforce platform API limits; some Chatter resources instead have a per-user, per-application, per-hour limit. These limits can change without notice, so consult the current Connect REST API limits documentation rather than relying on a universal quota. These details concern Connect REST API and should not be mistaken for a fixed Apex callout allowance.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Test the integration without exposing production credentials
Salesforce recommends verifying target-org credentials in a sandbox or testing org and warns against testing credentials in production. Use test credentials and a test endpoint where possible, and verify both successful responses and relevant failure handling before deployment. Salesforce’s Test Credentials in the Target Org documentation describes this credential check.
Free tools Windows power users keep installed
One-click scans. No signup required.
For Apex tests, Salesforce’s 2018 Platform Developer II exam guide references Test.setMock() and HttpCalloutMock for callout testing. Because that guide is dated, verify the current syntax and test APIs against Salesforce’s current Apex documentation before adopting an example: Certified Platform Developer II Exam Guide (2018).
Quick Recap
Best Value
- Used Book in Good Condition
Choose the right Salesforce API
| Need | Approach | Why |
|---|---|---|
| Common Salesforce record or metadata access | Check Lightning Data Service first | LDS supports many typical record and metadata use cases. |
| Salesforce entity or operation outside LDS’s supported subset | Apex | Apex can handle other Salesforce APIs and entities. |
| External API call from Apex | Modern Named Credential with an External Credential | Separates endpoint configuration from authentication and permission mapping. |
| sObject extraction, migrations, synchronization, analytics, or record queries | Salesforce REST or SOAP API, rather than Connect REST API | Salesforce recommends the standard REST or SOAP APIs for these use cases. |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




