Threads can be assigned to users or machine users. The latter is useful if you want a bot to handle or are building a complex automation of some kind.

Assigning a thread

To assign threads you need an API key with the following permissions:

  • thread:assign
  • thread:read
import { PlainClient } from '@team-plain/typescript-sdk';

const client = new PlainClient({ apiKey: 'plainApiKey_xxx' });

const res = await client.assignThread({
  threadId: 'th_01H8H46YPB2S4MAJM382FG9423',
  userId: 'u_01FSVKMHFDHJ3H5XFM20EMCBQN',

  // You could instead assign to a machine user by doing:
  // machineUserId: 'XXX'
});

if (res.error) {
  console.error(res.error);
} else {
  console.log(`Thread assigned (${res.data.id})`);
}

Where res.data is the full thread:

{
  "__typename": "Thread",
  "id": "th_01H8H46YPB2S4MAJM382FG9423",
  "externalId": null,
  "customer": {
    "id": "c_01H8H46Y7N69VTSF4WXCJTVATZ"
  },
  "status": "TODO",
  "statusChangedAt": {
    "__typename": "DateTime",
    "iso8601": "2023-08-23T11:59:22.315Z",
    "unixTimestamp": "1692791962315"
  },
  "title": "Support request",
  "previewText": "Hello can you please...",
  "priority": 2,
  "labels": [],
  "assignedAt": {
    "__typename": "DateTime",
    "iso8601": "2023-10-05T13:12:12.050Z",
    "unixTimestamp": "1696511532050"
  },
  "assignedTo": {
    "__typename": "User",
    "id": "u_01FSVKMHFDHJ3H5XFM20EMCBQN",
    "fullName": "Matt Vagni",
    "publicName": "Matt",
    "email": "matt@plain.com",
    "updatedAt": {
      "__typename": "DateTime",
      "iso8601": "2023-05-16T12:36:52.266Z",
      "unixTimestamp": "1684240612266"
    }
  },
  "createdAt": {
    "__typename": "DateTime",
    "iso8601": "2023-08-23T11:59:22.315Z",
    "unixTimestamp": "1692791962315"
  },
  "createdBy": {
    "__typename": "MachineUserActor",
    "machineUserId": "mu_01H0J96X3KJ3C2S2W9T5S3CC1R"
  },
  "updatedAt": {
    "__typename": "DateTime",
    "iso8601": "2023-10-05T13:12:12.050Z",
    "unixTimestamp": "1696511532050"
  },
  "updatedBy": {
    "__typename": "MachineUserActor",
    "machineUserId": "mu_01HBZM0TZAKAZPJ47NX7X7XMDY"
  }
}

Unassigning threads

To unassign threads you need an API key with the following permissions:

  • thread:unassign
  • thread:read
import { PlainClient } from '@team-plain/typescript-sdk';

const client = new PlainClient({ apiKey: 'plainApiKey_xxx' });

const res = await client.unassignThread({
  threadId: 'th_01H8H46YPB2S4MAJM382FG9423',
});

if (res.error) {
  console.error(res.error);
} else {
  console.log(`Thread unassigned (${res.data.id})`);
}

Where res.data is the full thread like with assignment.