Docs

createOrganizationMembership()

Creates a membership to an organization for a user directly (circumventing the need for an invitation).

function createOrganizationMembership(
  params: CreateOrganizationMembershipParams,
): Promise<OrganizationMembership>
  • Name
    organizationId
    Type
    string
    Description

    The organization ID of the organization a user is being added to.

  • Name
    userId
    Type
    string
    Description

    The user ID of the user being added to the organization.

  • Name
    role
    Type
    string
    Description

    The role to assign the added user within the organization.

Example

In the following example, an OrganizationMembership is created for a user with the role org:member.

const organizationId = 'org_123'

const userId = 'user_123'

const role = 'org:member'

const response = await clerkClient.organizations.createOrganizationMembership({
  organizationId,
  userId,
  role,
})

console.log(response)
/*
_OrganizationMembership {
  id: 'orgmem_123',
  role: 'org:member',
  publicMetadata: {},
  privateMetadata: {},
  createdAt: 1705534546701,
  updatedAt: 1705534546701,
  organization: _Organization {
    id: 'org_123',
    name: 'TestOrg',
    slug: 'test-org',
    imageUrl: 'https://img.clerk.com/eyJ...',
    hasImage: false,
    createdBy: 'user_456',
    createdAt: 1702488558853,
    updatedAt: 1705534260298,
    publicMetadata: {},
    privateMetadata: {},
    maxAllowedMemberships: 3,
    adminDeleteEnabled: true,
    members_count: undefined
  },
  publicUserData: _OrganizationMembershipPublicUserData {
    identifier: 'testclerk123@gmail.com',
    firstName: 'Test',
    lastName: 'Clerk',
    imageUrl: 'https://img.clerk.com/eyJ...',
    hasImage: true,
    userId: 'user_123'
  }
}
*/

Backend API (BAPI) endpoint

This method in the SDK is a wrapper around the BAPI endpoint POST/organizations/{organization_id}/memberships. See the BAPI reference for more details.

Feedback

What did you think of this content?

Last updated on