Automate Kii Server Extensions with Server Hooks

Kii Server Extension Banner

Suppose you created this great Kii Sever Extension but, instead of programmatically executing it from your client, you want it to run when something meaningful happens on the backend (e.g. a user is created). Wait no more!

Server Hooks allow you run Server Extensions automatically by attaching a Hook config file upon code upload. Your server extension can stay dormant and run when the conditions specified in your config file are met.

Right now Kii Cloud supports two types of Server Hooks:

  • Hook for server triggered execution: Server Code executes when the designated trigger occurrs on the server side (e.g new object created or new user added).
  • Hook for scheduled execution: Server Code executes at a designated time. coming soon!

For server triggered execution, Kii supports running your code when actions such as these happen:

  • Object CRUD operations on buckets:
    • DATA_OBJECT_CREATED: A new object has been created in the specified bucket.
    • DATA_OBJECT_DELETED: An object in the specified bucket has been deleted.
    • DATA_OBJECT_UPDATED: An object in the specified bucket has been updated.
  • User management operations:
    • USER_CREATED: A new user has been created.
    • USER_EMAIL_VERIFIED: A user’s email address has been verified.
    • USER_PHONE_VERIFIED: A user’s phone number has been verified.
    • USER_PASSWORD_RESET_COMPLETED: A user’s password has been reset.
    • USER_PASSWORD_CHANGED: A user’s password has been changed.
    • USER_DELETED: A user has been deleted.
    • USER_UPDATED: A user’s attributes have been updated.
  • Group management operations:
    • GROUP_CREATED: a new group has been created.
    • GROUP_DELETED: a group is deleted.
    • GROUP_MEMBERS_ADDED: A new group member has been added.
    • GROUP_MEMBERS_REMOVED: A group member has been removed.

To use Server Hooks, you’ll need to write your Server Code and save it as a file as usual. But you will also need to prepare a “Server Hook” config file such as this:

{
  "kiicloud://users": [
    {
      "when": "USER_CREATED",
      "what": "EXECUTE_SERVER_CODE",
      "endpoint": "autogift"
    }
  ]
}

In this example, the endpoint autogift will be executed when a new user is created. Let’s take a look at a piece of server code that could be executed when this hook is triggered:

function autogift(params, context, done) {
  var admin = context.getAppAdminContext();
  var user = admin.userWithID(params.userID);

  // Gift for new user.
  var selectedGift = getSignUpGift();
  var giftObject = user.bucketWithName("gifts").createObject();
  giftObject.set("name", "Battle Axe");
  giftObject.set("imageUrl", "uri://axe_image_url_here");
  giftObject.save({
    success: function(targetObj) {
      done(0);
    },
    failure: function(targetObj, error) {
      done(1);
    }
  });
}

When you upload these two together (server code and hook), the above server code will be launched when a new user is created (i.e. when the USER_CREATED trigger is triggered). In the code we are just adding a Battle Axe into the private user’s “gifts” bucket.

That’s all there is to it. With server hooks, automate as much as you can on the server side and leave your app free to provide the best user experience. For more detail on setting up hooks, visit our Server Code Management Guide.

- Share -

German Viscuso

More posts from

  • Kii IoT platform enables customers to create smart, connected solutions and services
  • Long track record of supporting customers like Docomo, Toshiba, Kyocera and many others
  • Strong ecosystem partners across the globe, consisting of carriers, device/sensor manufacturers, system integrators, network/infrastructure providers, chip vendors and solution developers
  • HQ in Tokyo, US HQ in Silicon Valley & offices in China, Taiwan, Hong Kong and Europe (UK, Spain, Germany)
  • www.kii.com