Docs‎ > ‎Live API‎ > ‎

Node SDK

Our npm package provides a number of useful methods for working with API's generated within Espresso's Logic Designer. Getting started is as easy as running this from the command line:

npm install espressologic

And making your first request will only take a few lines of code. Here we are connecting to a sample API which is available as a sandbox for exploring the basics.

var espressologic = require('espressologic');

var api = espressologic.connect('https://eval.espressologic.com/rest/livedemo/demo/v1', 'readonly');

var customers = api.endpoint('customer');
customers.get().then(function (data) {
    console.log(data); //an array of objects from the customers table
});

Note: the code above works as is -- if you've installed the SDK (using npm), you should be able to copy and paste the code directly into a Node command line, and see a list of customers. This is using a read-only API key. If you'd like to go beyond simple reads, register for an evaluation on Espresso's home page (Try it Free button).

API Methods

1 - connect

Initializes the connection to a project API. It returns an instance of the library according to the project defined. API keys and users are accessible from the Logic Designer -> Security section.

Syntax:

espressologic.connect(string projectUrl, string username, string password)
or
espressologic.connect(string projectUrl, string apiKey)

Examples:

// Connect with API key
var e1 = espressologic.connect('https://eval.espressologic.com/rest/livedemo/demo/v1', 'readonly');
// Connect with user name/password
var e2 = espressologic.connect('https://eval.espressologic.com/rest/livedemo/demo/v1', 'demo', 'Password1'); //


2 - endpoint

The endpoint fragment refers to the resource, table, view, or procedure defined in Logic Designer. This method returns an Endpoint Object, which is used to make RESTful requests.

Syntax:

espressologic.endpoint(string endpointFragment)

Example:

var customers = e1.endpoint('customer');

3 - 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:

espressologic.setPageSize(integer number)

Example:

e1.setPageSize(5);


Endpoint Object Methods

1 - get

Returns a promise for a GET request. 

Syntax:

endpoint.get([object params])

Example:

var custPromise = customers.get({filter: 'balance < 1000', order: 'name'});
custPromise.then(function (c) { console.log(c); });

Typical output from this last bit of code:

> [ { '@metadata':
  { href: 'https://eval.espressologic.com/rest/livedemo/demo/v1/customer/Alpha%20and%20Sons',
    checksum: 'A:e86aea2e0a4e74bf',
    links: [Object] },
  name: 'Alpha and Sons',
  balance: 4484,
  credit_limit: 9000 },
{ '@metadata':
  { href: 'https://eval.espressologic.com/rest/livedemo/demo/v1/customer/Argonauts',
    checksum: 'A:f69d919ec09d6d7c',
    links: [Object] },
  name: 'Argonauts',
  balance: 1858,
  credit_limit: 2000 },
etc...

Parameters by request type are described in detail here.

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

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

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