Integration — Callouts, REST APIs & Events
HTTP callouts with Named Credentials, exposing Apex REST services and Platform Events.
Developer 2 min readIntegrationRESTNamed CredentialsPlatform Events
Outbound: HTTP callout with a Named Credential
Named Credentials store the endpoint and authentication, so no secrets live in code.
apex
public with sharing class ExchangeRateService {
public static Decimal usdToInr() {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:ExchangeRates/latest?base=USD');
req.setMethod('GET');
req.setTimeout(10000);
HttpResponse res = new Http().send(req);
if (res.getStatusCode() != 200) {
throw new CalloutException('Rate service returned ' + res.getStatus());
}
Map<String, Object> body = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
Map<String, Object> rates = (Map<String, Object>) body.get('rates');
return (Decimal) rates.get('INR');
}
}
Rules:
- No callouts after uncommitted DML in the same transaction ("You have uncommitted work pending").
- No synchronous callouts from triggers — use
@future(callout=true)or a Queueable withDatabase.AllowsCallouts. - Up to 100 callouts per transaction, 120 s total timeout.
Inbound: an Apex REST service
apex
@RestResource(urlMapping='/accounts/*')
global with sharing class AccountApi {
@HttpGet
global static Account getAccount() {
String id = RestContext.request.requestURI.substringAfterLast('/');
return [SELECT Id, Name, Industry FROM Account WHERE Id = :id WITH USER_MODE];
}
@HttpPost
global static Id create(String name, String industry) {
Account a = new Account(Name = name, Industry = industry);
insert as user a;
return a.Id;
}
}
Called at /services/apexrest/accounts/<id> with an OAuth access token.
Standard APIs you should name in interviews
| API | Use |
|---|---|
| REST API | CRUD + SOQL over JSON (/services/data/vXX.X/sobjects/Account) |
| SOAP API | Enterprise/partner WSDLs |
| Bulk API 2.0 | Millions of records asynchronously |
| Metadata API | Deploy/retrieve configuration and code |
| Composite API | Several requests in one call |
| Streaming / Pub/Sub API | Change Data Capture and Platform Events |
Platform Events (event-driven)
apex
// Publish
EventBus.publish(new Order_Shipped__e(Order_Number__c = 'ORD-1001'));
// Subscribe with an after-insert trigger on the event
trigger OrderShippedTrigger on Order_Shipped__e (after insert) {
for (Order_Shipped__e ev : Trigger.new) {
System.debug('Shipped: ' + ev.Order_Number__c);
}
}
Interview questions
- Why use a Named Credential? → Endpoint + auth configured declaratively, no hard-coded secrets, no remote site setting needed.
- Can you make a callout from a trigger? → Not synchronously — use async Apex.
- Which API for loading 5 million records? → Bulk API 2.0.