+ Start a Discussion
TRIPTI JAIN 2TRIPTI JAIN 2 

how to call salesforce lightning APIs from client side script?

Can someone please share an example or a  way to call a lightning REST API from a client side script?
Thanks in advance for the help.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Tripti,

Greetings to you!

According to Salesforce document: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_api_calls_platform.htm

By default, you can’t make calls to third-party APIs from client-side code. Add a remote site as a CSP Trusted Site to allow client-side component code to load assets from and make API requests to that site’s domain.

The Lightning Component framework uses Content Security Policy (CSP), which is a W3C standard, to control the source of content that can be loaded on a page. Lightning apps are served from a different domain than Salesforce APIs, and the default CSP policy doesn’t allow API calls from JavaScript code. You change the policy, and the content of the CSP header, by adding CSP Trusted Sites.

You can’t load JavaScript resources from a third-party site, even a CSP Trusted Site. To use a JavaScript library from a third-party site, add it to a static resource, and then add the static resource to your component. After the library is loaded from the static resource, you can use it as normal.

Sometimes, you have to make API calls from server-side controllers rather than client-side code. In particular, you can’t make calls to Salesforce APIs from client-side Lightning component code.

For information about making API calls from server-side controllers, see Making API Calls from Apex (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_api_calls_platform.htm).

Please refer to below links:
https://douglascayers.com/2018/09/10/calling-rest-api-from-lightning-components-without-named-credentials/

http://amitsalesforce.blogspot.com/2017/04/http-callout-from-java-script-rest.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas​​​​​​​
TRIPTI JAIN 2TRIPTI JAIN 2
Hi Anas,
Thank you for your reply. I am completely new to this CRM. I just want to do CURD from my client side script. Can you please explain how can i do this. I already tryed to search a contact using lightning session Id (got from cookies). Already whiteListed my ligtning domin as per your document. Already created a connected app and authorized the user to named credentials. Still getting the cross origin exception.