Node.js Overview

Examples within this document demonstrate how to access the Primavera Administration API using Node.js.

To access the API with Node.js:

  • Install Node.js, as described in Using Node.js.
  • Write Node.js scripts including one or more HTTP requests containing the object properties described in this topic.
  • Run Node.js scripts using the Node command line interface.

The following table summarizes the HTTP request properties used in the Node.js examples in this document:

HTTP Request Property and Value Description
hostname:"<hostName>"

Identifies the host name of the server on which Primavera Administration is hosted. For example:

hostname:"localhost"

port:<portNumber>

Indicates the port used to connect to the Primavera Administration server. For example:

port:80

path:'<endpoint>'

Specifies the path, or API endpoint the HTTP request is sent to. For example:

path:'/cloudapi/restapi/user'

headers:{

"<httpHeader>":"<value>"

}

Defines one or all of the following:

  • Content-Type: Media type of the body of the request
  • Accept: Media type of the body of the request
  • Version: API version to use to process the request

For example:

headers:{

"Content-Type":"application/json",

"Version":"2"

}

auth:"<username>:<password>"

Specifies the username and password for the Primavera Administration account. For example:

auth:"jsmith:mypassword1"

method:'<httpVerb>'

Indicates the type of request. For example:

method:'DELETE'

To use Node.js to interact with the API, enter the node command followed by one or more Node.js script filenames.

For example:

		node CAScript.js
		

Command Examples

The following examples demonstrate how to use Node.js to access the API.

To fetch a user object by its user ID, write a Node.js script like the following:

		var https = require('https'); 
      
		var options = { 
      
		    hostname : "localhost", 
      
		    port : 7001, 
      
		    method : 'GET', 
      
		    path : "/cloudapi/restapi/user/ABROWN", 
      
		    auth : "jsmith:mypassword1", 
      
		    headers : { 
      
				"Accept" : "application/json", 
      
				"Version" : "2" 
      
			} 
      
		} 
      
		var request = https.request(options, function(result){ 
      
		    result.setEncoding('utf8'); 
      
		    result.on('data', function(response){ 
      
		        console.log(response); 
      
		    }); 
      
		}); 
       
      
		request.on('error', function(err){ 
      
			console.log("Could not process request: ", err); 
      
		}); 
       
      
		request.on('end', function(){ 
      
			console.log("Finished processing response."); 
      
		}); 
       
      
		request.end();
		

API requests that use the POST method require additional data. Pass additional data to Node.js using the fs object. The following example sends a POST request to the /user endpoint using data stored in a JSON file named user_example.json. The user_example.json file contains the following:

{"modified": 
      [{ 
          "loginId": "JSMITH", 
          "organization": { 
              "associations": [], 
              "bidderCompany": false, 
              "isDeletable": false, 
              "inheritedPolicy": false, 
              "displayName": "sfo", 
              "id": "4", 
              "name": "sfo" 
          }, 
          "statusInfo": "", 
          "emailAddress": "jsmith@pgbu.com", 
          "roles": [ 
              { 
                  "displayName": "Primavera Unifier Production", 
                  "id": "77", 
                  "name": "PrimaveraUnifierProduction" 
              } 
          ], 
          "locked": false, 
          "createdBy": "COADMIN", 
          "status": "Active", 
          "userType": "EMP", 
          "firstName": "Jim", 
          "lastName": "Smith", 
          "createdDate": "2018-01-17T15:41:26", 
          "updatedDate": "2018-02-15T16:16:08", 
          "pwdExpireDate": "2018-11-13T15:46:30", 
              "pwdWarnDate": "2018-06-16T15:46:30", 
          "provisionedDate": "2018-01-17T15:46:30", 
          "pwdExpired": false, 
          "pwdCantChange": false, 
          "pwdWarned": false, 
          "loginAttemptsCtr": "0", 
          "changePwdAtNextLogin": true, 
          "lastAction": "Updated", 
          "disabled": false, 
          "id": "205" 
      }]}
		

To create a user using this JSON data, use a Node.js script like the following:

var fs = require('fs'); 
      var postData = fs.readFileSync('user_example.json', 'utf8'); 
      var options = { 
          hostname : "localhost", 
          port : 80, 
          method : 'POST', 
          path : "/cloudapi/restapi/user/add-list", 
          auth : "jsmith:mypassword1", 
          headers:{ 
              "Content-Type" : "application/json", 
         "Version" : "2", 
              "Accept" : "application/json", 
         "Version" : "2" 
          } 
      } 
      var request = https.request(options, function(result){ 
          result.setEncoding('utf8'); 
          result.on('data', function(response){ 
              console.log(response); 
          }); 
      }); 
       
      request.on('error', function(err){ 
       console.log("Could not process request: ", err); 
      }); 
       
      request.on('end', function(){ 
       console.log("Finished processing response."); 
      }); 
       
      request.write(postData); 
      request.end();
		
		Note: If the user specified with the 
		auth property does not have permission to modify the specified user, or if the user does not exist, the user creation will fail, and the API will return a response of 401, 404, or 500.