Skip to content

NodeJS SDK CloudSignal documentation

Vasyl Kushniruk edited this page Mar 11, 2020 · 2 revisions

Getting started

What is Cloudprinter CloudSignal NodeJS SDK?

The Cloudprinter.com CloudSignal NodeJS SDK is a package that helps your app respond to events from Cloudprinter.com

We at Cloudprinter.com have connected 150+ printers to print & ship print products in almost any country in the world. Whether this is around the corner or on the other side of the globe, we've got you covered: we can deliver 500+ different products in more than 100 countries currently.

Our platform makes use of smart routing algorithms to route any print job to the most local and qualified printer. Based on location, performance, price and production options, your print job is routed by these algorithms to the nearest printing facility near your delivery address to help you save on transit times and costs.

Visit our website for more information on all the products and services that we offer.

Prerequisites

  • npm (for installation)
  • node 6.0 or above
  • Cloudprinter.com Print API account

How to install?

The CloudSignal SDK package can be installed with npm. Run this command:

npm i @cloudprinter/cloudsignal --save

Or, if you prefer yarn:

yarn add @cloudprinter/cloudsignal

Quick start

Once you have completed the installation, you could write an app that uses the SDK, you will create your first NodeJs script that will create a CloudSignal event handler. Look at CloudSignal Api Key to learn how to get an API key.

const CloudSignal = require('@cloudprinter/cloudsignal');

// The port the server is going to listen on. Defaults to 8100.
const port = 8100;

const cloudSignalApiKey = '*';
const eventHandler = new CloudSignal.EventHandler(cloudSignalApiKey, port);

After that you can handle signals:

eventHandler.on('ItemCanceled', (signalData) => {
    // handle
});

General

Your RESTful API endpoint

Each client can have one RESTful API endpoint for use with CloudSignal Webhooks. You need a public URL where the app can begin receiving events. To register this URL need to the Cloudprinter.com Dashboard.

CloudSignal API Key

Each account is associated with an API key, which is used as authentication in all API calls. Each webhook request includes a Webhook API key, which is different from the account API key. This Webhook API key should be validated on each request. It can be found in the Cloudprinter.com Dashboard.

Error Handling

In case an error is thrown inside a listener, it must be handled, to prevent your application from terminating unexpectedly. This event handler allows you to define an error handler for catching errors thrown inside any of the listeners, using the .on(‘error’, handlerFn) method. It is best practice to log any errors.

eventHandler.on('error', (errorData) => {
    // handle
});

Examples

Handle signal events

CloudprinterOrderValidated

Order received and validated by Cloudprinter.com.

eventHandler.on('CloudprinterOrderValidated', (signalData) => {
    // handle
});

CloudprinterOrderCanceled

Order canceled by Cloudprinter.com.

eventHandler.on('CloudprinterOrderCanceled', (signalData) => {
    // handle
});

ItemValidated

Item received and validated by the production.

eventHandler.on('ItemValidated', (signalData) => {
    // handle
});

ItemProduce

Production has started for an item.

eventHandler.on('ItemProduce', (signalData) => {
    // handle
});

ItemProduced

Production completed for item.

eventHandler.on('ItemProduced', (signalData) => {
    // handle
});

ItemPacked

Item has been packed.

eventHandler.on('ItemPacked', (signalData) => {
    // handle
});

ItemShipped

Item has been shipped. The tracking code for the packet is included in this signal.

eventHandler.on('ItemShipped', (signalData) => {
    // handle
});

ItemError

Item has a problem or an error. This could be due to a print error or failure in the quality check etc.

eventHandler.on('ItemError', (signalData) => {
    // handle
});

ItemCanceled

Confirmation that an item has been canceled in production and in Cloudprinter.com. Cancel requests are normally based on a request by the client.

eventHandler.on('ItemCanceled', (signalData) => {
    // handle
});

Handle multiple signal event

It can be useful when you want to handle more than one event in one function.

eventHandler.onAny(['ItemShipped', 'ItemCanceled'], (signalData) => {
    // handle
});

Getting help

If you get stuck, we're here to help. The following are the best ways to get assistance working through your issue: