To consume REST services there is a lot of boilerplate code needed:

  1. Build Apex Objects to model the responses
  2. Code to deserialize REST responses to those classes
  3. Handling of HTTP requests, responses and errors
  4. ...

Did anyone come up with patterns or best practices codified into an open source library or framework that one could just reuse in a similar project without reinventing the callout-wheel over an over again?

Like the zillions of trigger-frameworks that exist as repos on Github...


The ffhttp library should do the trick: https://github.com/financialforcedev/ffhttp-core

It includes error handling, redirections, building header fields, oauth, mime attachments and more.

EDIT - Sample code

Say you want to consume the JSONPlaceholder /users rest resource. You could create a Named Credential for the URL.

JSONPlaceholder named credential

Then you can use ffhttp to make the request, handle the failure or successful response and deserialize into the intended format. FFHTTP only includes an OAuth client but it's pretty simple to create your own...

public with sharing class ffhttpSample {

    * DTO for remote object result
    private class RemoteUser {
        Integer id;
        String name;
        String username;
        String email;

    * Vanilla HTTP request
    public static RemoteUser[] getUsers() {
        Http http = new Http();

        HttpRequest request = new HttpRequest();

        HttpResponse response = http.send(request);        
        if (response.getStatusCode() == 200) {
            return (RemoteUser[])JSON.deserialize(response.getBody(), List<RemoteUser>.class);
        } else {
            return new RemoteUser[]{};

    * FFHTTP request
    public static RemoteUser[] getUsersFfhttp() {
        SimpleClient client = new SimpleClient();
        client.setCredentials(new ffhttp_Client.NamedCredentials('JSONPlaceholder'));

        SimpleClientRequest request = new SimpleClientRequest(
            new ffhttp_JsonDeserializer(List<RemoteUser>.class));

        // Execute and parse the response.
        // Alternatively you can use executeUnparsed() to return the response body as a String
        return (RemoteUser[])request.execute();

    * FFHTTP client implementations
    private class SimpleClient extends ffhttp_Client.AbstractClient {}
    private class SimpleClientRequest extends ffhttp_Client.AbstractClientRequest {
        SimpleClientRequest(IAbstractClient client, String endpoint, String requestMethod, ffhttp_IDeserialize deserializer) {
            super(client, endpoint, requestMethod, null, deserializer);
  • Awesome. Actually a fully sufficient answer but I will leave it open to invite more people to share. – Robert Sösemann Apr 3 at 20:59
  • would you mind adding a few snippets intro your answer outlining how to use the library? How it handles certain aspects of REST consumption, de/serializing responses and such. I checked the repo and also the specific repos using this but its still somewhat hard to grasp the core concepts. But.. I have no doubt that it is like the fflib the best thing out there... (I would grant a big bounty to you for that in case you care about some SFSE points ;-) – Robert Sösemann Apr 4 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.