Objects

This document will cover all the nodejs methods related to objects

Objects represent non-user entities such as organizations, teams, roles, and projects. Understand more about objects here.

Create or update an object

This is an upsert operation which creates or updates the object.

const {Suprsend} = require("@suprsend/node-sdk");

const supr_client = new Suprsend("__workspace_uid__", "__worksapce_secret__") // initialize SDK

const response = await supr_client.objects.upsert(object_type, object_id);

Edit an object

There are 2 ways in which you can edit an object data.

  • Build edit payload yourself
  • Use helper methods provided by SDK (Recommended)

Build edit payload yourself

Use this to modify an object, typically for removing channels or unsetting properties. The payload will follow the same structure as the Object Edit API.

//for all supported operations, refer API documentation
const payload = {operations: [{$unset: ["name"]}, {$remove: {"$email": "[email protected]"}}]}

const response = await supr_client.objects.edit(object_type, object_id, payload};

Edit using helper methods

It is possible to use the SDK's helper methods to perform edit operations on an object.

For this, first create object instance then call any of the helper methods mentioned below and finally save the changes.

// create instance
const object_instance = supr_client.objects.get_instance(object_type, object_id);

// call any helper methods
object_instance.set(key, value);
object_instance.add_email("[email protected]");

// save the changes
const response = await supr_client.objects.edit(object_instance);

Add channels helper methods

Use object_instance.add_* method(s) to add channels.

object_instance.add_email("[email protected]");

object_instance.add_sms("+15555555555");

object_instance.add_whatsapp("+15555555555");

object_instance.add_androidpush("__android_push_fcm_token__");

object_instance.add_iospush("__iospush_token__");

// - To add Slack using email
object_instance.add_slack(
  {
    "email": "[email protected]",
    "access_token": "xoxb-XXXXXXXX"
  });

// - To add Slack if slack member_id is known
object_instance.add_slack(
  {
    "user_id": "U03XXXXXXXX",
    "access_token": "xoxb-XXXXXXXX"
  });

// - To add Slack channel
object_instance.add_slack(
  {
    "channel_id": "CXXXXXXXX",
    "access_token": "xoxb-XXXXXXXX"
  });

// - To add Slack incoming webhook
object_instance.add_slack(
  {
    "incoming_webhook": {
      "url": "https://hooks.slack.com/services/TXXXXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXXXX"
    }
  });

// - To add MS teams user or channel using conversation_id
object_instance.add_ms_teams(
  {
		"tenant_id": "c1981ab2-9aaf-xxxx-xxxx",
		"service_url": "https://smba.trafficmanager.net/amer",
		"conversation_id": "19:c1524d7c-a06f-456f-8abe-xxxx"
  });

// - To add MS teams user using user_id
object_instance.add_ms_teams(
  {
		"tenant_id": "c1981ab2-9aaf-xxxx-xxxx",
		"service_url": "https://smba.trafficmanager.net/amer",
		"object_ins_id": "29:1nsLcmJ2RKtYH6Cxxxx-xxxx"
  });

// - To add MS teams using incoming webhook
object_instance.add_ms_teams(
  {
    "incoming_webhook": {
      "url": "https://wnk1z.webhook.office.com/webhookb2/XXXXXXXXX"
    }
  });

Remove channels helper methods

Use object_ins.remove_* method(s) to remove channels from an object.

object_instance.remove_email("[email protected]");

object_instance.remove_sms("+15555555555");

object_instance.remove_whatsapp("+15555555555");

object_instance.remove_androidpush("__android_push_fcm_token__"); // - by default, token is assumed to be fcm-token

object_instance.remove_iospush("__iospush_token__");

// - To remove Slack email
object_instance.remove_slack(
  {
    "email": "[email protected]",
    "access_token": "xoxb-XXXXXXXX"
  });  

// - To remove Slack if slack member_id is known
object_instance.remove_slack(
  {
    "object_ins_id": "U03XXXXXXXX",
    "access_token": "xoxb-XXXXXXXX"
  });

