Skip to main content
Docs

Users

To get started, it's important to first understand Clerk's .

The User object holds all of the information for a single user of your application and provides a set of methods to manage their account. Each User has at least one authentication identifier, which might be their email address, phone number, or a username.

A user can be contacted at their primary email address or primary phone number. They can have more than one registered email address, but only one of them will be their primary email address (User.primaryEmailAddress). This goes for phone numbers as well; a user can have more than one, but only one phone number will be their primary (User.primaryPhoneNumber). At the same time, a user can also have one or more external accounts by connecting to social providers such as Google, Apple, Facebook, and many more (User.externalAccounts).

Finally, a User object holds profile data like the user's name, profile picture, and a set of metadata that can be used internally to store arbitrary information. The metadata are split into publicMetadata and privateMetadata. Both types are set from the Backend API, but public metadata can also be accessed from the Frontend API.

For more information on the User object, such as helper methods for retrieving and updating user information and authentication status, see the . The User object is also available in the backend, but it looks slightly different. For more information, see the .

Manage users

You can manage your users in the Clerk Dashboard, or programmatically.

In the Clerk Dashboard

To manage users in the Clerk Dashboard, navigate to the Users page.

Programmatically

You can manage users programmatically through the frontend or backend.

In the frontend

Depending on the level of abstraction you need, you can manage users in the frontend using Clerk's prebuilt components, React hooks, or lower-level JavaScript methods.

  • Prebuilt components: Clerk provides the prebuilt components <UserButton /> and <UserProfile /> to help your users manage their profile data.
  • Hooks: Because Clerk's React-based SDKs are built on top of the Clerk React SDK, you can use the that the React SDK provides. These hooks include access to the User object and helpful methods for managing user authentication and profile data.
  • JavaScript methods: If Clerk's prebuilt components don't meet your specific needs or if you require more control over the logic, you can rebuild the existing Clerk flows using the Clerk API. For more information, see the custom flow guides.

In the backend

The is a wrapper around the Backend API that makes it easier to interact with the API. It includes many methods for managing users, such as getUser(), createUser(), and deleteUser(). For more information, see the .

Create users

You can create users either in the Clerk Dashboard or programmatically.

In the Clerk Dashboard

To create a user in the Clerk Dashboard, navigate to the Users page and select Create user.

Programmatically

To create a user programmatically, you can either or use the method as shown in the following example.

app/api/example/route.ts
import { auth, clerkClient } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'

export async function GET() {
  try {
    const client = await clerkClient()
    const user = await client.users.createUser({
      emailAddress: ['test@example.com'],
      password: 'password',
    })
    return NextResponse.json({ message: 'User created', user })
  } catch (error) {
    console.log(error)
    return NextResponse.json({ error: 'Error creating user' })
  }
}
src/api/example.ts
import type { APIRoute } from 'astro'
import { clerkClient } from '@clerk/astro/server'

export const GET: APIRoute = async (context) => {
  await clerkClient(context).users.createUser({
    emailAddress: ['test@example.com'],
    password: 'password',
  })

  return new Response(JSON.stringify({ success: true }), { status: 200 })
}
public.ts
import { getAuth, clerkClient } from '@clerk/express'

app.post('/createUser', async (req, res) => {
  await clerkClient.users.createUser({
    emailAddress: ['test@example.com'],
    password: 'password',
  })

  res.status(200).json({ success: true })
})
app/routes/example.tsx
import { clerkClient, getAuth } from '@clerk/react-router/server'
import type { Route } from './+types/example'

export async function loader(args: Route.LoaderArgs) {
  await clerkClient.users.createUser({
    emailAddress: ['test@example.com'],
    password: 'password',
  })

  return { success: true }
}
app/routes/api/example.tsx
import { json } from '@tanstack/react-start'
import { createFileRoute } from '@tanstack/react-router'
import { clerkClient } from '@clerk/tanstack-react-start/server'

export const ServerRoute = createFileRoute('/api/example')({
  server: {
    handlers: {
      GET: async () => {
        await clerkClient().users.createUser({
          emailAddress: ['test@example.com'],
          password: 'my-secure-password',
        })

        return json({ success: true })
      },
    },
  },
})

Delete users

You can delete users either in the Clerk Dashboard or programmatically.

In the Clerk Dashboard

To delete a user in the Clerk Dashboard, navigate to the Users page. You can either select the user and then in the side navigation menu, select Delete user, or select the menu icon on the right side of the user's row and select Delete user.

Programmatically

To delete a user programmatically, you can either or use the method as shown in the following example.

app/api/example/route.ts
import { auth, clerkClient } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'

export async function GET() {
  const { userId } = await auth()

  await clerkClient.users.deleteUser(userId)

  return NextResponse.json({ success: true })
}
src/api/example.ts
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.deleteUser(userId)

  return new Response(JSON.stringify({ success: true }), { status: 200 })
}
public.ts
import { getAuth, clerkClient } from '@clerk/express'

app.post('/deleteUser', async (req, res) => {
  const { userId } = getAuth(req)

  await clerkClient.users.deleteUser(userId)

  res.status(200).json({ success: true })
})
app/routes/example.tsx
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.deleteUser(userId)

  return { success: true }
}
app/routes/api/example.tsx
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.deleteUser(userId)

        return json({ success: true })
      },
    },
  },
})

Feedback

What did you think of this content?

Last updated on