Use Node SDK

Use Node SDK
lac31
The 
APICreatorSDK
 Node SDK is a rapid development Node SDK for your APIs. You can use it to write Node.js programs and for simplified data access from Node.js programs. This npm package provides some useful methods for working with the APIs you create using API Creator.
In this article:
 
 
Verify the Prerequisite
Before using the 
APICreatorSDK
 Node SDK, ensure that you have installed the 
APICreatorSDK
 Node SDK.
For more information about how to install the 
APICreatorSDK
 Node SDK, see Install NodeJS.
Connect to a Sample API
The following example shows how to connect to the 
Demo
 API sample.
For more information about the 
Business Logic
 API sample, see Business Logic API Project Sample.
  1. From a command window, create a 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:
    • 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 apicreator Object Method
The 
connect
 method initializes the connection to an API. It returns an instance of the library according to the API 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 Business to Business Example.
The endpoint apicreator Object Method
The 
endpoint()
 method refers to the resource, table, view, or procedure 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 apicreator Object 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 following endpoint object methods are included:
The get endpoint Object Method
The 
get()
 object 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 endpoint Object Method
 The 
put()
 object 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 endpoint Object Method
The 
post()
 object 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 endpoint Object Method
The 
del()
 object 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
});
});