Skip to main content

Overview

Admin endpoints for user management, including listing all users and updating user roles.
All endpoints on this page require admin role authentication via the requireAdmin middleware.

List Users

Retrieve a paginated list of all users in the system.

Query Parameters

page
number
default:"1"
Page number for pagination
limit
number
default:"10"
Number of users per page

Response

success
boolean
required
Indicates if the request was successful
data
array
required
Array of user objects
pagination
object
required
Pagination metadata
{
  "success": true,
  "data": [
    {
      "id": "user_123abc",
      "email": "[email protected]",
      "name": "John Doe",
      "role": "user",
      "createdAt": "2024-01-15T10:30:00.000Z"
    },
    {
      "id": "user_456def",
      "email": "[email protected]",
      "name": "Admin User",
      "role": "admin",
      "createdAt": "2024-01-10T08:00:00.000Z"
    }
  ],
  "pagination": {
    "currentPage": 1,
    "totalPages": 5,
    "totalItems": 50,
    "limit": 10
  }
}

Update User Role

Update a user’s role. Admins can promote users to admin or demote admins to regular users.

Path Parameters

id
string
required
User ID to update

Request Body

role
string
required
New role for the user. Must be either "user" or "admin"

Response

success
boolean
required
Indicates if the role was updated successfully
message
string
required
Success or error message
{
  "success": true,
  "message": "User role updated successfully"
}

Request Example

cURL
curl -X PUT https://api.example.com/api/admin/users/user_123abc/role \
  -H "Content-Type: application/json" \
  -H "Cookie: session=..." \
  -d '{
    "role": "admin"
  }'
JavaScript
const response = await fetch('/api/admin/users/user_123abc/role', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
  },
  credentials: 'include',
  body: JSON.stringify({
    role: 'admin'
  })
});

const data = await response.json();

Validation Rules

  • Role must be either "user" or "admin"
  • User ID must exist in the database
  • Admins cannot change their own role (prevents accidental lockout)
  • Returns 400 for validation errors
  • Returns 404 if user not found
  • Returns 500 for server errors

Build docs developers (and LLMs) love