Setting up a GET callout takes three Apex statements once the Named Credential is in place. The rest is parsing the response.
- Create a Named Credential
Setup, Named Credentials, New. Enter the URL, choose the authentication protocol, and save. The Named Credential gets a unique name (e.g., StripeApi).
- Build the HttpRequest in Apex
HttpRequest req = new HttpRequest(); req.setEndpoint('callout:StripeApi/customers/cus_abc'); req.setMethod('GET'); The callout: protocol references the Named Credential and injects authentication automatically.
- Send and capture the response
Http http = new Http(); HttpResponse res = http.send(req); String body = res.getBody(); Integer status = res.getStatusCode();
- Parse the JSON
Use JSON.deserialize or JSON.deserializeUntyped to convert the body into an Apex object or a Map. Handle missing fields and null values explicitly.
- Handle errors and retry transient failures
Check status code; 2xx is success, 4xx is a client error (do not retry), 5xx is a server error (retry with backoff). Log failures to a custom Object or a logging framework.
The URL or Named Credential reference the request targets.
GET; setMethod is optional but should be explicit.
Handled by Named Credential or set explicitly with setHeader('Authorization', 'Bearer ...').
Defaults to 10 seconds; max 120. Set via setTimeout(120000) for slow endpoints.
- Apex callouts cannot be made after a DML statement in the same transaction. Make the GET first or move the DML to an async context.
- Hard-coding endpoint URLs in Apex breaks across sandbox, production, and packaged deployments. Use Named Credentials instead.
- The 6-MB response limit is hard. APIs returning large payloads must paginate; the Apex code must follow the next-page links.
- Query-string parameters with special characters need EncodingUtil.urlEncode. Skipping the encode leads to silent integration failures when users have apostrophes or spaces in input.