Last active
June 27, 2024 20:54
-
-
Save steffensbola/0749795979ecee960df7e8158d8f4586 to your computer and use it in GitHub Desktop.
Simple JS client to connect to the Salesforce API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios'; | |
| import dotenv from 'dotenv'; | |
| import assert from 'assert'; | |
| dotenv.config(); | |
| assert(process.env.SF_CONSUMER_KEY, 'SF_CONSUMER_KEY not set'); | |
| assert(process.env.SF_CONNECTED_APP_API_NAME, 'SF_CONNECTED_APP_API_NAME not set'); | |
| assert(process.env.SF_CONSUMER_SECRET, 'SF_CONSUMER_SECRET not set'); | |
| assert(process.env.SF_USERNAME, 'SF_USERNAME not set'); | |
| assert(process.env.SF_PASSWORD, 'SF_PASSWORD not set'); | |
| assert(process.env.SF_SECURITY_TOKEN, 'SF_SECURITY_TOKEN not set'); | |
| const sfUrl = 'https://test.salesforce.com'; | |
| const clientId = process.env.SF_CONSUMER_KEY; | |
| const connectedAppApiName = process.env.SF_CONNECTED_APP_API_NAME; | |
| const clientSecret = process.env.SF_CONSUMER_SECRET; | |
| const username = process.env.SF_USERNAME; | |
| const password = process.env.SF_PASSWORD; | |
| const securityToken = process.env.SF_SECURITY_TOKEN; | |
| const combinedPasswordToken = `${password}${securityToken}`; | |
| // Get access token | |
| const getAccessToken = async (clientId, clientSecret, username, combinedPasswordToken) => { | |
| const authHeaders = { | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| }; | |
| const urlencoded = new URLSearchParams(); | |
| urlencoded.append("grant_type", "password"); | |
| urlencoded.append("client_id", clientId); | |
| urlencoded.append("client_secret", clientSecret); | |
| urlencoded.append("username", username); | |
| urlencoded.append("password", combinedPasswordToken); | |
| try { | |
| const response = await axios.post(`${sfUrl}/services/oauth2/token`, urlencoded, { headers: authHeaders }); | |
| console.log(response.data); | |
| return { | |
| // have to scape especial charactersc https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_curl.htm | |
| accessToken: response.data.access_token.replace('!', '\!'), | |
| instanceUrl: response.data.instance_url | |
| }; | |
| } catch (error) { | |
| console.error(error); | |
| } | |
| }; | |
| async function fetchUserData(accessToken, instanceUrl, query = 'SELECT+id,name+FROM+user+LIMIT+5', apiVersion = 'v61.0') { | |
| const requestHeaders = { | |
| 'Authorization': `Bearer ${accessToken}` | |
| }; | |
| const requestOptions = { | |
| method: "GET", | |
| headers: requestHeaders, | |
| redirect: "follow" | |
| }; | |
| try { | |
| const response = await axios.get(`${instanceUrl}/services/data/${apiVersion}/query?q=${query}`, requestOptions); | |
| console.log(response.data); | |
| } catch (error) { | |
| console.error(error); | |
| } | |
| } | |
| // Get access token and instance URL | |
| const accessToken = await getAccessToken(clientId, clientSecret, username, combinedPasswordToken); | |
| const instanceUrl = accessToken.instanceUrl; | |
| const token = accessToken.accessToken; | |
| // fetch user data | |
| fetchUserData(token, instanceUrl, 'SELECT+id,name+FROM+user+LIMIT+5', 'v61.0'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment