Access REST API Using Node.js File

You can access the Field Service REST API using the Node.js file. In this example, the Demo.js file gets an activity and extracts the city and state values from the activity.

  1. Download and install the Node.js file on your local computer.
  2. Create the Demo.js file as follows:
    var https = require('https'); //Loads the built-in HTTP modules that provides the required methods.
    var allData = ' '; //Stores the response to the request when an activity is found.
    //Required options to be set for the request.
    var requestOpts = {
    'hostname' :  '<instance_name>', //Indicates the Field Service site.
    'path' : '/rest/ofscCore/v1/activities/3954794',
    //Points to the company's REST API interface where the path is accessing the activity with ID 3954794 from the Activities collection. 
    'method': 'GET', //Enables you to fetch an activity.
    'auth' : 'user@instanceId:passwd', //Indicates the Client ID, Instance Name, and Client Secret where user is the Client ID, instanceId is the Instance Name, and the passwd is the Client Secret.
    'headers' : { 'Content-Type': 'application/json' }, //designates the content to be in JSON format.
    'rejectUnauthorized': false, 
    //Set to False since the site that is being accessed does not have a secure certificate.
    function getInfo ( ){
    var request = https.request (requestOpts); //Creates and sends the request
    request.on ('response', function(response) 
    //Checks the data in the body of the response and stores the data in the allData variable.
    {response.on ('data', function(chunk) {
    allData += chunk;
    .on ('error', function (e) //Error handling code. 
    console.log ("Got error: " + e.message);
    .on ('close', function ( ) {
    console.log ("\nBody:  " + allData); //Lists the data received in the body of the response on the console.
    var info = JSON.parse (allData); //Converts data into a format that lets you obtain the required fields using the parse method of the JSON class. 
    console.log ("\nCity & State: " +  ", +info.stateProvince); //Lists the City and State labels and their corresponding values.
    request.end ( );
    getinfo ( ); //Calls the getInfo function.
  3. To run demo.js, type node demo.js in the command prompt and press Enter.
    The data from the activities object along with the City and State fields are displayed in the output.

    This tutorial shows how to access Oracle Field Service REST API with Node.js.