Retrieving particular files from Google Drive folder using Node.js with known folder and file IDs

I’m working on a Node.js application where I need to fetch certain files from a Google Drive folder. I already have the folder ID and multiple file IDs that I want to retrieve.

I tried using this approach but it doesn’t return any results:

const result = await driveAPI.files.list({
    q: `'${folderIdentifier}' in parents and '${documentId}' in parents`
});

The query above returns nothing. I need to handle multiple document IDs in a single request. What’s the correct way to structure this query to get specific files from a folder when I know both the folder ID and the individual file IDs I’m looking for?

Any help with the proper syntax for handling multiple file identifiers would be appreciated.

The Drive API supports batch requests - way more efficient than Promise.all for multiple files. Use gapi.client.newBatch() to bundle your requests together. If you’re stuck with the list approach, here’s another option: use files.list() with just the folder filter first (q: "'${folderIdentifier}' in parents"), then filter client-side by your known file IDs. This cuts down API calls vs individual gets, especially if the folder doesn’t have tons of files. But honestly, batch requests are still your best bet for performance.

Your query syntax is incorrect; you are mistakenly treating file IDs as parent folders. Since you have the specific file IDs, there’s no need to filter by parent folder. Instead, you should use the file IDs directly. To handle multiple files, you will need to make separate requests for each file ID using driveAPI.files.get(). Unfortunately, the Drive API does not allow fetching multiple specific files by ID in a single request. A good approach is to batch the requests using Promise.all:

const filePromises = fileIds.map(fileId => 
    driveAPI.files.get({ fileId: fileId })
);
const results = await Promise.all(filePromises);

If verifying that files belong to a specific folder is necessary, you can perform that verification after retrieval, although it’s typically unnecessary if you trust the file IDs.

you can still use the folder filter, but your query structure’s wrong. for single files, try id='${documentId}' and '${folderIdentifier}' in parents. for multiple IDs, you’ll need an OR query: (id='file1' or id='file2') and 'folderId' in parents. but honestly, just go with Promise.all like Tom said - it’s cleaner and way more reliable.