Named Credentials Patterns

Overview

Named Credentials should be the default integration boundary for outbound callouts from Salesforce.

They separate endpoint/auth configuration from Apex logic, reduce secret sprawl, and make environment promotion safer.

Consensus Best Practices

Core Patterns

Pattern 1: Service Class + Named Credential Endpoint

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:BillingApi/v1/invoices');
req.setMethod('GET');
req.setTimeout(120000);
HttpResponse res = new Http().send(req);

Use a service layer (BillingApiService) to centralize parsing and error contracts.

Pattern 2: Environment Isolation

Avoid environment if/else statements in Apex. Route environment differences through deployment metadata/config.

Pattern 3: Metadata-Driven Endpoint Selection

Store logical interface keys in Custom Metadata and resolve to Named Credential names at runtime only when necessary.

Use this for multi-tenant or multi-endpoint integration landscapes.

Security and Governance

Operational Checklist

Common Failure Modes