+ Start a Discussion

How create a Rest Client in SF that make a Get from another system?

I'm trying consume a Rest API of another system using apex, but I don't know how do that.
I'm reading and following the steps of the Trailhead (REST and SOAP Integration) but this is not clearly for me.

So my ideia is:
Consume Rest API from another system and make a GET request (Thats include the authentication of the another system).
I don't know which exactly code the apex accept, so I'm totaly lost.
if someone can help me, telling me the steps I need to do and some GET code for example that include authentication, will help me a lot.

Someone can help me with this issue?

Best Regards
Saurabh BSaurabh B
Hi Desenv,

The answer depends upon type of API you are trying to reach. For example,
  • Some API offer one way authentication - which means after you send userid and password/token, you will directly get response (data) from the server
  • Some offer two way authentication, which means after you send userid and password/token, their API server will return another security token/or Session ID. You use that security token or security token/or Session ID to make 2nd callout and that will give you the actual data.

You should understand the authentication process of the API you are trying to communicate. I would suggest, try using tools like POSTMAN or cURL (Postman is lot easier). It will allow you the test the API that you are trying to use.

See these good articles which will help you with the sample code,


Also, when you say "I'm trying consume a Rest API of another system using apex," , are you trying to establish connection between one Salesforce org to another Salesforce. If yes, try using Salesforce to Salesforce funtionality. Its easier way to establish connnection between two salesforce orgs. If you want to do that via Rest API, see below link -


Hope this helps! :)
Hi Saurabh,
Thanks for your help.
 - The other System that I'm saying is ServiceNow, so the integration will be of the Salesforce and ServiceNow.
 - I already consume the Rest API of ServiceNow in Postman and Java, and I did a GET and POST for test.
 - But I need do the same thing in APEX. I don't know if all the code I did in Java works in APEX.
 - I don't know if exist some doc that give me all the code that the APEX accept.(Some type of library of APEX).
 - The authentication accept a type "BASIC AUTHeNTICATION".
I write this to be more especific of what I want, but I thing these two links that you sent to me will help me, if not I came back :)

Thanks again Saurabh

Saurabh BSaurabh B
Hi Desenv,
I`m glad it helped.
  • If you are able to consume the API using cURL or Postman, thats a good news. You can now replicate similar methods in Salesforce.
  • Though Apex and Java have similarities, Java code will not work in Apex.
  • If its a Basic Authentication, you can try using a code like below,
public class WebServiceCallout {
    public static void sendCalloutREST(String jsonString){
            String endPointURL = 'https://samplesite.com/samplerest';
            String userName = 'samuser';
            String password = 'Sam@789';
            // Specify the required user name and password to access the endpoint
            // As well as the header and header information
            Blob headerValue = Blob.valueOf(userName + ':' + password);
            String authorizationHeader = 'BASIC ' +
            Httprequest request = new HttpRequest();
            Http http = new Http();
            request.setHeader('Content-Type', 'application/json');
            // Header info with remote server user name and password
            request.setHeader('Authorization', authorizationHeader);
            //Check the client certificate
            // timeout in milliseconds       
            //Making call to external REST API
            HttpResponse response = http.send(request);  
            System.debug('responseBody: '+response.getBody());
        }catch(Exception e){

Also, keep in mind you below things,
1) You will need to parse your JSON response in Apex so that you can use it. You can use this tool which will generate an Apex class for you to parse your response. All you have to do is pass your JSON query and click on generate.
2) You will need to write test class for your Apex callouts. Use Apex HttpCalloutMock Interface to replicate behaviour of your Webservice API.

Hope this helps.

Please mark this as Best Answer if it helps you!
Hi Saurab,
Can you post an example of GET?
The POST I did and works.

Ankit Arora 29Ankit Arora 29

Hi All,

I need some help. I have received APIs from a company that has API Username and Password and other details in JSON format. I have to POST some data on their server from Salesforce and then, later on, GET the data into the Salesforce. I am not sure how to do it. Can you please help me?

I tried the above code but I am still confused.
Thank you