Docs‎ > ‎Live API Creator Overview‎ > ‎

Node SDK

Install Node SDK

Install as described here.

Download the Live API Creator npm Package

The Live API Creator package provides a number of useful methods for working with API's generated within API Creator.

Download the Live API Creator package or use the npm install command. For more information, see the npm site.

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. Run it as follows (this presumes the jetty install):
  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('caliveapicreator');

    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 3 lines above into a single line.
A list of customers is expected in the response.

API Methods

connect

Initializes the connection to a project API. 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'); //

endpoint

The endpoint fragment 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

A convenience function for defining the default number of objects retrieved in a endpoint.get() request. This filter can be overridden by explicitly defining the page size in endpoint.get(parameters).

Syntax:

apicreator.setPageSize(integer number)

Example:

apicreator.setPageSize(5);

Endpoint Object Methods

get

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

Typical output from this last bit of code:

> [ { '@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))'

Parameters by request type are described in detail here.

put

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

Example Response:

{
  "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

Returns a promise for a POST request. Parameters by request type are described in detail here.

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

Example Resonse:

{
  "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

Returns a promise for a DELETE request. Parameters by request type are described in detail here.

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