Skip to Content
Clerk logo

Clerk Docs

Ctrl + K
Go to clerkstage.dev

updateUser()

Updates a user with a given ID with attribute values provided in a params object.

The provided ID must be valid, otherwise an error will be thrown.

const userId = 'my-user-id'; const params = { firstName: 'John', lastName: 'Wick' }; const user = await clerkClient.users.updateUser(userId, params);

UpdateUserParams

NameTypeDescription
userIdstringThe ID of the user to update.
firstName?stringThe user's first name.
lastName?stringThe user's last name.
username?stringThe user's username.
password?stringThe plaintext password to give the user.
primaryEmailAddressID?stringEmail address that will replace user's current primary email address. Must be unique across your instance.
primaryPhoneNumberID?stringPhone number that will replace user's current primary phone number. Must be unique across your instance.
primaryWeb3WalletID?stringWeb3 wallet that will replace user's current primary web3 wallet. Must be unique across your instance.
externalId?stringAn external identifier for the user. Must be unique across your instance.
publicMetadata?Record<string, unknown>Metadata saved on the user, that is visible to both your Frontend and Backend APIs.
privateMetadata?Record<string, unknown>Metadata saved on the user that is only visible to your Backend API.
unsafeMetadata?Record<string, unknown>Metadata saved on the user, that can be updated from both the Frontend and Backend APIs. Note: Since this data can be modified from the frontend, it is not guaranteed to be safe.

Examples

app/page.[jsx/tsx]
import { NextRequest, NextResponse } from 'next/server'; import { getAuth, clerkClient } from '@clerk/nextjs/server'; // If you use `request` you don't need the type export async function POST(req: NextRequest) { // Get the user ID from the session const { userId } = getAuth(req); if (!userId) return NextResponse.redirect('/sign-in'); // The user attributes to update const params = { firstName: 'John', lastName: 'Wick' }; const updatedUser = await clerkClient.users.updateUser(userId, params); return NextResponse.json({ updatedUser }); }
pages/api/updateUser.[jsx/tsx]
import { clerkClient, getAuth } from '@clerk/nextjs/server'; import type { NextApiRequest, NextApiResponse } from 'next'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { // Get the user ID from the session const { userId } = getAuth(req); if (!userId) { return res.status(500).json({ error: "No valid user" }) } // The user attributes to update const params = { firstName: 'John', lastName: 'Wick' }; const updatedUser = await clerkClient.users.updateUser(userId, params); return res.status(200).json({ updatedUser }); }

What did you think of this content?

Clerk © 2024