New Rest API - handlePing and other handle functions


I have an question, about new API , I think that I don’t understand new model Smart App, totally. I created SmartApp on base of documentation but if I should create all handle functions by myself (despite of exsisting API) ? And if response is yes, in what way I should create handlePIng function, because I can’t see information on this subject in documentation.

indent preformatted text by 4 spaces
> Import the necessary libraries/declare the necessary objects:
// Basic Server:
var express = require("express");
// Body-Parser
var bodyParser = require("body-parser");
// Request
var request = require('request');
// Http
var http = require('http');
var baseUrl = "https;//";

// Https
var https = require('https');
// Files
var fs = require('fs');

// Http-signature
var httpSignature = require('http-signature');

//Key and cert created by openssl
var options = {
key: fs.readFileSync('data/agent2-key.pem'),
cert: fs.readFileSync('data/agent2-cert.cert')

//create a service
var app = express();

//app.use(myParser.urlencoded({extended : true}));'/smartthings', function (req, response) {
// We don't yet have the public key during PING (when the app is created),
// so no need to verify the signature. All other requests are verified.
if (req.body && req.body.lifecycle === "PING" || signatureIsVerified(req)) {
handleRequest(req, response);
} else {


function signatureIsVerified(req) {
 try {
  let parsed = httpSignature.parseRequest(req);
  if (!httpSignature.verifySignature(parsed, publicKey)) {
  console.log('forbidden - failed verifySignature');
  return false;
 } catch (error) {
  return false;
 return true;

function handleRequest(req, response, error) {
// handle all lifecycles from SmartThings
let evt = req.body;
let lifecycle = evt.lifecycle;
let res = null;

switch(lifecycle) {
    case 'PING':
     res = handlePing(evt.pingData.challenge);
     resp.json({statusCode: 200, pingData: {challenge:res}});
   case 'CONFIGURE':
     res = handleConfig(evt.configurationData);
     resp.json({statusCode: 200, configurationData: res});
   case 'INSTALL':
      handleInstall(evt.installData.installedApp, evt.installData.authToken);
      resp.json({statusCode: 200, installData: {}});
   case 'UPDATE':
      handleUpdate(evt.updateData.installedApp, evt.authToken);
      resp.json({statusCode: 200, updateData: {}});
   // handle other lifecycles...
   case 'EVENT':
     //???????? handleEvent write
     handleEvent(evt.eventData.installedApp, evt.authToken);
     resp.json({statusCode: 200, eventData:{}});
     //???????? handleEvent write   
     handleoAuthCallback(evt.oAuthCallbackData.installedAppId , evt.oAuthCallbackData.urlPath);
     resp.json({statusCode: 200, oAuthCallbackData:{}});
   case 'UNINSTALL':
       handleUninstall(evt.uninstallData.installedApp, evt.uninstallData.authToken);
      resp.json({statusCode: 200, uninstallData:{}});
   console.log(`lifecycle ${lifecycle} not supported`);


function initializing(){
    console.log("Https server works");
//Start the server and make it listen for connections on port 8080 
 //http.createServer(app).listen(3000 , listening);
 https.createServer(options, initializing).listen(8080);

Best regards

The purpose of PING is to verify that your SmartApp exists and is running during app registration. So, your SmartApp will receive a payload as documented here and the app should respond with the challenge code sent.

An example would look like:

    // PING happens during app creation. Purpose is to verify app
    // is alive and is who it says it is.
    case 'PING': {
      let chal = evt.pingData.challenge;
      response.json({statusCode: 200, pingData: {challenge: chal}});


Ok, Thank you for your answer, so in the case of ping event there is no handlePing function, but I understand that these functinos are neccessary for all other lifecycle events:

-oauth callback ?