Connecting Node.js and Salesforce
We’ve been using Node.js at Red Argyle for about a year now, and being a Salesforce partner we are always looking for new tools to help with our Salesforce integration projects. I found a handful of libraries that can connect Node.js to the Salesforce API, but I took a particular liking to JSforce. JSforce seems to work with nearly all of the Salesforce APIs, including the REST API, Chatter API, Bulk API, and Tooling API. The user documentation is excellent and allowed me to get up and running in no time. The feature that really peaked my interest is that JSforce can be used both in Node.js and in the web browser, which coincides perfectly with the advantages of Node.js itself.
An especially exciting feature of JSforce is that it allows for an application to connect to a Salesforce Org via OAuth. OAuth is a secure method for consumers to authorize an application to access their account. In this case, a consumer could give authorization for a Node.js web application to access and interact with their Salesforce Org. This model is safe for the consumer because they can easily revoke the application’s access to their account at any time through Salesforce (My Settings -> Personal -> Connections).
Get Started with Node.js and JSforce
Below is some boilerplate code I’ve put together to get you up and running quickly with Node.js and JSforce. Note that the code uses Express.js which is a web application framework for Node.js. Express.js is not required for Node.js + JSforce functionality, but it offers a great set of features for quickly building a Node.js application. There are tons of tutorials out there for getting up and running with Express.js, so I won’t go into that here.
First, define your JSforce OAuth object. The clientId and clientSecret are for your App, which must be registered in Salesforce (Setup -> Create -> Apps -> New Connected App). I suggest not hardcoding these, but instead saving them as environment variables. If you’re deploying to Heroku, you can store them at ‘process.env.SF_CLIENT_ID’ for example.
[gist id=”632d5c3cacd07f054968″ file=”new_jsforce_oauth”]
Next you create your OAuth routes. The first one will simply redirect the user to Salesforce to login and accept authorization. The second is the callback route which Salesforce will redirect the user to after authorization (this must be the same as the ‘redirectUri’ field above as well as the ‘Callback URL’ field you set in Salesforce when registering your app). Upon authorization, the user’s OAuth access token and instance URL will be saved to their session at ‘req.session.accessToken’ and ‘req.session.instanceUrl’. The user will then be redirected.
[gist id=”4b0fe6f0e7b12123dcba” file=”sf_oauth_routes”]
Authorization was successful and the app can now access the user’s Org. Below is a quick example showing the use of the access token received above to query the user’s Salesforce Org through JSforce. The resulting records are passed to a template engine for rendering.
[gist id=”d0ea66829f80c2d63d2d” file=”sf_accounts_query”]
Do you use Node.js? What are your tips for other Node.js users? If you have comments, suggestions, or questions be sure to leave them below.