Skip to Content
You are viewing a beta version of Clerk Docs
Visit the latest docs
Clerk logo

Clerk Docs

Ctrl + K
Go to clerk.com

createOrganizationMembership()

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

function createOrganizationMembership: (params: CreateOrganizationMembershipParams) => Promise<OrganizationMembership>;

CreateOrganizationMembershipParams

NameTypeDescription
organizationIdstringThe organization ID of the organization a user is being added to.
userIdstringThe user ID of the user being added to the organization.
rolestringThe role to assign the added user within the organization.

createOrganizationMembership() example

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

const organizationId = 'org_2ZUtbk2yvnFGItdeze1ivCh3uqh'; const userId = 'user_2VdArFEn2gfBqt8sRePmsVPyDHv'; const role = 'org:member'; const response = await clerkClient.organizations.createOrganizationMembership({ organizationId, userId, role }); console.log(response); /* _OrganizationMembership { id: 'orgmem_2b6TUmlDXlo3e9XPq3Wd9EyfIfj', role: 'org:member', publicMetadata: {}, privateMetadata: {}, createdAt: 1705534546701, updatedAt: 1705534546701, organization: _Organization { id: 'org_2ZUtbk2yvnFGItdeze1ivCh3uqh', name: 'Test', slug: 'test', imageUrl: 'https://img.clerk.com/eyJ0eXBlIjoiZGVmYXVsdCIsImlpZCI6Imluc18yVjdKRFdyclJwRmZFZTlqQUM2dWpSMG8xSlQiLCJyaWQiOiJvcmdfMlpVdGJrMnl2bkZHSXRkZXplMWl2Q2gzdXFoIiwiaW5pdGlhbHMiOiJUIn0', hasImage: false, createdBy: 'user_2V7JJKmoA9HqzHhfMqK5cpgLl56', createdAt: 1702488558853, updatedAt: 1705534260298, publicMetadata: {}, privateMetadata: {}, maxAllowedMemberships: 3, adminDeleteEnabled: true, members_count: undefined }, publicUserData: _OrganizationMembershipPublicUserData { identifier: 'testclerk123@gmail.com', firstName: 'test', lastName: 'account', imageUrl: 'https://img.clerk.com/eyJ0eXBlIjoicHJveHkiLCJzcmMiOiJodHRwczovL2ltYWdlcy5jbGVyay5kZXYvb2F1dGhfZ29vZ2xlL2ltZ18yVmRBckFNNWNyRUpxaGdmb3cydVBHZ29US2QifQ', hasImage: true, userId: 'user_2VdArFEn2gfBqt8sRePmsVPyDHv' } } */

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(opens in a new tab) for more details.

Last updated on December 19, 2023

What did you think of this content?

Clerk © 2024