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.
Returns a User object.
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.
const userId = 'user_123'
const response = await clerkClient.users.updateUserMetadata(userId, {
publicMetadata: {
example: 'metadata',
},
})Example
This example updates the user's public metadata to include a birthday, but you can update the public, private, or unsafe metadata to include any information you want.
import { auth, clerkClient } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'
export async function GET() {
const { userId } = await auth()
const client = await clerkClient()
await client.users.updateUserMetadata(userId, {
publicMetadata: {
birthday: '1990-01-01',
},
})
return NextResponse.json({ success: true })
}import type { APIRoute } from 'astro'
import { clerkClient } from '@clerk/astro/server'
export const GET: APIRoute = async (context) => {
const { userId } = context.locals.auth()
await clerkClient(context).users.updateUserMetadata(userId, {
publicMetadata: {
birthday: '1990-01-01',
},
})
return new Response(JSON.stringify({ success: true }), { status: 200 })
}import { getAuth, clerkClient } from '@clerk/express'
app.post('/updateBirthday', async (req, res) => {
const { userId } = getAuth(req)
await clerkClient.users.updateUserMetadata(userId, {
publicMetadata: {
birthday: '1990-01-01',
},
})
res.status(200).json({ success: true })
})import { clerkClient, getAuth } from '@clerk/react-router/server'
import type { Route } from './+types/example'
export async function loader(args: Route.LoaderArgs) {
const { userId } = await getAuth(args)
await clerkClient.users.updateUserMetadata(userId, {
publicMetadata: {
birthday: '1990-01-01',
},
})
return { success: true }
}import { json } from '@tanstack/react-start'
import { createFileRoute } from '@tanstack/react-router'
import { auth, clerkClient } from '@clerk/tanstack-react-start/server'
export const ServerRoute = createFileRoute('/api/example')({
server: {
handlers: {
GET: async () => {
const { userId } = await auth()
await clerkClient().users.updateUserMetadata(userId, {
publicMetadata: {
birthday: '1990-01-01',
},
})
return json({ success: 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.
Here's an example of making a request directly to the endpoint using cURL.
curl -XPATCH -H 'Authorization: Bearer CLERK_SECRET_KEY' -H "Content-type: application/json" -d '{
"public_metadata": {
"birthday": "1990-01-01"
}
}' 'https://api.clerk.com/v1/users/{user_id}/metadata'Feedback
Last updated on