Files
Fetch Files

Fetch files

Fetch files using API

ROQ Platform provides an API to fetch files from the ROQ Platform. The API is available at the generated SDK.

Fetch all files

We can use the files() API to fetch all files belonging to the current owner. For example, to fetch files in the Next.js application, you can use the following code:

import { roqBrowserClient } from "lib/roq/roq-client"
 
const fetchFiles = async () => {
    try {
        const dataList = await roqBrowserClient.roqPlatform.files();
    } catch (error) {
        console.error('Error fetching files:', error);
    }
};

The files API is live on the ROQ Platform, and it's guaranteed to be available. Whatever data model you use, you can always fetch files from the ROQ Platform.

How does the files API know who the current owner is? The owner is determined by the roqBrowserClient. It will get the current user token and use the token for every file request.

Fetch files with filters

The files API also accepts filters. For example, to fetch files with a file category equal to USER_FILES, you can use the following code:

import { roqBrowserClient } from "lib/roq/roq-client"
 
const fetchFilesWithFilters = async () => {
    try {
        const dataList = await roqBrowserClient.roqPlatform.files({
            filter: {
                fileCategory: {
                    equalTo: "USER_FILES"
                }
            }, limit: 20
        });
    } catch (error) {
        console.error('Error fetching files:', error);
    }
};

The code above will fetch files with a file category equal to USER_FILES and limit the result to 20 files. Please look here for more information about file category.

Fetch files using file associations

ROQ Platform provides a way to associate files with data models. For example, you can associate files with a project. To retrieve files linked with the project, you can filter files based on the entityName which is project:

const fetchFileAssociations = async () => {
    try {
        const dataList = await roqBrowserClient.roqPlatform.files({
            filter: {
                entityName: {
                    equalTo: "project"
                }
            }
        });
    } catch (error) {
        console.error('Error fetching file associations:', error);
    }
}

The code above will fetch files associated with the project and files belonging to the current owner. Please look here for more information about file API.