Friday, 20 March 2020

Salesforce API Rest Endpoints

Salesforce has exposed a soap library as well as Rest Endpoints. In the following doc, we will talk about Rest endpoints.
I hope all of you know what is Rest and why it is favored over soap lots of times.
To know more about it, check out the following link.  https://restfulapi.net/
Read Soap vs Rest if you have time as well. https://restfulapi.net/soap-vs-rest-apis/
Following is the standard salesforce Rest API documentaion - https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_prereq.htm


Firstly you need a tool through which you can hit the API. You can hit GET API using a browser and POST using a terminal using a curl command.  I have used the Postman app. It is wildly popular for API testing. It makes the job easier. You can find it here - https://www.postman.com/downloads/

So, to start with salesforce API, you will first need to know what are the endpoints to hit. If you have custom code in your org and you have exposed some new rest endpoints then you can hit them up as well. The important condition being you know what path to hit.

At first, we will talk about the default REST endpoints. Salesforce has detailed documentation on which API to hit but it could take time to figure it out. So, I am here to help you with it.

So, Salesforce has a tool called Workbench where you can execute REST queries and get to know its path. That's how I got this as well. I have found an excellent Salesforce doc for this. https://trailhead.salesforce.com/en/content/learn/modules/api_basics/api_basics_rest
Play around with it figure it out.

Once you are done with it, if you want to still make API calls using POSTMAN, you need to Authenticate yourself first.
For that, you need to create a connected app in your salesforce org with API access enabled. It is a POST call. here is a great link which shows how to do it in detail -(https://docs.datawatch.com/swarm/desktop/Generating_a_Client_ID_and_ClientSecret_Key_for_Salesforce_Connections.htm)

Following is the standard document from salesforce to gain more knowledge about authentication flows. Use password appended with the security token.
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_oauth_and_connected_apps.htm

This returns a token, which you can use for making other calls.
For example, 

This is just for a token, you still need to input parameters that you are using for the API under the params tab for the GET call. Like,

However, for POST call you need to provide data that you want to send over under the tab named 'Body' in raw format. and give the content type under the 'Headers' tab as 'JSON'.
At least that's what I did.
I am trying to create the Community user object from the managed package I have installed using POST API.



Add token, add headers, add body and you are good to go. Hit the API and see the response. 
The response depends on what is the call made. If it was for creating an SObject like community user, it will return the id of the object created.
If you hit a GET call asking for a bunch of contacts, then the response will contain that. You can see the format of response while playing around with Workbench anyways. (https://workbench.developerforce.com/login.php)
Use it effectively. It is a great tool.

You can as well create your own rest endpoints in salesforce.
You will see how it hit such customized endpoints as well. 
Although to know correct path for hitting such endpoints declared inside installed managed package, follow the link - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm

Para from the above link - 
Apex REST methods can be used in managed and unmanaged packages. When calling Apex REST methods that are contained in a managed package, you need to include the managed package namespace in the REST call URL. For example, if the class is contained in a managed package namespace called packageNamespace and the Apex REST methods use a URL mapping of /MyMethod/*, the URL used via REST to call these methods would be of the form https://instance.salesforce.com/services/apexrest/packageNamespace/MyMethod/. For more information about managed packages, see What is a Package?



So, I think this is it, guys. You can do REST API salesforce testing on your own then. Take care. Stay Hydrated. 
PS - I always forget to drink water.




No comments:

Post a Comment