Nextcloud Node.js Client

1.8.1 · active · verified Tue Apr 21

The `nextcloud-node-client` is a TypeScript-first library offering a comprehensive API to interact with Nextcloud servers from Node.js applications. Currently at version 1.8.1, it demonstrates an active development cadence with regular minor releases introducing new features like recursive file retrieval, user management, and improved sharing capabilities. The library differentiates itself by providing a rich, simple API that abstracts away the underlying Nextcloud communication protocols, supporting essential operations such as uploading and downloading files, managing folder structures, handling user accounts, creating and managing shares, and applying tags and comments to filesystem elements. A key feature is its flexible credential management, allowing configuration via environment variables (NEXTCLOUD_USERNAME, NEXTCLOUD_PASSWORD, NEXTCLOUD_URL) or Cloud Foundry's `VCAP_SERVICES`, and strongly recommending the use of Nextcloud's app-specific passwords for enhanced security. This client aims to streamline automation tasks for Nextcloud instances.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates creating a Nextcloud client, managing folders and files, adding tags, retrieving file content, and creating password-protected public shares, followed by cleanup. It uses environment variables for authentication.

import Client, { File, Folder, Share } from 'nextcloud-node-client';

// Ensure these environment variables are set:
// process.env.NEXTCLOUD_USERNAME
// process.env.NEXTCLOUD_PASSWORD
// process.env.NEXTCLOUD_URL (e.g., 'https://your.nextcloud.host')

(async () => {
    try {
        // Create a new client. Credentials are automatically picked up from environment variables.
        const client = new Client();

        // Create a folder structure if it doesn't exist
        const folder: Folder = await client.createFolder('my-test-folder/subfolder');
        console.log(`Created folder: ${folder.name}`);

        // Create a file within the folder
        const fileContent = Buffer.from('Hello, Nextcloud from Node.js!');
        const file: File = await folder.createFile('hello.txt', fileContent);
        console.log(`Created file: ${file.name}`);

        // Add a tag to the file (the tag will be created if it doesn't exist)
        await file.addTag('nodejs-api-test');
        console.log('Added tag to file.');

        // Get the content of the created file
        const downloadedContent: Buffer = await file.getContent();
        console.log(`Downloaded file content: ${downloadedContent.toString()}`);

        // Share the file publicly with a password and note
        const share: Share = await client.createShare({
            fileSystemElement: file,
            password: 'my-secure-password',
            note: 'Shared via nextcloud-node-client example.'
        });
        console.log(`File shared publicly. Share link: ${share.url}`);
        console.log(`Share password: ${share.password}`);

        // Clean up: Delete the folder, which also deletes the file and share within it
        await folder.delete();
        console.log(`Deleted folder: ${folder.name}`);

    } catch (e) {
        console.error('An error occurred:', e);
        // More robust error handling might include checking error types or messages
        if (e instanceof Error) {
            console.error(e.message);
        }
    }
})();

view raw JSON →