Access Layer’s Server API (SAPI) services using a variant of @layerhq/web-xdk that exposes SAPI operations rather than Browser/user-centric operations.


This was tested with Node v9.4.0.


This is distributed as an npm repo:

npm install @layerhq/web-xdk-sapi-prerelease


const Layer = require('@layerhq/web-xdk-sapi-prerelease');

var client = Layer.init({
  appId: "layer:///apps/staging/UUID",

Use Cases

Much of what makes the @layerhq/web-xdk work in a browser has been stripped out. What remains are support for the following use cases:

  • Create Message Models, turn them into Messages and send them
  • Receive Messages via SAPI or Webhooks (through your own code) and feed them into this code to turn them into easily understood Message Models

Creating Message Models

const Layer = require('@layerhq/web-xdk-sapi-prerelease');

// Get the Class definition for a Text Message Model
const TextModel = Layer.Core.Client.getMessageTypeModelClass('TextModel');

const senderId = 'layer:///identities/frodo-the-dodo';
const conversationId = 'layer:///conversations/UUID';

var model = new TextModel({
  text: "This is the body of your message",
  title: "This is your title"

  senderId: senderId,
  conversationId: conversationId,
.then(model => console.log('MESSAGE SENT: ', model.text, model.message))
.catch(result => console.log('MESSAGE ERROR: ', result));

The following Message Type Models are built in; all others must be explicitly imported:

  • Text Message
  • Response Message
  • Status Message
  • Link Message
  • Image Message
  • Button Message
  • File Message
  • Choice Message

To use a Product Message, import it first:

const ProductModel = require('@layerhq/web-xdk-prerelease/ui/messages/product/layer-product-message-model');

var model = new ProductModel({
  currency: 'USD',
  price: 175,
  quantity: 3,
  brand: 'Apple',
  name: 'Apple 2 plus desktop computer',
}).send({ senderId, conversationId });

For instructions on instantiating different types of models, and what properties they support, see WebXDK Docs.

Parsing Message Data

  • client.createObject will parse any Message, Conversation or Identity Object from the server into a Layer.Core.Message, Layer.Core.Conversation or Layer.Core.Identity.

This example gets a simple one line summary describing the message; this is the same text used in the Web XDK’s Conversation List showing the most recent message.

function getMessageSummary(messageObj) {
  const message = client.createObject(messageObj);
  const model = message.createModel();
  return model.getOneLineSummary();
Previewing Messages API Reference