Query Salesforce from Node.js



The CData API Server exposes Salesforce data (and 80+ other sources) as an OData endpoint, which can be queried from Node.js using simple HTTP requests. Create, read, update, and delete (CRUD) queries are supported. This article shows how to use the API Server to request JSON-formatted Salesforce data in Node.js.

If you have not already connected successfully in the administration console, see the "Getting Started" chapter in the help documentation for a guide.

The URL below will make an authenticated request for Account data formatted as JSON. The API Server uses authtokens to authenticate users authorized to access the OData endpoint. It is recommended to set the authtoken in the header when making the request, but for simplicity, the example below sets the authtoken in the URL.

var endpoint = "https://your-server:8032/api.rsc/Account?@authtoken=9q1P9d4z7A1l7w3Z2r7m";

After you have authorized your request, you can request data using the HTTP client in Node.js. JavaScript standards make it easy to define additional parameters. The example URL below applies a simple filter that searches for records with a value of "Floppy Disks" in the Industry column. After making the request, construct the body of the response and call the JSON.parse() function to parse it into records:

var http = require('http');
http.get(endpoint + "&$filter=" + encodeURIComponent("Industry eq 'Floppy Disks'"), 
function(res) {
  var body = '';
  res.on('data', function(chunk) {
    body += chunk;
  });
  res.on('end', function() {
    console.log(body);
    var jsonData = JSON.parse(body);
  });
}).on('error', function(e) {
  console.log("Error: ", e);
});