Docs

updateUserMetadata()

Updates the metadata associated with the specified user by merging existing values with the provided parameters.

A "deep" merge will be performed - "deep" means that any nested JSON objects will be merged as well. You can remove metadata keys at any level by setting their value to null.

function updateUserMetadata(userId: string, params: UpdateUserMetadataParams): Promise<User>
  • Name
    userId
    Type
    string
    Description

    The ID of the user to update.

  • Name
    publicMetadata?
    Type
    UserPublicMetadata
    Description

    Metadata that can be read from the Frontend API and Backend API and can be set only from the Backend API.

  • Name
    privateMetadata?
    Type
    UserPrivateMetadata
    Description

    Metadata that can be read and set only from the Backend API.

In the following example, the returned User object has its publicMetadata property updated with the new metadata provided.

const userId = 'user_123'

const response = await clerkClient.users.updateUserMetadata(userId, {
  publicMetadata: {
    example: 'metadata',
  },
})

console.log(response)
/*
_User {
  id: 'user_123',
  passwordEnabled: true,
  totpEnabled: false,
  backupCodeEnabled: false,
  twoFactorEnabled: false,
  banned: false,
  locked: false,
  createdAt: 1708103362688,
  updatedAt: 1708103702285,
  imageUrl: 'https://img.clerk.com/eyJ...',
  hasImage: false,
  primaryEmailAddressId: 'idn_123',
  primaryPhoneNumberId: null,
  primaryWeb3WalletId: null,
  lastSignInAt: null,
  externalId: null,
  username: null,
  firstName: 'Test',
  lastName: 'Clerk',
  publicMetadata: { example: 'metadata' },
  privateMetadata: {},
  unsafeMetadata: {},
  emailAddresses: [
    _EmailAddress {
      id: 'idn_123',
      emailAddress: 'testclerk123@gmail.com',
      verification: [_Verification],
      linkedTo: []
    }
  ],
  phoneNumbers: [],
  web3Wallets: [],
  externalAccounts: [],
  lastActiveAt: null,
  createOrganizationEnabled: true
}
*/

Backend API (BAPI) endpoint

This method in the SDK is a wrapper around the BAPI endpoint PATCH/users/{user_id}/metadata. See the BAPI reference for more information.

Feedback

What did you think of this content?

Last updated on