Skip to content

Calling Plumber APIs from Node.js#

Requirements#

To call APIs hosted in RStudio Connect from Node.js, you'll need:

  1. URL for the RStudio Connect Host
  2. Path for the API endpoint hosted on RStudio Connect
  3. API key (if your API requires authorization)

Node.js Example#

You can use the https package in Node.js to call Plumber APIs from Node.js applications:

var https = require("https");

var connectApiHost = "connect.yourcompany.com"
var connectApiPath = "/rest-api/route"
var connectApiKey = "YfB5XBRB7slkkBSEi5qr93mWJvbpXQQy"

var options = {
  host: connectApiHost,
  path: connectApiPath,
  headers: {"Authorization": "Key " + connectApiKey},
  port: 443,
  method: "GET",
};

https.request(options, function(res) {
  console.log("STATUS: " + res.statusCode);
  console.log("HEADERS: " + JSON.stringify(res.headers));
  res.setEncoding("utf8");
  res.on("data", function (chunk) {
    console.log("BODY: " + chunk);
  });
}).end();

You can replace the values of connectApiHost, connectApiPath, and connectApiKey with the URL of the RStudio Connect server, the path to the API on RStudio Connect, and the API key from RStudio Connect.

Scope#

The code examples assume that you are calling a published API in RStudio Connect that is:

  • Hosted on a secure server with TLS/SSL at an HTTPS endpoint
  • Using an API key to make an authorized call to an API
  • Making an HTTP GET request to the API

If your use case is different, then you can modify the example code accordingly.