SOAP vs REST Integration – When to Use Each

Integrating external systems with Salesforce requires choosing between SOAP and REST APIs. This guide explains the differences and when to use each approach.

1. REST API Overview

Advantages

Example REST Callout


public class RESTCalloutExample {
    public static void callExternalAPI() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.example.com/users');
        request.setMethod('GET');
        request.setHeader('Authorization', 'Bearer token');
        request.setTimeout(120000);
        
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            System.debug('Response: ' + response.getBody());
        }
    }
}
  

2. SOAP API Overview

Advantages

Example SOAP Callout


public class SOAPCalloutExample {
    public static void callSOAPService() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.example.com/soap');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', 'http://example.com/action');
        
        String soapBody = ''
            + ''
            + ''
            + '123'
            + ''
            + '';
        
        request.setBody(soapBody);
        request.setTimeout(120000);
        
        HttpResponse response = http.send(request);
        System.debug('SOAP Response: ' + response.getBody());
    }
}
  

3. REST vs SOAP Comparison

Feature REST SOAP
Protocol HTTP Methods (GET, POST, PUT, DELETE) XML-based, typically over HTTP
Message Format JSON (preferred), XML, Plain Text XML (strict)
Performance Faster, lightweight Slower, verbose
Learning Curve Easy Steep
Caching Built-in HTTP caching Limited caching
Error Handling HTTP Status Codes SOAP Faults in XML

4. When to Use REST

5. When to Use SOAP

6. Best Practices

For REST Callouts:

For SOAP Callouts:

7. Decision Tree


Is it a modern API?
├─ YES → Use REST
└─ NO → Is it a legacy system?
        ├─ YES → Check if it has SOAP/WSDL
        │        ├─ YES → Use SOAP
        │        └─ NO → Use REST
        └─ NO → Use REST (modern approach)