An Apex framework for HTTP API integrations in Salesforce, with a fluent Builder API, several authentication methods, a testing mock library, and the things production work needs anyway: timeouts and retries.
What is the Salesforce Integration Framework?
The Salesforce Integration Framework is a production-ready Apex library for HTTP API integrations. It has a fluent Builder-pattern API covering all HTTP methods, supports several authentication methods (Named Credentials, Bearer Token, Basic Auth, API Key) and content types (JSON, XML, form, multipart), and ships with a testing mock library.
Features
- All the HTTP methods: GET, POST, PUT, PATCH, DELETE
- Several authentication options, with Named Credentials recommended
- A builder pattern for readable, chainable requests
- Response parsing (JSON/XML), header access, and status checking built in
- A testing library with request capture and verification
- Timeouts, compression, retries, and error handling for production
Quick installation
Install the unmanaged package for Sandbox or Production using the following URLs:
- Sandbox: https://test.salesforce.com/packaging/installPackage.apexp?p0=04tgK0000003CXh
- Production: https://login.salesforce.com/packaging/installPackage.apexp?p0=04tgK0000003CXh
Before vs After (code example)
A plain Apex HttpRequest takes a lot of setup lines and leaves the error handling to you. The framework cuts the boilerplate, and the result reads better.
// Before HttpRequest req = new HttpRequest(); req.setEndpoint('https://api.example.com/users'); req.setMethod('POST'); req.setHeader('Content-Type', 'application/json'); req.setHeader('Authorization', 'Bearer ' + token); req.setBody(JSON.serialize(userData)); req.setTimeout(30000);
Http http = new Http(); HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) { Map result = (Map) JSON.deserializeUntyped(res.getBody()); // Process result } else { // Manual error handling }
// After - using the Integration Framework IntegrationResponse response = IntegrationFramework.newRequest() .method(HttpMethod.POST) .endpoint('/api/users') .withNamedCredential('My_API') .jsonBody(userData) .execute();
if (response.isSuccess()) { Map result = response.getBodyAsMap(); // Process result }
Authentication methods
Use Named Credentials in production. The framework also handles Bearer tokens, Basic Auth, and API keys.
Response handling and testing
The framework has helpers for:
- Checking status codes: isSuccess(), isClientError(), isServerError()
- Parsing bodies: getBodyAsMap(), getBodyAsXml(), raw body access
- Accessing headers for pagination and rate limiting
- Mocking HTTP callouts with IntegrationMockLibrary for unit tests
@isTest public class MyIntegrationTest { @isTest static void testSuccessfulCall() { IntegrationMockLibrary.MockHttpCallout mock = new IntegrationMockLibrary.MockHttpCallout(); Map mockData = new Map{'id' => '123', 'name' => 'Test'}; mock.setResponse('callout:My_API/api/users', IntegrationMockLibrary.jsonSuccessResponse(mockData)); Test.setMock(HttpCalloutMock.class, mock);
Test.startTest();
IntegrationResponse response = IntegrationFramework.get('My\_API', '/api/users', true);
Test.stopTest();
System.assert(response.isSuccess());
Map data = response.getBodyAsMap();
System.assertEquals('123', data.get('id'));
}
}
Best practices & recommendations
- Security: prefer Named Credentials; store secrets in Custom Settings or Custom Metadata; never hardcode secrets
- Performance: set sane timeouts, turn on compression for large payloads, and page through large datasets
- Testing: write unit tests with the mock library and verify the request details
- Error handling: check the status every time, retry transient errors, and log failures so monitoring picks them up
How teams typically use it
Common patterns: service classes that hold the API logic, trigger handler patterns for async callouts, batch processing for bulk syncs, and queueable or future methods for background work.
Conclusion
The Salesforce Integration Framework standardizes how a team builds and tests HTTP integrations in Apex, which mostly shows up as more consistent code and integrations you can actually test.
Admins get integrations that are easier to configure through Named Credentials, with fewer surprises in production. Developers write less boilerplate and keep the same patterns across projects, and unit tests stop being the hard part. Business users see integrations land sooner and data flows that break less often.
Leave a Comment