REST API calls from Salesforce Lightning Web Components (LWC)| Medium

Introduction -

Generally, the Salesforce developers encounter a problem of connecting multiple Salesforce organizations to work on their data in a combined way. So in this blog, we will see how we can get data from one Salesforce organization to another.

Let’s assume a scenario where we have multiple Salesforce Organizations and wish to connect them & transfer data from one to another to work on it. In this condition what will be the process to accomplish this task?

So, the solution is to use Salesforce APIs because we cannot run queries to achieve the result.

Here I’ll explain how we can solve this problem using REST API. Along with how we can use it in LWC.

First of all, what is the REST API?

REST API — Salesforce provides a number of APIs to interact with its system and it is one of them.


REST API provides the easiest way to connect with third-party applications/services, as we cannot run queries between one organization to another organization.

For making a REST API call from LWC to Salesforce, you cannot directly use the standard process i.e. the following -

Http ht = new Http ();HttpRequest req = new HttpRequest ();HttpResponse resp = new HttpResponse ();req.setMethod('GET');req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());req.setEndpoint('callout:<Named Credential name>/services/data/v48.0');resp = ht.send(req);

It will give you an error like — INVALID_HEADER OR INVALID_SESSION_ID.

To work with REST API in LWC you need to create three things -

  1. Connected Apps
  2. Auth Provider
  3. Named Credentials

Connected App -

The connected app is a framework that enables an external application to integrate with Salesforce using APIs and standard protocols, such as SAML, OAuth, and OpenID Connect. Connected apps use these protocols to authenticate, authorize, and provide single sign-on (SSO) for external apps. The external apps that are integrated with Salesforce can run on the customer success platform, other platforms, devices, or SaaS subscriptions.

In other words, the connected app provides access to data stored in Salesforce using authentication

Example — when you want to get data from any other org of Salesforce then you will need a connected app.

Create the connected app like this -

Callback URL — temp:value (as it needs to be changed later)

OAuth Scopes — API, full, (refresh_token,offline_access)

Auth. Provider -

An authentication provider lets your users log in to your Salesforce org using their login credentials from an external identity provider, such as Facebook, Google, LinkedIn, and Twitter. Salesforce provides default authentication providers where Salesforce manages the required configuration values.

So, in our case, we are going to use Salesforce as an external provider and connected app for data.

Create an Auth Provider -

Select Provider Type as Salesforce.

Default Scopes — refresh_token api.

Consumer key and Consumer secret — consumer key and consumer secret obtained from the connected app.

Now set the callback URL obtained from here to the connected app.

Named Credentials -

A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. To simplify the setup of authenticated callouts, specify a named credential as the callout endpoint. If you instead specify a URL as the callout endpoint, you must register that URL in your org’s remote site settings and handle the authentication by yourself.

So, we can make the callout to the external system without even using the username and password.

It makes it easy to maintain the credentials.

Create a Named Credentials -

URL — Paste the base URL of your org if you are in Classic copy it from the browser’s URL box and if you are in lightning mode open the developer console and copy the URL from there.

Authentication Protocol — OAuth 2.0

Authentication Provider — Select the auth provider created previously.

Scope — refresh_token api

Start Authentication Flow on Save — Checked.

Click save, login to your org, and allow Auth Client.

You are done setting up Named Credentials and now make the following changes to your code -

HttpRequest req = new HttpRequest();        req.setEndpoint('callout:BigObjectNC/services/data/v48.0/query/?q=SELECT+Id,Name__c,Mobile__c+FROM+Reporting__c+LIMIT+10');req.setMethod('GET');string autho = 'Bearer {!$BigObjectNC.OAuthToken}';req.setHeader('Authorization', autho);//Get ResponseHttp http = new Http();HTTPresponse res= http.send(req);string response = res.getBody();

Conclusion -

❖ Developers sometimes find it difficult to create a REST API call from an LWC and get confused between components that are used to call the REST API.

❖ Now, we have seen how we can call a REST API from an LWC and also why and which components of Salesforce are required to do so.

❖ We have achieved the solution of organization connectivity by following the above mentioned three steps.

❖ Things to keep in mind -

➢ Use + for space in the endpoint.

➢ See the space between Bearer and Token.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mukul Mahawariya

4x Salesforce Certified | Trailhead Ranger | Salesforce Enthusiast