// - To remove Slack channel
object_instance.remove_slack(
  {
    "channel_id": "CXXXXXXXX",
    "access_token": "xoxb-XXXXXXXX"
  });

// - To remove Slack incoming webhook
object_instance.remove_slack(
  {
    "incoming_webhook": {
      "url": "https://hooks.slack.com/services/TXXXXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXXXX"
    }
  });

// - To add MS teams user or channel using conversation_id
object_instance.remove_ms_teams(
  {
		"tenant_id": "c1981ab2-9aaf-xxxx-xxxx",
		"service_url": "https://smba.trafficmanager.net/amer",
		"conversation_id": "19:c1524d7c-a06f-456f-8abe-xxxx"
  });

// - To add MS teams user using user_id
object_instance.remove_ms_teams(
  {
		"tenant_id": "c1981ab2-9aaf-xxxx-xxxx",
		"service_url": "https://smba.trafficmanager.net/amer",
		"object_ins_id": "29:1nsLcmJ2RKtYH6Cxxxx-xxxx"
  });

// - To add MS teams using incoming webhook
object_instance.remove_ms_teams(
  {
    "incoming_webhook": {
      "url": "https://wnk1z.webhook.office.com/webhookb2/XXXXXXXXX"
    }
  });

Remove channels in bulk

If you need to delete/unset all emails (or any other channel data) of an object, you can call object_ins.unset() method. The method accepts the channel key/s (a single key or list of keys).

object_instance.unset("$email");
object_instance.unset(["$email", "$sms", "$whatsapp"]);

// available channel keys - 
//["$email","$whatsapp","$sms","$androidpush","$iospush","$webpush","$slack","$ms_teams"]

Set preferred language

object_instance.set_preferred_language("en");

Set timezone

object_instance.set_timezone("America/Belize");

Set

Set any custom property using this method. It will shallow merge existing properties with new values. Key shouldn't start with $ or ss_.

object_instance.set("key", "value");

object_instance.set({
  "prop1": "val1", 
  "prop2": ["one", "two", "three"], 
  "number": 20
});

Unset

Use this to unset existing properties.

object_instance.unset("wishlist");
object_instance.unset(["wishlist", "cart"]);

Append

Use this to append item to an array based property.

object_instance.append("wishlist", "iphone12");
object_instance.append({"wishlist" : "iphone12", "cart" : "Apple airpods"});

Remove

Use this to remove an item from array based property.

o1.remove("wishlist", "iphone12");
o1.remove({"wishlist" : "iphone12", "cart": "Apple airpods"});

Set once

Use this to add new properties which are not overridden, such as first_login_at.

object_instance.set_once("first_login", "2021-11-02");
object_instance.set_once({"first_login" : "2021-11-02", "DOB" : "1991-10-02"});

Increment

Use on numeric values to increment/decrement. Provide a negative value for decrement.

object_instance.increment("login_count", 1);
object_instance.increment({"login_count" : 1, "remaining_credits" : -1});

List objects

const response = await supr_client.objects.list(object_type,{"limit":10, "after:"cursor"});

You can pass optional query parameters -limit,before,after


Object details

const response = await supr_client.objects.get(object_type, object_id);

Delete object

const response = await supr_client.objects.delete(object_type, object_id);

Bulk delete objects

const response = await supr_client.objects.bulk_delete(object_type, {object_ids: ["object_id1", "object_id2"]});

Create subscriptions to object

const payload = {
  "recipients": ["recipient1", "recipient2"],
  "properties": {"role":"manager"}
}

const response = await supr_client.objects.create_subscriptions(object_type, object_id, payload);

List subscriptions of object

const response = await supr_client.objects.get_subscriptions(object_type, object_id,{"limit":10})

You can pass optional query parameters -limit,before,after.


Delete subscriptions in object

const payload = {"recipients": ["recipient1", "recipient2"]}

const response = await supr_client.objects.delete_subscriptions(object_type, object_id, payload)

Get list of objects subscribed by object

An object can subscribe to other objects. Use this method to get the list of all objects that the current object has subscribed to.

const response = await supr_client.objects.get_objects_subscribed_to(object_type, object_id);