Invoking APIs‎ > ‎

Use Node SDK

APICreatorSDK is a rapid development Node SDK for your API projects. This npm package provides a number of useful methods for working with the API projects you create using API Creator.

Verify the Prerequisite

Before working with Node SDK, be sure that you have installed Node SDK, including the SDK library.

For more information about how to install the Node SDK, how to try out the library, and how to connect to an existing API project, see the npm site.

npm install APICreatorSDK -g

Connect to a Sample API

Making your first request takes a few lines of code. You can connect to a sample API which is available as a sandbox for exploring the basics.

Prerequisite: You have installed the self-contained, single-user version of Live API Creator, which is demonstration package based on Jetty.

  1. From a command window, create a file (for example, test.js) in your current working directory.
  2. Paste in the following contents into the file and 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 above into a single line.

A list of customers is expected in the response.

API Methods

The following API methods are included:

connect

The connect method initializes the connection to an API project. It returns an instance of the library according to the project defined. Auth tokens and users are accessible from the Security section.

Syntax:

apicreator.connect(string projectUrl, string username, string password)

or

apicreator.connect(string projectUrl, string authToken)

Examples:

Connect with API key:

var e1 = apicreator.connect('http://localhost:8080/rest/default/demo/v1', 'demo_full');

Connect with user name/password:

var e2 = apicreator.connect('http://localhost:8080/rest/default/demo/v1', 'demo', 'Password1'); //

The following example accesses your API project's endpoints:

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 example is from the b2bscripts/scs/projects/test/node/verify.js file.

endpoint

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');

setPageSize

The setPageSize method is a convenience function for defining the default number of objects retrieved in a endpoint.get() request. You can override this filter by explicitly defining the page size in endpoint.get(parameters).

Syntax:

apicreator.setPageSize(integer number)

Example:

apicreator.setPageSize(5);

Endpoint Object Methods

The following endpoint object methods are included:

get

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); });

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...

Your filter can include boolean logic, like this:

'sysfilter=less(balance:1000)&sysfilter=less(credit_limit:5000))'

put

The put object method returns a promise for a PUT request. Parameters by request type are described in detail here.

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"
  },

  {...}]
}

post

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"
    }, 
    {...}]
}

del

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
    });
});

More Information

For more information about the parameters by request type, see URL Parameters.