Velo - trying to get list of files in media manager

Trying to use Velo to get a list of files in my Media Manager. Just using the code example from the Velo API reference (modified to log to console) and I’m just getting an empty error back and nothing else.

Using:

What am I doing wrong?

import { files } from "wix-media.v2";

$w.onReady(async function () {
   try {
     const result = await files.listFiles();
     console.log("result: " + result);

   } catch (error) {
     console.error(error);
   }
 });

Console:

Error: message: Unknown
details:
  applicationError:
    description: Unknown
    code: UNKNOWN
    data: {}

This seems to be an issue with the listFiles() endpoint. Please report to Contact Wix to get someone looking at this.

Done, thanks, anthony!

1 Like

Got some further clarity on this. It seems to be a permissions issues so you need to elevate() this function and call it from the backend then return that result to the frontend.

So basically you need to write a function something like the following in a file like backend/files.jsw:

import {files} from 'wix-media.v2';
import {elevate} from 'wix-auth';

export async function listFiles(parentFolderId = undefined) {
    try{
        const result =  await (elevate(files.listFiles)({
            paging:{limit:20},
        ...(parentFolderId && {parentFolderId})
        }));
        return result;
        
    }catch(e){
        console.error(e);
        return `got error \n ${e.message}`;
    }
}

Then call it from the frontend like:

import { listFiles } from "backend/files.jsw"
$w.onReady(async function () {
    let files = await listFiles();
    console.log(files);
});
1 Like

One additional note, the code above will list files for any user. If this is not what you want you’ll need to make sure your listFiles function checks the user has the appropriate roles first before returning listFiles() results via this function:

Thanks, Anthony. Looking at the API docs, this seems to be a universal module, not a backend module, so I thought I could use it from frontend code. I don’t see any specific indicators that the Files APIs can only be used from the backend. Am I missing something?