SmartThings Core SDK

The SmartThings Core SDK is a wrapper designed to simplify the use of the SmartThings REST API from JavaScript and TypeScript applications. This is the very first release of this SDK and should be considered a work in progress. Changes may still be made that are not backwardly compatible.

Installation

npm install @smartthings/core-sdk

Importing

NodeJS:

const {SmartThingsClient} = require('@smartthings/core-sdk')

Or ES2015+:

import {SmartThingsClient} from '@smartthings/core-sdk'

Example Usage

Substitue your Personal Access Token (PAT) with at least the r:locations:* scope for {YOUR-PAT-TOKEN} in the following code.

const {SmartThingsClient, BearerTokenAuthenticator} = require('@smartthings/core-sdk')
const client = new SmartThingsClient(new BearerTokenAuthenticator('{YOUR-PAT-TOKEN}'))

client.locations.list().then(locations => {
    console.log(`Found ${locations.length} locations`)
})

Logging

There is some logging done of requests and responses made to the API. The default logger does nothing but you can pass your own. Logging is done via a generic interface so you can use whatever logger you want in your application.

First, write an implementation of the Logger interface defined in logger.ts which proxies to your logger. For example:

import { Logger as WinstonLogger } from 'winston'
import { Logger } from '@smartthings/core-sdk'

export class WinstonLoggerProxy implements Logger {
    proxy: WinstonLogger
    level: string

    constructor(winstonLogger) {
        this.level = proxy.level
    }

    trace(message: any, ...args: any[]): void {
        // Winston doesn't have a "trace" level but it has a "silly" level in the same place.
        proxy.silly(message, args)
    }

    debug(message: any, ...args: any[]): void {
        proxy.debug(message, args)
    }

    info(message: any, ...args: any[]): void {
        proxy.info(message, args)
    }

    ...

    isTraceEnabled(): boolean {
        return proxy.isSillyEnabled()
    }

    ...
}

Then, when you create your SmartThingsClient, pass this in via the config parameter.

const config = {
    logger: new WinstonLoggerProxy(myWinstonLoggerInstance)
}
const client = new SmartThingsClient(new BearerTokenAuthenticator('{YOUR-PAT-TOKEN}'), config)

Reference

Authenticators

This SDK supports multiple ways of authenticating with the SmartThings platform. The currently available authenticators are:

Endpoints