A Node.js library, which provides a wrapper for the Layer Platform API.
The Layer Platform API is designed to empower developers to automate, extend, and integrate functionality provided by the Layer platform with other services and applications. For more on this see our blog post.
This library supports requests from your servers only.
You can find full documentation on Platform API at developer.layer.com/docs/platform.
npm install layer-api
var LayerAPI = require('layer-api');
// Initialize by providing your Layer credentials
var layer = new LayerAPI({
token: API_TOKEN,
appId: APP_ID
});
// Create a Conversation
layer.conversations.create({participants: ['abcd']}, function(err, res) {
var cid = res.body.id;
// Send a Message
layer.messages.sendTextFromUser(cid, 'abcd', 'Hello, World!', function(err, res) {
console.log(err || res.body);
});
});
To use this library you need to create a new instance of the layer-api
module by passing config
object to a constructor.
Layer API constructor is initialized with the following configuration values:
token
- Layer Platform API token which can be obtained from Developer DashboardappId
- Layer application ID
Optional values:
version
- API version to use (default:1.0
)timeout
- Request timeout in milliseconds (default:10000
milliseconds)agent
- Instance of https.Agent to use HTTPS KeepAliveagentOptions
- Or use Agent options hash directlydebug
- Enable debugging (default:false
)
Conversations coordinate messaging within Layer and can contain up to 25 participants. All Messages sent are sent within the context of a conversation.
Create a new Conversation by providing paylod. Payload should contain at least participants
array. Optional properties are metadata
object and distinct
boolean.
payload
- Payload objectcallback(err, res)
- Optional Callback function returns an error and response objects
layer.conversations.create({participants: ['abcd']}, function(err, res) {
if (err) return console.error(err);
// conversation ID
var cid = res.body.id;
});
Same as create conversation above but including de-duplicating UUID value.
Retrieve an existing Conversation by providing conversation ID. Response body
will result in conversation object representation.
cid
- Conversation IDcallback(err, res)
- Callback function returns an error and response objects
layer.conversations.get(cid, function(err, res) {
if (err) return console.error(err);
// conversation data
var conversation = res.body;
});
Retrieve an existing Conversation by providing user ID and conversation ID. Response body
will result in conversation object representation.
uid
- User IDcid
- Conversation IDcallback(err, res)
- Callback function returns an error and response objects
layer.conversations.getFromUser(uid, cid, function(err, res) {
if (err) return console.error(err);
// conversation data
var conversation = res.body;
});
Retrieve all Conversations by providing user ID. Response body
will result in an array of conversation objects.
uid
- User IDparams
- Optional Query parameters can containpage_size
,from_id
andsort_by
callback(err, res)
- Callback function returns an error and response objects
var params = {
page_size: 50,
from_id: cid,
sort_by: 'last_message' // `created_at` or `last_message`
};
layer.conversations.getAllFromUser(uid, params, function(err, res) {
if (err) return console.error(err);
// conversation data
var conversation = res.body;
});
Set metadata on an existing Conversation by providing conversation ID and properties
object.
cid
- Conversation IDproperties
- Metadata properties objectcallback(err, res)
- Optional Callback function returns an error and response objects
var properties = {
foo: 'bar'
};
layer.conversations.setMetadataProperties(cid, properties, function(err, res) {
if (err) return console.error(err);
});
[Delete metadata]((https://developer.layer.com/docs/platform#set-amp-delete-metadata) on an existing Conversation by providing conversation ID and properties
object.
cid
- Conversation IDproperties
- Properties objectcallback(err, res)
- Optional Callback function returns an error and response objects
Edit an existing Conversation by providing conversation ID and one or more operations
as described by the Layer Patch format.
cid
- Conversation IDoperations
- Layer Patch operations arraycallback(err, res)
- Optional Callback function returns an error and response objects
Add participants to an existing Conversation by providing conversation ID and array of participants
.
cid
- Conversation IDparticipants
- Array of participantscallback(err, res)
- Optional Callback function returns an error and response objects
var participants = ['user1'];
layer.conversations.addParticipants(cid, participants, function(err, res) {
if (err) return console.error(err);
});
Remove participants of an existing Conversation by providing conversation ID and array of participants
.
cid
- Conversation IDparticipants
- Array of participantscallback(err, res)
- Optional Callback function returns an error and response objects
Replace all participants of an existing Conversation by providing conversation ID and array of participants
.
cid
- Conversation IDparticipants
- Array of participantscallback(err, res)
- Optional Callback function returns an error and response objects
Delete an existing Conversation by providing conversation ID.
cid
- Conversation IDcallback(err, res)
- Optional Callback function returns an error and response objects
layer.conversations.delete(cid, function(err, res) {
if (err) return console.error(err);
});
Messages can be made up of one or many individual pieces of content.
- Message
sender
can be specified byuser_id
orname
, but not both. - Message
parts
are the atomic object in the Layer universe. They represent the individual pieces of content embedded within a message. - Message
notification
object represents push notification payload.
Send a Message by providing conversation ID and payload.
cid
- Conversation IDpayload
- Message payload containingsender
andparts
datacallback(err, res)
- Optional Callback function returns an error and response objects
var payload = {
sender: {
user_id: 'abcd'
},
parts: [
{
body: 'Hello, World!',
mime_type: 'text/plain'
}
]
};
layer.messages.send(cid, payload, function(err, res) {
if (err) return console.error(err);
// message ID
var messageId = res.body.id;
});
Same as send a message above but including de-duplicating UUID value.
Shorthand method for sending a plain text Message by providing userId
and text
.
cid
- Conversation IDuserId
- User ID of the participant that this message will appear to be fromtext
- Text or base64 encoded data for your messagecallback(err, res)
- Optional Callback function returns an error and response objects
Shorthand method for sending a plain text Message by providing name
and text
.
cid
- Conversation IDname
- Arbitrary string naming the service that this message will appear to be fromtext
- Text or base64 encoded data for your messagecallback(err, res)
- Optional Callback function returns an error and response objects
Retrieve all messages in a conversation by providing cid
. Response body
will result in array of messages.
cid
- Conversation IDparams
- Optional Query parameters can containpage_size
,from_id
andsort_by
callback(err, res)
- Callback function returns an error and response objects
var params = {
page_size: 50,
from_id: messageId,
sort_by: 'last_message' // `created_at` or `last_message`
};
layer.messages.getAll(cid, params, function(err, res) {
if (err) return console.error(err);
var messages = res.body;
});
Retrieve all messages in a conversation from a specific user by providing userId
and cid
. Response body
will result in array of messages.
userId
- User IDcid
- Conversation IDparams
- Optional Query parameters can containpage_size
,from_id
andsort_by
callback(err, res)
- Callback function returns an error and response objects
Retrieve a single message in a conversation from a specific user by providing userId
and messageId
. Response body
will result in a single message representation.
userId
- User IDmessageId
- Message IDcallback(err, res)
- Callback function returns an error and response objects
Announcements are messages sent to all users of the application or to a list of users. Recipients should be specified in the recipients
property, which takes an array of user IDs.
Send an Announcement by providing a payload.
payload
- Message payload containingrecipients
,sender
andparts
datacallback(err, res)
- Optional Callback function returns an error and response objects
var payload = {
recipients: ['abcd', '12345'],
sender: {
name: 'The System'
},
parts: [
{
body: 'Hello, World!',
mime_type: 'text/plain'
}
]
};
layer.announcements.send(payload, function(err, res) {
if (err) return console.error(err);
// announcement data
var announcement = res.body;
});
Same as send an announcement above but including de-duplicating UUID value.
Layer Platform API allows you to manage a block list in order to align with your own application level blocking. A block list is maintained for each user, enabling users to manage a list of members they don't want to communicate with.
ownerId
The owner of the block listuserId
A user that is being blocked from communicating with theownerId
Retrieve an array of all blocked users for the specified owner.
ownerId
- The owner of the block listcallback(err, res)
- Callback function returns an error and response objects
layer.blocklist.get(ownerId, function(err, res) {
if (err) return console.error(err);
// block list array
var blocklist = res.body;
});
Add a new blocked user to the block list for the specified owner.
ownerId
- The owner of the block listuserId
- A user that is being blocked by the ownercallback(err, res)
- Optional Callback function returns an error and response objects
layer.blocklist.block(ownerId, userId, function(err, res) {
if (err) return console.error(err);
// user blocked
});
Remove a user from the block list for the specified owner.
ownerId
- The owner of the block listuserId
- A user that is being blocked by the ownercallback(err, res)
- Optional Callback function returns an error and response objects
layer.blocklist.unblock(ownerId, userId, function(err, res) {
if (err) return console.error(err);
// user unblocked
});
Layer Platform API allows you to manage Identities in order to associate data about your users with the user. Helpful for quickly rendering information about a user when showing a Message from them.
An Identity consists of the following properties:
display_name
: Required Display nameavatar_url
: Optional Avatar URLfirst_name
: Optional First namelast_name
: Optional Last namephone_number
: Optional Phone numberemail_address
: Optional Email addresspublic_key
: Optional Encryption keymetadata
: Optional Hash of custom keys
Creates an Identity on the Identities server.
userId
- User ID of the user we are creatingproperties
- Properties of the user to be created. (display_name
is a required property)callback(err)
- Optional Callback function returns an error or status 201
layer.identities.create('frodo', {
display_name: 'Frodo the Dodo',
first_name: 'Frodo',
last_name: 'Baggins'
}, function(err, res) {
if (err) return console.error(err);
// Identity created
});
Retrieves an Identity from the Identities server.
userId
- User ID of the user we are updatingcallback(err)
- Callback function returns an error or identity object
layer.identities.get('frodo', function(err, res) {
if (err) return console.error(err);
// Identity retrieved:
console.log('User: ' + res.body.display_name);
});
Updates an Identity on the Identities server.
userId
- User ID of the user we are updatingproperties
- Properties of the user that should be updated.callback(err)
- Optional Callback function returns an error or status 204
layer.identities.edit('frodo', {
first_name: 'Frodo 2'
}, function(err, res) {
if (err) return console.error(err);
// Identity updated
});
Replace all properties on Identity.
userId
- User ID of the user we are updatingproperties
- Properties of the user that should be updated. (display_name
is a required property)callback(err)
- Optional Callback function returns an error or status 204
layer.identities.replace('frodo', {
display_name: 'Frodo the Dodo 3',
first_name: 'Frodo 3'
}, function(err, res) {
if (err) return console.error(err);
// Identity updated
});
Deletes an Identity from the Identities server.
userId
- User ID of the user we are deletingcallback(err)
- Optional Callback function returns an error or status 204
layer.identities.delete("frodo", function(err, res) {
if (err) return console.error(err);
// Identity deleted
});
All the above functions can be used to return a promise by appending the Async
suffix to the function name e.g.:
conversations.createAsync({participants: ['abcd']}).then(function(res) {
// conversation ID
var cid = res.body.id;
}).catch(function(err) {
console.error(err);
});
The unit tests are based on the mocha module, which may be installed via npm. To run the tests make sure that the npm dependencies are installed by running npm install
from the project directory.
npm test
Layer API is an Open Source project maintained by Layer. Feedback and contributions are always welcome and the maintainers try to process patches as quickly as possible. Feel free to open up a Pull Request or Issue on Github.
For a list of version changes please refer to Github releases page.