Use Node SDK

Use Node SDK
lac42
You can write Node.js programs and access data in your APIs from these programs using the 
APICreatorSDK
 Node SDK. Work with the APIs that you create using API Creator using the methods that this Node SDK includes.
In this article:
 
 
Install the APICreatorSDK Node SDK
For more information about how to install the 
APICreatorSDK
 Node SDK, see the APICreatorSDK site on GitHub.
Connect to an API Sample
The following procedure shows how to connect to the 
Demo
 API sample.
For more information about the 
Demo
 API sample, see Demo API Sample.
Follow these steps:
 
  1. From Terminal or a command prompt, create a JavaScript file (for example, 
    test.js
    ) in your current working directory.
  2. Paste the following contents into the file, and then save your changes:
    var apicreator = require('APICreatorSDK');
    var api = apicreator.connect('http://localhost:8080/rest/default/demo/v1', 'demo_full');
    var customers = api.endpoint('customer');
    customers.get().then(function (data) {
    console.log(data); //an array of objects from the customers table
    });
  3. Complete 
    one
     of the following:
    • From terminal or a command prompt, run the node commands from the file by issuing the following command:
      node test.js
    • Enter the commands directly into a Node command line. Remove the comment from the contents of the 
      test.js
       file and combine the last three lines from step 1 into a single line.
A list of customers is expected in the response.
apicreator Object Methods
The 
apicreator
 object includes the following methods:
The connect Method
The 
connect()
 method initializes the connection to an API. It returns an instance of the library according to the API that is defined.
Syntax:
 
apicreator.
connect
(string projectUrl, string username, string password)
or
apicreator.
connect
(string projectUrl, string authToken)
Examples:
 
Access your API using the API key:
var e1 = apicreator.
connect
('http://localhost:8080/rest/default/demo/v1', 'demo_full');
Access your API using the user name and password:
var e2 = apicreator.
connect
('http://localhost:8080/rest/default/demo/v1', 'demo', 'Password1'); //
The following code snippet show an example of how you can connect to your API endpoints using the API key (by way of the 
connect()
 method:
var api = apicreator.
connect
('http://localhost:8080/rest/default/b2bderbynw/v1', 'Bzn8jVyfOTiIpW6UQCgy');
var ordersEP = api.endpoint('nw:Orders');
ordersEP.get('sysfilter=equal(OrderID:2000)').then(function (data) {
var theOrder = data[0];
if (theOrder.AmountTotal == 301.20 && theOrder.discountedAmount == 298.188)
{
console.log("....Success...");
} else {
console.log("** Expected values not found, theOrder...");
}
});
This code snippet is drawn from the 
b2bscripts/scs/projects/test/node/
 
verify.js
 file that is part of the B2B sample.
For more information about the B2B sample, see B2B API Sample.
The endpoint Method
The 
endpoint()
 method refers to the resource, table, view, or procedure that is defined in API Creator. This method returns an endpoint object, which is used to make RESTful requests.
Syntax:
 
apicreator.
endpoint
(
string
endpointFragment)
Example:
 
var customers = apicreator.endpoint('customer');
The setPageSize Method
The 
setPageSize()
 method is a convenience function for defining the default number of objects that are retrieved in an 
endpoint.get()
 request. You can override this filter by explicitly defining the page size in an 
endpoint.get([string urlParams])
 request.
Syntax:
 
apicreator.
setPageSize
(
integer
number)
Example:
 
apicreator.setPageSize(5);
endpoint Object Methods
The 
endpoint
 object includes the following methods:
The get Method
The 
get()
 method returns a promise for a GET request.
Syntax:
 
endpoint.
get
([string urlParams])
Example:
 
var custPromise = customers.get('sysfilter=less(balance:1000)'); custPromise.then(function (c) { console.log(c); });
Your filter can include boolean logic, for example:
'sysfilter=less(balance:1000)&sysfilter=less(credit_limit:5000))'
The following output is expected:
> [ { '@metadata':
{ href: 'https://localhost:8080/APIServer/rest/default/demo/v1/customer/Alpha%20and%20Sons',
checksum: 'A:e86aea2e0a4e74bf',
links: [Object] },
name: 'Alpha and Sons',
balance: 4484,
credit_limit: 9000 },
{ '@metadata':
{ href: 'https://localhost:8080/APIServer/rest/default/demo/v1/customer/Argonauts',
checksum: 'A:f69d919ec09d6d7c',
links: [Object] },
name: 'Argonauts',
balance: 1858,
credit_limit: 2000 },
etc...
The put Method
The 
put()
 method returns a promise for a PUT request.
For more information about the parameters by request type, see URL Parameters.
Syntax:
 
endpoint.
put
(
object
data, [
object
params])
Example:
 
var custGetPromise = customers.get();
custGetPromise.then(function (customers) {
//customers is a paginated list of records and the metadata for each
customers[0].name = "New Name";
//a customers[0]["@metadata"].href attribute describes the endpoint for making PUT requests specifically for customers[0]
var updatedCustomerEndpoint = e1.endpoint(customers[0]["@metadata"].href);
var custPutPromise = updatedCustomerEndpoint.put(customers[0], {rulessummary: true});
custPutPromise.then(function (txSummary) {
console.log(txSummary); //an object which will include a transaction summary and a summary of the rules fired during this request
});
});
The following response is expected:
{
"statusCode": 201,
"txsummary": [{
"@metadata": { ... },
"name": "New Name",
"balance": 0,
"credit_limit": 1234
}],
"rulessummary": [{
"type": "LOGIC_RUNNER",
"entity": "demo:customer",
"pk": "demo:customer[{name=New Customer 1}]",
"subtype": "BEGINUPDATE"
},
{...}]
}
 
The post Method
The 
post()
 method returns a promise for a POST request.
Syntax:
 
endpoint.
post
(
object
data, [
object
params])
Example:
 
var custPromise = customers.post({ "name": "New Customer 1", "credit_limit": 1234 }, {rulessummary: true}); custPromise.then(function (txSummary) { console.log(txSummary); //an object which will include a transaction summary and a summary of the rules fired during this request });
The following response is expected:
{
"statusCode": 200,
"txsummary": [{
"@metadata": { ... },
"name": "New Customer 1",
"balance": 0,
"credit_limit": 1234
}],
"rulessummary": [{
"type": "LOGIC_RUNNER",
"entity": "demo:customer",
"pk": "demo:customer[{name=New Customer 1}]",
"subtype": "BEGINUPDATE"
},
{...}]
}
The del Method
The 
del()
 method returns a promise for a DELETE request.
Syntax:
 
endpoint.
del
(
object
data, [
object
params])
Example:
 
var custGetPromise = customers.get();
custGetPromise.then(function (customers) {
//customers is an array of customer records
//the customers[0]["@metadata"].href attribute describes the endpoint for making DELETE requests specifically for customers[0]
var customerZero = e1.endpoint(customers[0]["@metadata"].href);
var custDeletePromise = customerZero.put(customers[0], {rulessummary: true});
custDeletePromise.then(function (txSummary) {
console.log(txSummary); //an object which will include a transaction summary and a summary of the rules fired during this request
});
});
 B2B DevOps Node SDK Sample
The Business to Business (B2B) DevOps sample scripts include an example of using the 
APICreatorSDK
 Node SDK.
The sample DevOps scripts are available in the 
<liveapicreator-devops>/sample-scripts/b2b-devops/deployProdTests/node
 directory.