Skip to content

Losant/losant-rest-js

Repository files navigation

Losant Javascript REST API Client

Build Status npm version

The Losant REST API client provides a simple way to use the comprehensive Losant API. You can authenticate either as a Losant device or with your user account, and have access to all the functionality of the Losant platform.

This client works both in browsers and node.js (v20 and newer), and supports both callbacks and promises. It uses Axios under the covers for the actual HTTP communication.


Installation

The latest stable version is available in NPM and can be installed using

npm install --save losant-rest

Example

Below is a high-level example of using the Losant JavaScript REST API client to authenticate against the Losant Platform and report state for a device.

import api from 'losant-rest';
import mySensor from './my-analog-sensor';

const client = api.createClient();
const {
  applicationId, token
} = await client.auth.authenticateDevice({ credentials: {
  deviceId: 'my-device-id',
  key: 'my-app-access-key',
  secret: 'my-app-access-secret'
}});

client.setOption('accessToken', token);
const sendSensorDataToLosant = async () => {
  const state = { data: { temperature: mySensor.read() }, time: Date.now() };
  return client.device.sendState({
    deviceId: 'my-device-id',
    applicationId: appId,
    deviceState: state
  });
};

setInterval(async () => {
  await sendSensorDataToLosant();
}, 60000);

OR

var api = require('losant-rest');
var mySensor = require('./my-analog-sensor');

var client = api.createClient();

client.auth.authenticateDevice({ credentials: {
  deviceId: 'my-device-id',
  key: 'my-app-access-key',
  secret: 'my-app-access-secret'
}}).then(function (response) {
  client.setOption('accessToken', response.token);
  var appId = response.applicationId;

  var state = { data: { temperature: mySensor.read() } };
  return client.device.sendState({
    deviceId: 'my-device-id',
    applicationId: appId,
    deviceState: state
  });
})
.then(function (response) {
  console.log(response); // { success: true }
})
.catch(function (error) {
  console.error(error);
});

API Documentation

const client = api.createClient({ accessToken: undefined, url: 'https://api.losant.com' });

The losant-rest module exposes a single function named createClient which returns a client instance. It takes the following options:

  • accessToken
    The access token to be used for authentication - by default there is no access token. An access token can be acquired through any of the Auth methods, or can be created for a particular application through applicationApiTokens.

  • url
    The url of the Losant API - by default https://api.losant.com.

Client

A client is a single api instance. By default, it is unauthenticated, but can be given an access token to perform authenticated requests.

Functions

  • setOption(name, value)
    The setOption function can be used to change any of the options currently set on the client, such as the access token.

  • getOption(name)
    The getOption function can be used to retrieve the current value of any of the options set on the client instance.

API Reference



Copyright (c) 2025 Losant IoT, Inc

https://www.losant.com

About

JavaScript REST Client for consuming the Losant IoT Platform API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 7