Skip to main content

Manage rooms

This example demonstrates how to manage rooms in ONLYOFFICE DocSpace using the API. It covers creating, retrieving, renaming, archiving, and deleting rooms through API requests.

Before you start

  1. Replace https://yourportal.onlyoffice.com and YOUR_API_KEY with your actual DocSpace portal URL and API key. Ensure you have the necessary data and permissions to perform migration operations.
  2. Before you can make requests to the API, you need to authenticate. Check out the Personal access tokens page to learn how to obtain and use access tokens.
Full example
// Set API base URL
const BASE_URL = 'https://yourportal.onlyoffice.com';
const API_KEY = 'YOUR_API_KEY';

// Headers with API key for authentication
const HEADERS = {
Authorization: API_KEY, // если используете PAT, Bearer не нужен
// Authorization: `Bearer ${API_KEY}`, // вариант, если токен JWT
'Content-Type': 'application/json',
};

// Step 1: Create a room
async function createRoom(roomName, description) {
const url = `${BASE_URL}/api/2.0/files/rooms`; // исправлен путь
const res = await fetch(url, {
method: 'POST',
headers: HEADERS,
body: JSON.stringify({ title: roomName, description, roomType: 2 }),
});
if (!res.ok) {
const t = await res.text();
console.log(`Create room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
const json = await res.json();
return json.response;
}

// Step 2: Retrieve room details
async function getRoomDetails(roomId) {
const url = `${BASE_URL}/api/2.0/files/rooms/${roomId}`;
const res = await fetch(url, { method: 'GET', headers: HEADERS });
if (!res.ok) {
const t = await res.text();
console.log(`Get room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
const roomInfo = await res.json();
return roomInfo.response;
}

// Step 3: Rename a room
async function renameRoom(roomId, newName) {
const url = `${BASE_URL}/api/2.0/files/rooms/${roomId}`;
const res = await fetch(url, {
method: 'PUT',
headers: HEADERS,
body: JSON.stringify({ title: newName }),
});
if (!res.ok) {
const t = await res.text();
console.log(`Rename room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
}

// Step 4: Archive a room
async function archiveRoom(roomId) {
const url = `${BASE_URL}/api/2.0/files/rooms/${roomId}/archive`;
const res = await fetch(url, { method: 'PUT', headers: HEADERS });
if (!res.ok) {
const t = await res.text();
console.log(`Archive room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
}

// Step 5: Delete a room
async function deleteRoom(roomId) {
const url = `${BASE_URL}/api/2.0/files/rooms/${roomId}`;
const res = await fetch(url, { method: 'DELETE', headers: HEADERS });
if (!res.ok) {
const t = await res.text();
console.log(`Delete room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
}

// Run (пример)
(async () => {
const room_name = 'New Room'; // Replace with actual room name
const description = 'This is a test room.'; // Replace with actual room description
const new_room_name = 'Updated Room Name'; // Replace with actual new room name
const room_id = 1234 // Replace with actual room ID

try {
// Step 1
await createRoom(room_name, description);

// Step 2
await getRoomDetails(room_id); // вставьте актуальный ID

// Step 3
await renameRoom(room_id, new_room_name);

// Step 4
await archiveRoom(room_id);

// Step 5
await deleteRoom(room_id);
} catch (e) {
console.error(e.message);
}
})();

Step 1: Create a room

A POST request is sent to /api/2.0/files/rooms to create a new room.

async function createRoom(roomName, description) {
const url = `${BASE_URL}/api/2.0/files/room`;
const res = await fetch(url, {
method: 'POST',
headers: HEADERS,
body: JSON.stringify({ title: roomName, description }),
});
if (!res.ok) {
const t = await res.text();
console.log(`Create room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
}

Step 2: Retrieve room details

A GET request is sent to /api/2.0/files/rooms/:id to get room information.

async function getRoomDetails(roomId) {
const url = `${BASE_URL}/api/2.0/files/room/${roomId}`;
const res = await fetch(url, { method: 'GET', headers: HEADERS });
if (!res.ok) {
const t = await res.text();
console.log(`Get room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
const roomInfo = await res.json();
return roomInfo;
}

Step 3: Rename a room

A PUT request is sent to /api/2.0/files/rooms/:id to rename a room.

async function renameRoom(roomId, newName) {
const url = `${BASE_URL}/api/2.0/files/room/${roomId}/rename`;
const res = await fetch(url, {
method: 'PUT',
headers: HEADERS,
body: JSON.stringify({ title: newName }),
});
if (!res.ok) {
const t = await res.text();
console.log(`Rename room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
}

Step 4: Archive a room

A PUT request is sent to /api/2.0/files/rooms/:id/archive to archive a room.

async function archiveRoom(roomId) {
const url = `${BASE_URL}/api/2.0/files/room/${roomId}/archive`;
const res = await fetch(url, { method: 'PUT', headers: HEADERS });
if (!res.ok) {
const t = await res.text();
console.log(`Archive room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
}

Step 5: Delete a room

A DELETE request is sent to /api/2.0/files/rooms/:id to remove a room.

async function deleteRoom(roomId) {
const url = `${BASE_URL}/api/2.0/files/room/${roomId}`;
const res = await fetch(url, { method: 'DELETE', headers: HEADERS });
if (!res.ok) {
const t = await res.text();
console.log(`Delete room failed. Status code: ${res.status}, Message: ${t}`);
return null;
}
}