--- description: 'Learn how to manage users using the admin APIs. This includes listing, creating, updating, and deleting users.' addHowToData: true --- import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; # How to Manage Users In this document, you’ll learn how to manage users using the admin APIs. ## Overview You can use the user admin API to manage users and teams in the store. ### Scenario You want to add or use the following admin functionalities: - List users - Create a user - Update a user - Delete a user --- ## Prerequisites ### Medusa Components It is assumed that you already have a Medusa backend installed and set up. If not, you can follow the [quickstart guide](../../../development/backend/install.mdx) to get started. ### JS Client This guide includes code snippets to send requests to your Medusa backend using Medusa’s JS Client, among other methods. If you follow the JS Client code blocks, it’s assumed you already have [Medusa’s JS Client](../../../js-client/overview.mdx) installed and have [created an instance of the client](../../../js-client/overview.mdx#configuration). ### Medusa React This guide also includes code snippets to send requests to your Medusa backend using Medusa React, among other methods. If you follow the Medusa React code blocks, it's assumed you already have [Medusa React installed](../../../medusa-react/overview.mdx) and have [used MedusaProvider higher in your component tree](../../../medusa-react/overview.mdx#usage). ### Authenticated Admin User You must be an authenticated admin user before following along with the steps in the tutorial. You can learn more about [authenticating as an admin user in the API reference](https://docs.medusajs.com/api/admin#authentication). --- ## List Users You can retrieve users in a store by sending a request to the [List Users API Route](https://docs.medusajs.com/api/admin#users_getusers): ```ts medusa.admin.users.list() .then(({ users }) => { console.log(users.length) }) ``` ```tsx import { useAdminUsers } from "medusa-react" const Users = () => { const { users, isLoading } = useAdminUsers() return (
{isLoading && Loading...} {users && !users.length && No Users} {users && users.length > 0 && (
    {users.map((user) => (
  • {user.email}
  • ))}
)}
) } export default Users ```
```ts fetch(`/admin/users`, { credentials: "include", }) .then((response) => response.json()) .then(({ users }) => { console.log(users.length) }) ``` ```bash curl -L -X GET '/admin/users' \ -H 'x-medusa-access-token: ' ```
This API Route doesn't require any parameters. The request returns an array of user objects. --- ## Create a User You can create a user by sending a request to the [Create User API Route](https://docs.medusajs.com/api/admin#users_postusers): ```ts medusa.admin.users.create({ email: "user@example.com", password: "supersecret", }) .then(({ user }) => { console.log(user.id) }) ``` ```tsx import { useAdminCreateUser } from "medusa-react" const CreateUser = () => { const createUser = useAdminCreateUser() // ... const handleCreateUser = () => { createUser.mutate({ email: "user@example.com", password: "supersecret", }, { onSuccess: ({ user }) => { console.log(user.id) } }) } // ... } export default CreateUser ``` ```ts fetch(`/admin/users`, { credentials: "include", method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email: "user@example.com", password: "supersecret", }), }) .then((response) => response.json()) .then(({ user }) => { console.log(user.id) }) ``` ```bash curl -L -X POST '/admin/users' \ -H 'x-medusa-access-token: ' \ -H 'Content-Type: application/json' \ --data-raw '{ "email": "user@example.com", "password": "supersecret" }' ``` This API Route requires the following request body parameters: - `email`: a string indicating the email of the user. - `password`: a string indicating the password of the user. The API Route accepts other optional body parameters, such as first name or last name. Check the [API reference](https://docs.medusajs.com/api/admin#users_postusers) for details on optional body parameters. The request returns the created user as an object. --- ## Update User You can update a user’s details by sending a request to the [Update User API Route](https://docs.medusajs.com/api/admin#users_postusersuser): ```ts medusa.admin.users.update(userId, { first_name: "Marcellus", }) .then(({ user }) => { console.log(user.id) }) ``` ```tsx import { useAdminUpdateUser } from "medusa-react" type Props = { userId: string } const User = ({ userId }: Props) => { const updateUser = useAdminUpdateUser(userId) // ... const handleUpdateUser = ( firstName: string ) => { updateUser.mutate({ first_name: firstName, }, { onSuccess: ({ user }) => { console.log(user.first_name) } }) } // ... } export default User ``` ```ts fetch(`/admin/users/${userId}`, { credentials: "include", method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ first_name: "Marcellus", }), }) .then((response) => response.json()) .then(({ user }) => { console.log(user.id) }) ``` ```bash curl -L -X POST '/admin/users/' \ -H 'x-medusa-access-token: ' \ -H 'Content-Type: application/json' \ --data-raw '{ "first_name": "Marcellus" }' ``` This API Route requires the ID of the user as a path parameter. In the request body, you can pass any of the user’s fields that you want to update as parameters. In the example above, you update the user’s `first_name`. Check the [API reference](https://docs.medusajs.com/api/admin#users_postusersuser) for all the optional parameters. The request returns the updated user as an object. --- ## Delete a User You can delete a user by sending a request to the [Delete User API Route](https://docs.medusajs.com/api/admin#users_deleteusersuser): ```ts medusa.admin.users.delete(userId) .then(({ id, object, deleted }) => { console.log(id) }) ``` ```tsx import { useAdminDeleteUser } from "medusa-react" type Props = { userId: string } const User = ({ userId }: Props) => { const deleteUser = useAdminDeleteUser(userId) // ... const handleDeleteUser = () => { deleteUser.mutate(void 0, { onSuccess: ({ id, object, deleted }) => { console.log(id) } }) } // ... } export default User ``` ```ts fetch(`/admin/users/${userId}`, { credentials: "include", method: "DELETE", }) .then((response) => response.json()) .then(({ id, object, deleted }) => { console.log(id) }) ``` ```bash curl -L -X DELETE '/admin/users/' \ -H 'x-medusa-access-token: ' ``` This API Route requires the user ID as a path parameter. It deletes the user and returns the following fields: - `id`: The ID of the deleted user. - `object`: The type of object that was deleted. In this case, the value will be `user`. - `deleted`: A boolean value indicating whether the user was deleted. --- ## See Also - [How to manage a user’s profile](./manage-profile.mdx)