Skip to main content

Restore file to previous version

This example demonstrates how to retrieve the edit history of a file in ONLYOFFICE DocSpace and restore it to a specific previous version using the API.

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 API_HOST = 'https://yourportal.onlyoffice.com';
const API_KEY = 'your_api_key';

// Headers with API key for authentication
const HEADERS = {
Authorization: `Bearer ${API_KEY}`,
};

// Step 1: Get file version history
async function getFileVersions(fileId) {
const url = `${API_HOST}/api/2.0/files/file/${fileId}/edit/history`;
const res = await fetch(url, { method: 'GET', headers: HEADERS });

if (!res.ok) {
const text = await res.text();
console.log(`File versions retrieval failed. Status code: ${res.status}, Message: ${text}`);
return [];
}

const data = await res.json();
const versions = data?.response ?? [];
console.log('Available versions:');
for (const v of versions) {
console.log(`- Version ${v.version} created at ${v.created}`);
}
return versions;
}

// Step 2: Restore file to specific version
async function restoreFileVersion(fileId, versionNumber) {
const params = new URLSearchParams({ version: String(versionNumber) });
const url = `${API_HOST}/api/2.0/files/file/${fileId}/restoreversion?${params}`;
const res = await fetch(url, { method: 'GET', headers: HEADERS });

if (res.ok) {
console.log(`File restored to version ${versionNumber}`);
} else {
const text = await res.text();
console.log(`Restore to version ${versionNumber} failed. Status code: ${res.status}, Message: ${text}`);
}
}

// Run the flow
(async () => {
const file_id = 1569862; // Replace with your file ID
const versions = await getFileVersions(file_id);

if (versions && versions.length > 1) {
const target_version = versions[0].version;
await restoreFileVersion(file_id, target_version);
} else {
console.log('No previous versions available for restore.');
}
})();

Step 1: Get file version history

A GET request is sent to /api/2.0/files/file/:fileId/edit/history.

Returns a list of all saved versions with version and created timestamps.

async function getFileVersions(fileId) {
const url = `${API_HOST}/api/2.0/files/file/${fileId}/edit/history`;
const res = await fetch(url, { method: 'GET', headers: HEADERS });

if (!res.ok) {
const text = await res.text();
console.log(`File versions retrieval failed. Status code: ${res.status}, Message: ${text}`);
return [];
}

const data = await res.json();
const versions = data?.response ?? [];
console.log('Available versions:');
for (const v of versions) {
console.log(`- Version ${v.version} created at ${v.created}`);
}
return versions;
}

Step 2: Restore to specific version

A GET request is sent to /api/2.0/files/file/:fileId/restoreversion.

This method requires the following query parameter:

  • version. The file version number to restore.
async function restoreFileVersion(fileId, versionNumber) {
const params = new URLSearchParams({ version: String(versionNumber) });
const url = `${API_HOST}/api/2.0/files/file/${fileId}/restoreversion?${params}`;
const res = await fetch(url, { method: 'GET', headers: HEADERS });

if (res.ok) {
console.log(`File restored to version ${versionNumber}`);
} else {
const text = await res.text();
console.log(`Restore to version ${versionNumber} failed. Status code: ${res.status}, Message: ${text}`);
}
}