DEV Community

Cover image for Setting up Node.js Integration to Salesforce using OAuth
Tyler Steck
Tyler Steck

Posted on

Setting up Node.js Integration to Salesforce using OAuth

Salesforce is a popular customer relationship management (CRM) platform used by many businesses to manage their customer data. As a developer, you can use Salesforce's APIs to build integrations that enable your applications to interact with Salesforce data. In this article, we will walk through how to set up a Node.js integration to Salesforce using OAuth.

Overview

The OAuth flow allows you to authenticate a user's Salesforce account and obtain an access token that can be used to interact with Salesforce on their behalf. Here are the steps involved in setting up a Node.js integration to Salesforce using OAuth:

  1. Create a Salesforce Connected App
  2. Set up OAuth scopes
  3. Obtain a Salesforce access token
  4. Use the access token to interact with Salesforce's API

We'll go through each of these steps in detail below.

Step 1: Create a Salesforce Connected App

The first step is to create a Salesforce Connected App. To do this, follow these steps:

  1. Log in to your Salesforce account
  2. Click on the gear icon in the upper right corner and select "Setup"
  3. Under "Platform Tools", select "Apps" and click on "App Manager"
  4. Click on "New Connected App"
  5. Fill in the required information, including the app name, API name, and contact email
  6. Under "API (Enable OAuth Settings)", select "Enable OAuth Settings"
  7. Enter the Callback URL for your Node.js app
  8. Select the OAuth scopes you want to add to your app. For example, if you want your app to read and write Salesforce data, you'll need to select the "Access and manage your data (api)" scope.
  9. Click "Save"

Once you've created your Connected App, you'll be provided with a client ID and client secret that you can use to authenticate with Salesforce.

Step 2: Set up OAuth Scopes

OAuth scopes define the permissions that your app needs to interact with Salesforce's API. To set up OAuth scopes, follow these steps:

  1. Click on your Connected App in the App Manager
  2. Scroll down to the "API (Enable OAuth Settings)" section
  3. Under "Selected OAuth Scopes", click "Add"
  4. Select the scopes you want to add to your app. For example, if you want your app to read and write Salesforce data, you'll need to select the "Access and manage your data (api)" scope.
  5. Click "Add"

Step 3: Obtain a Salesforce Access Token

To obtain a Salesforce access token, you'll need to use Salesforce's OAuth 2.0 flow. Here are the steps:

Redirect the user to Salesforce's OAuth authorization page. The URL should look something like this: https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=<your_client_id>&redirect_uri=<your_redirect_uri>&scope=<your_scopes>
The user will be prompted to authorize your app to access their Salesforce account. Once they accept, Salesforce will redirect them back to your app with a temporary code.
Exchange the temporary code for an access token by making a POST request to https://login.salesforce.com/services/oauth2/token. You'll need to include your app's client ID, client secret, and the temporary code in the request body. For example:

const response = await fetch('https://login.salesforce.com/services/oauth2/token', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: `grant_type=authorization_code&client_id=<your_client_id>&client_secret=<your_client_secret>&code

Enter fullscreen mode Exit fullscreen mode

Top comments (0)