Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OAuth_API_withSubscriptions
const RESTrequest = require('request');
/*
* Create Subscription Request
*/
let subscription = {
"sourceType": "CAPABILITY",
"capability": {
"locationId": "xxxxx-xxxx-xxxx-xxxx",
"capability": "switch",
"attribute": "switch",
"value": "*",
"stateChangeOnly": true,
"subscriptionName": "switchHandler"
}
}
let createSubs = {
method: 'POST',
url: `https://api.smartthings.com/v1/installedapps/${appid}/subscriptions`,
headers: {
Authorization: `Bearer ${access_token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(subscription)
};
RESTrequest(createSubs, function (error, RESTresponse) {
if (error){
reject(error);
}else{
if (RESTresponse.statusCode != 200) {
reject(RESTresponse);
} else {
resolve(RESTresponse);
}
}
});
/*
* SmartApp creation
*/
const apiApp = new SmartApp()
.enableEventLogging()
.appId(app_id)//ApplicationID of the API access app (Workspace)
.clientId(client_id)
.clientSecret(client_secret)
.redirectUri(redirect_uri)//redirectUri, not Target URL
.subscribedEventHandler('switchHandler', async (ctx, event) => {
console.log(`EVENT ${JSON.stringify(event)}`)
console.log(`APP ${ctx.installedAppId}`)
})
//Don't forget to add the method that receives the requests from SmartThings (targetURL)
router.post('/', async (req, res) => {
apiApp.handleHttpCallback(req, res);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment