# Change user roles

Source: https://docs.musicnerd.net/api-reference/admin/set-user-roles

Change user roles

## PATCH /api/admin/users

Full OpenAPI specification: https://docs.musicnerd.net/spec/admin.json

## Authentication

This operation requires one of the security alternatives in the specification below. Security scheme definitions are included where present in the published specification.

[Authentication guide](https://docs.musicnerd.net/authentication)

## Operation and referenced schemas

```json
{
  "openapi": "3.1.0",
  "info": {
    "title": "Music Nerd API: admin",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://musicnerd-api.vercel.app",
      "description": "Production"
    },
    {
      "url": "https://musicnerd-api-staging.vercel.app",
      "description": "Staging (staging database)"
    }
  ],
  "paths": {
    "/api/admin/users": {
      "patch": {
        "summary": "Change user roles",
        "operationId": "setAdminUserRole",
        "security": [
          {
            "adminBearer": []
          }
        ],
        "description": "Turns one role on or off for the given users and returns how many were updated. `whitelisted` marks a trusted contributor, `admin` grants the admin (turning it on also whitelists the user), and `hidden` keeps the user off leaderboards.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "userIds",
                  "role",
                  "enabled"
                ],
                "properties": {
                  "userIds": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                      "type": "string",
                      "format": "uuid"
                    },
                    "description": "The users to change, from [Search users](/api-reference/admin/search-users) or the admin's user list."
                  },
                  "role": {
                    "type": "string",
                    "enum": [
                      "whitelisted",
                      "admin",
                      "hidden"
                    ]
                  },
                  "enabled": {
                    "type": "boolean",
                    "description": "`true` to grant the role, `false` to remove it."
                  }
                }
              },
              "example": {
                "userIds": [
                  "0b1e4c2a-7f3d-4a58-9c61-2d8e5f7a9b10"
                ],
                "role": "whitelisted",
                "enabled": true
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The roles are changed. IDs with no user are ignored.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "status",
                    "updated"
                  ],
                  "properties": {
                    "status": {
                      "type": "string",
                      "enum": [
                        "ok"
                      ]
                    },
                    "updated": {
                      "type": "integer",
                      "minimum": 0,
                      "description": "How many users were updated."
                    }
                  }
                },
                "example": {
                  "status": "ok",
                  "updated": 1
                }
              }
            }
          },
          "400": {
            "description": "The body is not a list of user IDs, a role and `enabled`.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "status": "error",
                  "error": "Invalid role change"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/NotSignedIn"
          },
          "403": {
            "$ref": "#/components/responses/NotAdmin"
          },
          "500": {
            "$ref": "#/components/responses/InternalError"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "status",
          "error"
        ],
        "properties": {
          "status": {
            "type": "string",
            "enum": [
              "error"
            ]
          },
          "error": {
            "type": "string"
          }
        }
      }
    },
    "responses": {
      "NotSignedIn": {
        "description": "No token, an invalid or expired token, or no Music Nerd user for it.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "status": "error",
              "error": "Not signed in"
            }
          }
        }
      },
      "NotAdmin": {
        "description": "The user is not an admin.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "status": "error",
              "error": "Forbidden"
            }
          }
        }
      },
      "InternalError": {
        "description": "The database could not be read or written.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "status": "error",
              "error": "Internal server error"
            }
          }
        }
      }
    },
    "securitySchemes": {
      "adminBearer": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "Access token",
        "description": "A Music Nerd admin's access token. Copy it at https://musicnerd.net/access after signing in. A signed-in user who is not an admin gets 403."
      }
    }
  }
}
```
