Manage user photo
This example demonstrates how to upload and delete user profile photos in ONLYOFFICE DocSpace using API requests. Managing user profile pictures helps create a personalized and professional experience within the system.
Before you start
- Replace
https://yourportal.onlyoffice.comandYOUR_API_KEYwith your actual DocSpace portal URL and API key. Ensure you have the necessary data and permissions to perform migration operations. - 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
- Node.js
- Python
// 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 = {
Accept: 'application/json',
Authorization: `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
};
// Step 1: Upload user photo
async function uploadUserPhoto(userId, photoUrl) {
const url = `${API_HOST}/api/2.0/people/${userId}/photo`;
const res = await fetch(url, {
method: 'PUT',
headers: HEADERS,
body: JSON.stringify({ files: photoUrl }),
});
if (res.ok) {
console.log(`Failed to upload user photo: ${res.status} - ${text}`);
} else {
const text = await res.text();
console.log(`Failed to upload user photo: ${res.status} - ${text}`);
}
}
// Step 2: Delete user photo
async function deleteUserPhoto(userId) {
const url = `${API_HOST}/api/2.0/people/${userId}/photo`;
const res = await fetch(url, { method: 'DELETE', headers: HEADERS });
if (res.ok) {
console.log(`Failed to delete user photo: ${res.status} - ${text}`);
} else {
const text = await res.text();
console.log(`Failed to delete user photo: ${res.status} - ${text}`);
}
}
// Run
(async () => {
const user_id = '10001';
const photo_url = 'https://github.com/ONLYOFFICE/DocSpace-client/blob/master/public/appIcon-192.png?raw=true';
await uploadUserPhoto(user_id, photo_url);
await deleteUserPhoto(user_id);
})();
import requests
# Set API base URL
API_HOST = 'https://yourportal.onlyoffice.com'
API_KEY = 'your_api_key'
# Headers with API key for authentication
HEADERS = {
'Accept': 'application/json',
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
# Step 1: Upload user photo
def upload_user_photo(user_id, photo_url):
url = f'{API_HOST}/api/2.0/people/{user_id}/photo'
data = {"files": photo_url}
response = requests.put(url, json=data, headers=HEADERS)
if response.status_code == 200:
print(f'User photo uploaded successfully for {user_id}')
else:
print(f'Failed to upload user photo: {response.status_code} - {response.text}')
# Step 2: Delete user photo
def delete_user_photo(user_id):
url = f'{API_HOST}/api/2.0/people/{user_id}/photo'
response = requests.delete(url, headers=HEADERS)
if response.status_code == 200:
print(f'User photo deleted successfully for {user_id}')
else:
print(f'Failed to delete user photo: {response.status_code} - {response.text}')
if __name__ == "__main__":
user_id = "10001"
# Replace with your image URL
photo_url = "https://github.com/ONLYOFFICE/DocSpace-client/blob/master/public/appIcon-192.png?raw=true"
# Step 1: Upload a new user photo
upload_user_photo(user_id, photo_url)
# Step 2: Delete user photo
delete_user_photo(user_id)
Step 1: Upload a new photo
A PUT request is sent to /api/2.0/people/:userid/photo with:
user_id: The user ID.files: The URL to a new photo.
- Node.js
- Python
async function uploadUserPhoto(userId, photoUrl) {
const url = `${API_HOST}/api/2.0/people/${userId}/photo`;
const res = await fetch(url, {
method: 'PUT',
headers: HEADERS,
body: JSON.stringify({ files: photoUrl }),
});
if (res.ok) {
console.log(`Failed to upload user photo: ${res.status} - ${text}`);
} else {
const text = await res.text();
console.log(`Failed to upload user photo: ${res.status} - ${text}`);
}
}
def upload_user_photo(user_id, photo_url):
url = f'{API_HOST}/api/2.0/people/{user_id}/photo'
data = {"files": photo_url}
response = requests.put(url, json=data, headers=HEADERS)
if response.status_code == 200:
print(f'User photo uploaded successfully for {user_id}')
else:
print(f'Failed to upload user photo: {response.status_code} - {response.text}')
Step 2: Delete user photo
A DELETE request is sent to /api/2.0/people/:userid/photo with:
user_id: The user ID.
- Node.js
- Python
async function deleteUserPhoto(userId) {
const url = `${API_HOST}/api/2.0/people/${userId}/photo`;
const res = await fetch(url, { method: 'DELETE', headers: HEADERS });
if (res.ok) {
console.log(`User photo deleted successfully for ${userId}`);
} else {
const text = await res.text();
console.log(`Failed to delete user photo: ${res.status} - ${text}`);
}
}
def delete_user_photo(user_id):
url = f'{API_HOST}/api/2.0/people/{user_id}/photo'
response = requests.delete(url, headers=HEADERS)
if response.status_code == 200:
print(f'User photo deleted successfully for {user_id}')
else:
print(f'Failed to delete user photo: {response.status_code} - {response.text}')