Create and Register Your Schema App
Create Your Schema App
A Schema App is used to handle the interaction between your cloud and the SmartThings Cloud. You will need to set up a Schema App to handle the interaction types.
Your Schema App can be either:
- An AWS Lambda function
- A webhook endpoint
Below, we look at each Schema App option and how you can get started.
AWS Lambda requires a target ARN for each region in which you plan to distribute your integration. For better execution latency, you should deploy your Lambda function in an AWS region that is geographically closer to your users' location.
Lambdas for SmartThings Schema must be deployed in at least one of the following regions, based on your availability:
- ap-northeast-1 (Tokyo)
- us-east-1 (N. Virginia)
- eu-west-1 (Ireland)
Get Started
Get started by creating a new Lambda function at https://aws.amazon.com.
Select Author from Scratch for your Lambda function. Enter the following details for your new Lambda function:
- A name for your function (we use
demoSTSchema
in our example) - Select a runtime (we use
Node.js
in our example) - Role: Create a new role
- Role Name (we use
demoSTSchema
in our example)
Click Create function when you are satisfied with your Lambda function details.
Locate your ARN
Next, locate the ARN after creating your Lambda function. It will look something like this:
arn:aws:lambda:us-east-1:123456789000:function:demoSTSchema
You will provide the ARN to SmartThings when registering your Schema App in a later step.
Provide SmartThings With Permissions to Your Function
Using the AWS CLI, give SmartThings permissions to access your Lambda function with the following command:
aws lambda add-permission --profile default --function-name demoSTSchema --statement-id smartthings --principal 148790070172 --action lambda:InvokeFunction
--profile
is your named profile for your AWS CLI (see AWS CLI documentation for details).--function-name
is the Lambda function created.--principal
148790070172 is the SmartThings account you are granting permissions to.
The principal ID for SmartThings Schema (148790070172
) must not be modified as it identifies the authorized account to invoke your Lambda function.
Add Schema App Code
Use the Schema App example code found below for your Lambda function. The code includes generic responses for SmartThings interaction requests and serves as a starting point for adding the required interaction types.
- Create a directory in your local device.
- Paste and save the Schema App example code found below into a file named
index.js
. - Navigate to the newly created directory using your terminal and install the SmartThings Schema library.
st-schema
is a package published to npm by SmartThings. Install with npm install st-schema
.
- Add
index.js
andnode_modules
to a ZIP file. - In your Lambda function's "code source" section, select upload from a .zip file and upload the file you just created.
- Deploy your Lambda function.
Schema App example code:
'use strict';
const {SchemaConnector} = require('st-schema')
const connector = new SchemaConnector()
.enableEventLogging(2)
.discoveryHandler((accessToken, response) => {
response.addDevice('3rd-party-device-0001', 'Kitchen Light', 'c2c-color-temperature-bulb')
.manufacturerName('Your Company')
.modelName("Model A Tunable White Bulb")
.hwVersion('v1 US Tunable White Bulb')
.swVersion('1.0.0')
.roomName('Kitchen')
.addGroup('Kitchen Table Lights')
})
.stateRefreshHandler((accessToken, response) => {
const device = response.addDevice('3rd-party-device-0001');
const component = device.addComponent('main');
component.addState('st.switch', 'switch', 'off');
component.addState('st.switchLevel', 'level', 80);
component.addState('st.colorTemperature', 'colorTemperature', 3000);
component.addState('st.healthCheck', 'healthStatus', 'online');
})
.commandHandler((accessToken, response, devices) => {
for (const it of devices) {
const device = response.addDevice(it.externalDeviceId);
const component = device.addComponent("main");
for (const command of it.commands) {
switch(command.capability) {
case 'st.switch':
component.addState('st.switch', 'switch', command.command);
break;
case 'st.switchLevel':
component.addState('st.switchLevel', 'level', command['arguments'][0]);
break;
case 'st.colorTemperature':
component.addState('st.colorTemperature', 'colorTemperature', command['arguments'][0]);
break;
}
}
}
})
.callbackAccessHandler(async (accessToken, callbackAuthentication, callbackUrls) => {
// TODO -- store the tokens and URLs for use in proactive state callbacks
})
.integrationDeletedHandler(accessToken => {
// TODO -- any cleanup necessary when an integration is removed from SmartThings
});
exports.handler = async (evt, context) => {
await connector.handleLambdaCallback(evt, context);
};
If you want to use a webhook endpoint for your Schema App, you can find a tutorial walking you through the process in the SmartThings Community.
Register Your Schema App
After creating your Schema App, you are now ready to register your app with the SmartThings platform.
The Schema Cloud Connector provides an interactive UI for you to register and manage your Schema App. Ensure you are signed in using the same account you intend to use to apply for WWST or are a member of the same organization as the account you intend to use when applying for WWST.
Before registering your Schema App, make sure you have the following information available:
From your OAuth 2 client application:
- Client ID or
client_id
- A unique public string used to ID your cloud.
- Client secret or
client_secret
- Used to authenticate with the Access Token URL. This is combined with the Client ID to identify the request.
- OAuth URL or
authorization_url
- The authorization URL provided by your OAuth application.
- OAuth scope or
scopes
- Specified the OAuth scopes required for your cloud to communicate with the SmartThings cloud.
- Token refresh URL or
token_url
- The refresh URL provided by your OAuth application.
- Alert notification email
- An email address used to inform you if errors are identified on the SmartThings platform.
If using AWS Lambda:
- Target ARNs for the following regions (if applicable to your product region availability):
- North America
- Europe
- Asia Pacific
If using a webhook endpoint:
- Target URL
- A secure URL that can receive incoming JSON payloads from SmartThings using HTTP POST requests.
After completing registration, you will be presented with a SmartThings Client ID and SmartThings Client Secret. Save these values in a secure location! You will not be able to retrieve these values after this point. You can generate new values using the SmartThings API.
Now that you have your Schema App, you are ready to register it with the SmartThings platform using the SmartThings CLI.
Ensure you are signed in to the SmartThings CLI using the same account you intend to use to register your products in the Certification Console or are a member of the same organization as the account you intend to use when registering your products in the Certification Console.
If you intend to use the SmartThings CLI to regularly develop for an Organization, you can specify a default Organization in the CLI.
Use of AWS Lambda requires a target ARN for each region where your device is supported.
If using AWS Lambda, you will be asked to provide the following when registering your Schema integration in the CLI:
Partner name
: The name of the entity or organization publishing this integration.User email
: An email address to associate with this integration.App name
: The desired name of your integration. This name will be displayed to users in the SmartThings app.OAuth authorization URL
: The authorization URL provided by your OAuth application.Partner OAuth refresh token URL
: The refresh token URL provided by your OAuth application.Icon URL
(optional): URL of the icon to display when users authenticate with your cloud. When registering your devices in the Console in a later step, this value will be overridden by any value provided in your Console certification submission.2x Icon URL
(optional): URL of the icon to display when using 2x.3x Icon URL
(optional): URL of the icon to display when using 3x.Partner OAuth Client Id
: A unique public string used to identify the device cloud.Partner OAuth client Secret
: Used to authenticate with the Access Token URI. This is combined with the client ID to identify the request.Partner OAuth Scope
(optional): Specifies the OAuth scopes required for your cloud to communicate with the SmartThings Cloud.Lambda ARN for US region
(optional): Lambda ARN to use for United States region.Lambda ARN for EU region
(optional): Lambda ARN to use for European Union region.Lambda ARN for AP region
(optional): Lambda ARN to use for Asia-Pacific region.Enable app-to-app linking?
: App-to-App Account Linking allows users to link their account on your platform with their SmartThings account when your app and the SmartThings app are installed on the user's mobile device. Linking is initiated from within the SmartThings app.Android Link
: Android App Link to your application.iOS Link
: iOS universal link to your application.
Use of a webhook endpoint requires a Target URL where your OAuth app is hosted.
If using a webhook endpoint, you will be asked to provide the following when registering your Schema integration in the CLI:
Partner name
: The name of the entity or organization publishing this integration.User email
: An email address to associate with this integration.App name
: The desired name of your integration. This name will be displayed to users in the SmartThings app.OAuth authorization URL
: The authorization URL provided by your OAuth application.Partner OAuth refresh token URL
: The refresh token URL provided by your OAuth application.Icon URL
(optional): URL of the icon to display when users authenticate with your cloud. When registering your devices in the Console in a later step, this value will be overridden by any value provided in your Console certification submission.2x Icon URL
(optional): URL of the icon to display when using 2x.3x Icon URL
(optional): URL of the icon to display when using 3x.Partner OAuth Client Id
: A unique public string used to identify the device cloud.Partner OAuth client Secret
: Used to authenticate with the Access Token URI. This is combined with the client ID to identify the request.Partner OAuth Scope
(optional): Specifies the OAuth scopes required for your cloud to communicate with the SmartThings Cloud.Webhook URL
: The URL your application will receive events at.Enable app-to-app linking?
: App-to-App Account Linking allows users to link their account on your platform with their SmartThings account when your app and the SmartThings app are installed on the user's mobile device. Linking is initiated from within the SmartThings app.Android Link
: Android App Link to your application.iOS Link
: iOS universal link to your application.
After installing the SmartThings CLI and ensuring you are signed in with the desired SmartThings account, run the following command in the SmartThings CLI to begin the registration process:
smartthings schema:create
After completing all fields, you will have a chance to change values or preview the final JSON or YAML files before finishing the registration process.
When your Schema integration is successfully registered, you will be presented with the following items:
Endpoint App Id
St Client Id
St Client Secret
Save these values in a secure location! You will not be able to retrieve these values after this point. You can generate new a new client ID and secret using the SmartThings API.
Up Next
-
After registering your Schema App with SmartThings, be sure to verify that your Schema App supports the required interaction types.
-
Devices using SmartThings Schema must be associated with a Device Handler Type. Review Device Handler Types to find a pre-made device handler type that fits your device.
-
Implement optional app-to-app linking to create an even more seamless experience for Android and iOS users to link their SmartThings account with your platform.