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.
The latest stable version is available in NPM and can be installed using
npm install --save losant-rest
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);
});
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.
A client is a single api instance. By default, it is unauthenticated, but can be given an access token to perform authenticated requests.
-
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.
- Application
- Application Api Token
- Application Api Tokens
- Application Certificate
- Application Certificate Authorities
- Application Certificate Authority
- Application Certificates
- Application Dashboard
- Application Dashboards
- Application Job Log
- Application Job Logs
- Application Key
- Application Keys
- Application Template
- Application Templates
- Applications
- Audit Log
- Audit Logs
- Auth
- Credential
- Credentials
- Dashboard
- Dashboards
- Data
- Data Table
- Data Table Row
- Data Table Rows
- Data Tables
- Device
- Device Attribute
- Device Attributes
- Device Recipe
- Device Recipes
- Devices
- Edge Deployment
- Edge Deployments
- Embedded Deployment
- Embedded Deployments
- Event
- Events
- Experience
- Experience Domain
- Experience Domains
- Experience Endpoint
- Experience Endpoints
- Experience Group
- Experience Groups
- Experience Slug
- Experience Slugs
- Experience User
- Experience Users
- Experience Version
- Experience Versions
- Experience View
- Experience Views
- File
- Files
- Flow
- Flow Version
- Flow Versions
- Flows
- Instance
- Instance Api Token
- Instance Api Tokens
- Instance Audit Log
- Instance Audit Logs
- Instance Custom Node
- Instance Custom Nodes
- Instance Member
- Instance Members
- Instance Notification Rule
- Instance Notification Rules
- Instance Org
- Instance Org Invite
- Instance Org Invites
- Instance Org Member
- Instance Org Members
- Instance Orgs
- Instance Sandbox
- Instance Sandboxes
- Instances
- Integration
- Integrations
- Me
- Notebook
- Notebooks
- Org
- Org Invites
- Orgs
- Resource Job
- Resource Jobs
- User Api Token
- User Api Tokens
- Webhook
- Webhooks
Copyright (c) 2025 Losant IoT, Inc