---
title: Assign Users to Workspaces (and Groups)
slug: assign-users-to-workspaces-and-groups
docTags: 
createdAt: 2025-04-23T22:21:47.014Z
---

### API Endpoint Documentation: /api/users/workspace-assign

### Description:

This endpoint allows you to assign multiple users to one or more workspaces and groups by sending a POST request. The request body contains user IDs, workspace IDs, and group IDs to establish these assignments.

### Endpoint:

:::BlockQuote
POST https\://admin.\<your-site>.com/api/users/workspace-assign
:::

### Headers:

- **accept**: application/json Specifies that the request expects a JSON response.
- **authorization**: Bearer \<JWT Token> A JSON Web Token (JWT) that authenticates the request. Replace \<JWT Token> with a valid token.
- **content-type**: application/json Specifies that the request body is in JSON format.

### Request Body:

The request body should be in JSON format, containing the following fields:

- **workspaces**: array An array of workspace IDs to which the users will be assigned.
- **userIds**: array An array of user IDs to be assigned to the workspaces.
- **groups**: array (Optional) An array of group IDs to which the users will be assigned within the workspaces.

### Example Request Body:

:::BlockQuote
\{
&#x20; "workspaces": \["67fc8f559c24f8e146543697"],
&#x20; "userIds": \[
&#x20;   "6762c9a8dea46c90b1d924ec",&#x20;
&#x20;   "67a350d93e2da4d0491687f0",&#x20;
&#x20;   "6633bd2b946db532e13a7286"
&#x20; ],
&#x20; "groups": \[
&#x20;   "6804317449913c261b1b3c6f",&#x20;
&#x20;   "6806af86ee8f019a1266b9ed"
&#x20; ]
}
:::

### Method:

- **POST**: Sends a request to assign the specified users to the specified workspaces and groups.

### Example Request (Node.js):

:::BlockQuote
const fetch = require('node-fetch');

const url = 'https\://admin.\<your-site>.com/api/users/workspace-assign';
const options = \{
&#x20; method: 'POST',
&#x20; headers: \{
&#x20;   'accept': 'application/json',
&#x20;   'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
&#x20;   'content-type': 'application/json'
&#x20; },
&#x20; body: JSON.stringify(\{
&#x20;   workspaces: \["67fc8f559c24f8e146543697"],
&#x20;   userIds: \[
&#x20;     "6762c9a8dea46c90b1d924ec",&#x20;
&#x20;     "67a350d93e2da4d0491687f0",&#x20;
&#x20;     "6633bd2b946db532e13a7286"
&#x20;   ],
&#x20;   groups: \[
&#x20;     "6804317449913c261b1b3c6f",&#x20;
&#x20;     "6806af86ee8f019a1266b9ed"
&#x20;   ]
&#x20; })
};

fetch(url, options)
&#x20; .then(response => response.json())
&#x20; .then(data => console.log(data))
&#x20; .catch(error => console.error('Error:', error));
:::

### Response:

The API will return a JSON response indicating the success or failure of the operation.

### Success Response (200):

:::BlockQuote
\{
&#x20; "status": 200,
&#x20; "message": "Users successfully assigned to workspaces."
}
:::

### Error Response (400):

:::BlockQuote
\{
&#x20; "status": 400,
&#x20; "message": "Please select at least one user."
}
:::

Or:

:::BlockQuote
\{
&#x20; "status": 400,
&#x20; "message": "Please select a valid User."
}
:::

### Authentication:

This API requires a valid JWT token for authentication and admin privileges. Ensure that the token is included in the authorization header as Bearer \<JWT Token>.

### Usage Notes:

- This endpoint requires admin privileges to use.
- You can assign multiple users to multiple workspaces in a single request.
- The system will automatically update the user's type based on their permissions.
- If no users are selected, the request will fail with a 400 error.
- If an invalid user ID is provided, the request will fail with a 400 error.

