Cloud code sandbox

Sometimes you need to execute a part of your bussiness logic in a server. You can execute JavaScript code at thethings.iO cloud using the Cloud Code API.

There are three types of Cloud Code: triggers, jobs and functions.

  • Triggers are executed when an event occurs like a thingWrite.
  • Jobs are tasks executed once every hour or day.
  • Functions are snippets of code executed with a REST API call.

You can use triggers to send alarms when the temperature is higher than a treshold or generate events to aggregate later in a job.

Product trigger example

This is a description of how you can set and manage a trigger for a product.

First, go to the Cloud code view clicking on the 'Cloud code' link at the left sidebar.

As you can see, this view is empty because you haven't created a trigger. Later, you will manage your triggers (list, delete and edit) from this view.

Now, just click at the 'Add Trigger' button and you'll see the next form:

This form is very straight forward, you only have to name the trigger, choose the product which you want to apply the trigger, put the code and save.

Once added, the trigger will subscribe to all the devices of a product. That means that every time an event on your devices occurs, we execute the trigger code.

For example (next code example), if a device writes a temperature value of 25, the trigger is triggered and will get this value via the params argument. If our trigger is configured to send an email every time a value is up to 50, the code will compare 25 and 50, and will do nothing. But if the device writes a 60, then the trigger will send an email alerting us.

Javascript exposed methods

The provided Cloud code sandbox only exposes some basic methods:

  • 'setTimeout',
  • 'setInterval',
  • 'clearTimeout',
  • 'clearInterval'

And the exposed services described at our documentation: Exposed APIs

Further reading