Trigger webhook on new file upload on Google Drive

Google Drive app on Make doesn’t offer an instant trigger on file uploads – so you either has to schedule your scenario and waste a lot of check runs or you do some other sourcery to trigger it on demand.

Well… I’ve searched and found out a way to use Google Apps Script to do this recurrent check on a Google Drive folder – so it fires a Make webhook that you can then use to start your scenario.

In order for this to work, go to script.google.com and create a new project.
Paste in this code:

// Replace 'myFolderID' with the actual ID of your folder and 'https://mysample.webhook.com' with your webhook URL
const folderId = 'myFolderID';
const webhookUrl = 'https://mysample.webhook.com';

const cache = CacheService.getScriptCache();

function isRecentlyCreated(file) {
  const now = new Date();
  const threshold = 60 * 1000; // Check for files created in the last minute (adjust as needed)
  return now - file.getLastUpdated() < threshold;
}

function checkForNewFiles() {
  const folder = DriveApp.getFolderById(folderId);
  const files = folder.getFiles();
  
  let newFilesFound = false;
  
  while (files.hasNext()) {
    const file = files.next();
    const fileId = file.getId();
    
    // Check cache for processed fileId
    if (!cache.get(fileId)) {
      if (isRecentlyCreated(file)) {
        const payload = { fileId };
        UrlFetchApp.fetch(webhookUrl, {
          method: 'post',
          contentType: 'application/json',
          payload: JSON.stringify(payload)
        });
        cache.put(fileId, 'processed', 60 * 60); // Cache processed fileId for 1 hour
        console.log(`New file found: ${fileId}`);
        newFilesFound = true;
      }
    } else {
      console.log(`File ${fileId} already processed`);
    }
  }
  
  if (!newFilesFound) {
    console.log('No new files found');
  }
}

function startMonitoring() {
  // Schedule a function to check for new files periodically (e.g., every minute)
  ScriptApp.newTrigger('checkForNewFiles')
      .timeBased()
      .everyMinutes(1)
      .create();
}

function main() {
  startMonitoring();
}


Also, click on the “Resources” section, on the left-side panel and add the Drive API.
Then, select the function “main” on the menu at the top and click “Execute” to run it. You’ll first will be prompted to authorize the script with your credentials, then click “Execute” again to create the trigger.

In order to see if this is working or not, go to the “Triggers” section (on the left-side panel) and check if there is a trigger created.
You can also see all the events executions in the “Executions” section.

Note: this will check for the file modification date – so it works for files recently created or modified – so take that in mind.

Hope it helps!

4 Likes

Thanks Hugo,
This have been a game changer :slight_smile: Thanks a lot.
Does google charge for checking the files. If it is I’ll change the time from every 5min to longer.
Thanks again. Great post.

1 Like

No, but there are limits, although you are unlikely to hit them with just a single script.

2 Likes

@samliew already got it – so there are limits.
The code is with 1 minute interval, I hardly think you need this much checking (I’ve set it with 1 minute only to test, now I’m using with 10 minute interval).

In any case, the idea here is to save some operations, and this will definitely do the trick!
Glad it helped!

1 Like

This is amazing thank you soooooo much for sharing!!

Where is this section “Then, select the function “main” on the menu at the top and click “Execute” to run it. You’ll first will be prompted to authorize the script with your credentials, then click “Execute” again to create the trigger.” ? I don’t seem to have this menu?

EDIT - I think I found it. This is amazing thank you! :slight_smile:

2 Likes

Glad you found, but just in case anyone else need it… it’s at the top of the Google Apps Script interface. Is just to start the function, in order to be prompted to allow the script to run – Google always need this authorization first.

Is this script return parent folders also ?

It’s getting files only for the folder ID you enter, not the parents.
I believe it will return files for children folders – but haven’t tested for that.

1 Like