SmartThings

IoT

Introduction to SmartThings Public API

SmartThings is an IoT platform that provides various smart devices to users. These smart devices can be controlled via the SmartThings mobile app. SmartThings also provides a public API that allows developers to create their own custom applications and integrations with the SmartThings platform.

The SmartThings Public API documentation can be found at http://developer.smartthings.com/. This documentation provides various API endpoints for developers to interact with the SmartThings platform.

Example API Code in JavaScript

Let's take a look at some example code in JavaScript that can interact with the SmartThings Public API.

Getting an Access Token

Before we can interact with the SmartThings Public API, we need to get an access token. We can get an access token by sending a POST request to the following endpoint:

const axios = require('axios');

// Send a POST request with the client ID and client secret to get an access token
axios.post(
  'https://auth-global.api.smartthings.com/oauth/token', 
  {
    client_id: 'CLIENT_ID',
    client_secret: 'CLIENT_SECRET',
    grant_type: 'client_credentials',
    scope: 'app'
  }
)
.then(response => {
  const accessToken = response.data.access_token;
  console.log('Access Token:', accessToken);
})
.catch(error => {
  console.log('Error:', error);
});

Creating a Device

We can create a device on the SmartThings platform by sending a POST request to the following endpoint:

// Send a POST request with the device details to create a new device
axios.post(
  'https://api.smartthings.com/v1/devices',
  {
    label: 'My New Device',
    deviceTypeID: 'myDeviceType',
    deviceTypeName: 'My Device Type',
    components: [
      {
        id: 'main',
        capabilities: [
          {
            id: 'switch',
            version: 1
          }
        ]
      }
    ]
  },
  {
    headers: {
      Authorization: `Bearer ${accessToken}`
    }
  }
)
.then(response => {
  console.log('Device:', response.data);
})
.catch(error => {
  console.log('Error:', error);
});

Getting Device Status

We can get the status of a device by sending a GET request to the following endpoint:

// Send a GET request to get the device status
axios.get(
  'https://api.smartthings.com/v1/devices/DEVICE_ID/status',
  {
    headers: {
      Authorization: `Bearer ${accessToken}`
    }
  }
)
.then(response => {
  console.log('Device Status:', response.data);
})
.catch(error => {
  console.log('Error:', error);
});

Conclusion

This blog post covered the basics of the SmartThings Public API and provided some example code in JavaScript that can interact with the API. With the SmartThings Public API, developers can create custom applications and integrations that can enhance the user experience of the SmartThings platform.

Related APIs in IoT