File Management
Tutorials
How to Fetch Files Using API

How to Fetch File Using API

After you upload a file, you can access them using the files() API. This query is equipped with filters, which allow you to find files by their associated objects or to fetch files by category.

Super Admin User

For example, how to get all the files as a Super Admin user:

Make sure you create .env file for ROQ credentials and use dotenv package to load them into the Node.js project. You can get apiKey, environmentId, and host from ROQ Console.

import 'dotenv/config'
import { Platform } from '@roq/nodejs'
 
/**
 * Connect to the ROQ Platform
 */
const client = new Platform({
	apiKey: process.env.ROQ_API_KEY,
	environmentId: process.env.ROQ_ENVIRONMENT_ID,
	host: process.env.ROQ_PLATFORM_URL
})
 
/**
 * Get all client files
 */
const filesResponse = await client.asSuperAdmin().files({
	filter: {
		fileCategory: {
			equalTo: "USER_FILES"
		}
	}, limit: 20
});
 
const filesArray = filesResponse?.files.data
 
filesArray.forEach(element => {
	console.log(element)
});

The result filesArray is an array contain the list of files (maximum 20) with the category equal to USER_FILES. Depends on the project, for example, this is the file data for each item in array:

[
  {
	id: '97e82506-2a0b-1135-9d99-164b1069f8ad',
	createdAt: '2023-07-19T05:25:38.501Z',
	updatedAt: '2023-07-19T05:25:52.067Z',
	contentType: 'video/mp4',
	createdByUserId: '7877d2d0-dea7-473e-a158-57eca3123906',
	fileCategoryId: 'eaea78c9-2a0d-402f-b666-b97a1adc7190',
	isPublic: true,
	name: 'sea.mp4',
	status: 'ready',
	url: 'https://s3.fr-par.scw.cloud/1e4eba76-328d-4cf0-b478-41076a6b83b3-environment-bucket/sea.mp4'
  }
]

User

To fetch files as user is very easy. We use function asUser(userId) instead of asSuperAdmin():

import 'dotenv/config'
import { Platform } from '@roq/nodejs'
 
/**
 * Connect to the ROQ Platform
 */
const client = new Platform({
	apiKey: process.env.ROQ_API_KEY,
	environmentId: process.env.ROQ_ENVIRONMENT_ID,
	host: process.env.ROQ_PLATFORM_URL
})
 
// Get all users data
const users = await client.asSuperAdmin().users()
const usersData = users?.users.data
 
/** 
 * Get the file as user
 */
const userId = "7877d2d0-dea7-473e-a158-57eca3123906"
const myFile = await client.asUser(userId).files({ limit: 5 })
 
console.log(myFile.files.data);

The users will return all the users data available in application and from this data we can get the user id

{
  users: {
    data: [
		{
			id: '7877d2d0-dea7-473e-a158-57eca3123906',
			reference: '7877d2d0-dea7-473e-a158-57eca3123906',
			firstName: 'Adam',
			lastName: 'Smith',
			active: true,
			email: 'aitesta100@gmail.com',
			phone: null,
			locale: 'en-US',
			isOptedIn: true,
			synced: false,
			tenantId: '62814d5d-377f-4980-83fc-863cd8dd314c',
			customData: {},
			timezone: 'Asia/Jakarta',
			avatarUrl: 'https://s3.fr-par.scw.cloud/1e4eba76-328d-4cf0-b478-41076a6b83b3-environment-bucket/photo_from_the_fro_0.jpg',
			createdAt: '2023-07-07T02:43:46.213Z',
			updatedAt: '2023-07-07T03:06:47.669Z'
  		},
  		{...},
  		{...}
  	],
    totalCount: 5
  }
}

and then we can use the user id id: '7877d2d0-dea7-473e-a158-57eca3123906' as a reference to get the files:

/** 
 * Get the file as user
 */
const userId = "7877d2d0-dea7-473e-a158-57eca3123906"
const myFile = await client.asUser(userId).files({ limit: 5 })