function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Admin User 4714Admin User 4714 

Consume REST API from Apex

Is it possible to consume a third-party API from Apex even if that third party is a REST API? What is the best approach?
Binh Tran ThanhBinh Tran Thanh
Please check this link from salesforce
https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_rest_callouts

My example:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://alokhoai.com/5-cach-lam-trang-da-tai-nha-an-toan-va-hieu-qua-co-the-ban-chua-biet/');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if(response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
    for(Object animal: animals) {
        System.debug(animal);
    }
}