Skip to content
Buy 10+ on select items — save 10% auto-applied
Free US shipping on orders $35+
Order by 3pm ET — ships same-day from the US
Skip to main content

Google Cloud Scheduler + Node: Schedule endpoint calls | ShillehTek

October 23, 2023 156 views

Google Cloud Scheduler + Node: Schedule endpoint calls | ShillehTek
Project

Build a Google Cloud Scheduler cron job that posts to a Node App Engine endpoint to automate MongoDB cleanup on a schedule, with ShillehTek.

20 min Intermediate

Video Tutorial (Optional)

Watch first if you want to follow along while setting up Cloud Scheduler to call a Node endpoint on a schedule.

Project Overview

Google Cloud Scheduler + Node App: In this tutorial, you create a Google Cloud Scheduler (cron) job that hits a backend endpoint in your Node application (running on GCP App Engine) to automatically clear unverified MongoDB users with zero credits.

You can adapt the same pattern to schedule any periodic task your application needs.

  • Time: 20 to 40 minutes
  • Skill level: Intermediate
  • What you will build: A scheduled Cloud Scheduler job that sends a POST request to a Node API endpoint

Prerequisites: You should be able to deploy a Node application to GCP App Engine and be familiar with creating APIs in Node.

Parts List

From ShillehTek

  • None

External

  • Google Cloud Platform (GCP) App Engine
  • Google Cloud Scheduler
  • Node.js application with an API route
  • MongoDB database
  • Mongoose (MongoDB ODM for Node)
  • https://crontab.guru - help building cron expressions

Note: This example uses a shared secret sent in the JSON body to protect the endpoint. Replace the hardcoded value with a secure approach for production.

Step-by-Step Guide

Step 1 - Set up a backend endpoint in your Node application

Goal: Create a POST endpoint that Cloud Scheduler can call to run your cleanup logic.

What to do: Add a POST route (example: /clearUnverifiedUsers) to your Node app. The request checks a secret value from the JSON body, then deletes MongoDB users where verified = false and credits = 0.

Code:

app.post('/clearUnverifiedUsers', async (req, res) => {
  try {
    const secret = req.body.secret;
    if (secret !== 'password') {
      return res.status(403).json({ message: `Body ${secret}` });
    }
    
    const result = await signinTable.deleteMany({ verified: false, credits: 0 });
    return res.json({ message: 'Users cleared successfully', deletedCount: result.deletedCount });
  } catch (error) {
    return res.status(500).json({ error: 'Internal Server Error' });
  }
});

Depending on your purposes, you will likely want to change the code in this endpoint.

Expected result: Your deployed Node app has a working POST endpoint at /clearUnverifiedUsers that rejects invalid secrets and runs the database cleanup when authorized.

Step 2 - Create a cron job in GCP (Cloud Scheduler)

Goal: Schedule Cloud Scheduler to call your Node endpoint on a recurring schedule.

What to do: In GCP, go to App Engine, open the Cron Jobs tab, then select GO TO CLOUD SCHEDULER and click CREATE JOB.

Google Cloud App Engine Cron Jobs tab showing link to Cloud Scheduler
Navigate from App Engine Cron Jobs to Cloud Scheduler.
Google Cloud Console showing the Cron Jobs tab option
Open Cloud Scheduler from the App Engine Cron Jobs area.

Cloud Scheduler will ask you for job details. The key setting is the frequency, which uses a cron expression. If you need help building the expression, use https://crontab.guru.

Google Cloud Scheduler create job form showing schedule and request configuration fields
Enter the cron schedule and configure the HTTP request.

Configure the job to call your Node endpoint URL (your App Engine URL plus /clearUnverifiedUsers).

Set the request headers for Content-Type: application/json, and include a JSON payload body that contains the secret your endpoint expects.

Google Cloud Scheduler HTTP target configuration showing JSON body and headers
Add the URL extension and JSON payload for the secret.

Save the cron job.

If your job runs weekly (or any long interval), use Force Run from the Cloud Scheduler menu to verify it works right away. A successful call should show an execution status of Success.

Google Cloud Scheduler run history showing a successful execution status
Use Force Run to confirm the scheduler can reach your endpoint.

Expected result: Cloud Scheduler successfully sends a POST request to your Node endpoint on demand (Force Run) and on the schedule you configured.

Conclusion

You set up a Google Cloud Scheduler cron job that calls a POST endpoint in a Node application hosted on GCP App Engine. In this example, the endpoint checks a shared secret and then removes unverified MongoDB users with zero credits using Mongoose.

Want parts and tools for your next build? Shop at ShillehTek.com. If you want help designing or implementing a backend automation flow like this for your product, check out our IoT consulting services.

For more full stack tutorials, you can also subscribe here: Youtube. Support: https://www.buymeacoffee.com/mmshilleh.