Add a member by principal ID
Use this guide to add a principal that has already signed in, when it can tell you its principal ID. It adds the principal in one call. To invite by an email address, invite a member by email; to hand over a token instead, invite a member by code. Workspaces explains members and their roles.
An owner or an admin adds. Only an owner adds an owner: an admin’s request with role: 'owner' answers forbidden.
Before you start
- The
apihelper that sends each request, from Platform quickstart. - Your credential,
token, as an owner or admin of the workspace,workspace, from Start a workspace. - The credential of the principal you add,
memberToken, such as the API key it got when it started its own workspace, from Start a workspace.
This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.
1. Get the principal’s ID
The principal you add reads its own ID with GET /v1/platform/me and its credential, then gives you the ID:
const { principal: member } = await api('GET', '/v1/platform/me', memberToken);
console.log(member.id.slice(0, 4)); // prn_
A principal that started its workspace with POST /v1/signup/start also has its ID in that answer’s principal.id.
2. Add the principal
Send POST /v1/platform/workspaces/{workspaceId}/members with the ID and the role: owner, admin, or member:
const { membership } = await api(
'POST',
`/v1/platform/workspaces/${workspace.id}/members`,
token,
{ principalId: member.id, role: 'admin' },
);
console.log(membership.role, membership.source); // admin direct_add
An ID that isn’t a principal answers not_found, and a principal that is already a member answers conflict.
Check it worked
List the workspace’s members with token:
const { members } = await api(
'GET',
`/v1/platform/workspaces/${workspace.id}/members`,
token,
);
console.log(members.map((m: { role: string }) => m.role)); // [ 'owner', 'admin' ]
Update a member changes a member’s role, and Remove a member takes it out, each under /v1/platform.