Build a Free Cloudflare Traffic Alert Worker with a PHP Webhook
A sudden traffic spike can be good news—or a sign that a bot discovered your website and invited several thousand of its closest friends. This tutorial creates a small Cloudflare Worker that checks recent request volume every five minutes and calls a webhook on your own website when traffic exceeds a threshold.
It uses Cloudflare Workers, the Cloudflare GraphQL Analytics API, and a simple PHP receiver. A paid Cloudflare subscription is not required: Cloudflare lists httpRequestsAdaptiveGroups as an essential GraphQL analytics dataset available across plans, and the Workers Free plan includes Cron Triggers and sufficient limits for a lightweight scheduled monitor such as this one.
What you will build
The finished workflow looks like this:
- A Cloudflare Worker runs every five minutes.
- It asks Cloudflare Analytics for the number of HTTP requests during the preceding five minutes.
- If the request count is above your chosen threshold, it sends a signed JSON
POSTrequest to a URL you control. - Your PHP endpoint verifies the signature and performs the alert action you choose.
Cloudflare Cron Triggers invoke a Worker’s scheduled() handler, and cron schedules use UTC.

Prerequisites
- A Cloudflare account and an active zone: Add your domain to Cloudflare and ensure its traffic passes through Cloudflare.
- Node.js: Install the current LTS release from the official Node.js installer; Wrangler requires Node.js 16.17.0 or later.
- A terminal: PowerShell, Terminal, or a similar command-line application.
- A PHP-enabled website: Create a small PHP file at a private or difficult-to-guess URL on a site you control (we’ll create a simple example below).
1. Create the Worker project
Run the following commands in a terminal:
|
|
When prompted, choose a Worker-only project using JavaScript. If the setup tool did not install packages automatically, run:
|
|
Cloudflare’s project generator creates a Worker project with files including src/index.js and wrangler.jsonc.
2. Add the five-minute schedule
Open wrangler.jsonc and add the triggers section to the existing configuration. Do not replace the entire file unless you enjoy configuration archaeology.
{
"triggers": {
"crons": [
"*/5 * * * *"
]
},
"secrets": {
"required": [
"CLOUDFLARE_API_TOKEN",
"ALERT_WEBHOOK_SECRET"
]
}
}
The cron expression */5 * * * * means “run every five minutes.” Declaring required secrets also lets Wrangler stop a deployment if a required secret has not been configured.
3. Create a least-privilege Analytics API token
In the Cloudflare dashboard:
-
Open Profile → API Tokens.
-
Select Create Token.
-
Choose Custom token.
-
Add this permission:
1Account → Account Analytics → Read -
Under Zone Resources, include the specific zone you intend to monitor.
-
Create the token and copy it immediately (you will not be able to view it again).
Cloudflare recommends API Tokens rather than Global API Keys because tokens can be restricted to the required permissions and resources. For GraphQL Analytics access, Cloudflare’s current token guidance uses Account → Account Analytics → Read, with zone resources scoped to the relevant zone or zones.
4. Add Worker secrets
Store the API token in Cloudflare as a Worker secret:
|
|
Next, generate a long random webhook secret:
|
|
Copy the generated value, then save it as the second Worker secret:
|
|
Use the same webhook secret in the PHP endpoint later. Never place API tokens or shared secrets directly in src/index.js, wrangler.jsonc, or a Git repository.
5. Add the Worker code
Open src/index.js and replace its contents with the following code.
|
|
Update these three values (at the top of the file) after pasting the code:
ZONE_TAG: Your Cloudflare Zone ID.REQUEST_THRESHOLD: The number of requests that should trigger an alert.WEBHOOK_URL: Your PHP endpoint URL.
The Worker queries Cloudflare’s GraphQL endpoint using a Bearer token. The httpRequestsAdaptiveGroups dataset is the appropriate general-purpose choice here: it exposes request counts and is available as an essential dataset across Cloudflare plans.
6. Create the PHP webhook receiver
Create a PHP file on your website, such as:
|
|
Store the shared secret as a server environment variable named TRAFFIC_ALERT_SECRET. The value must exactly match the value saved in the Worker’s ALERT_WEBHOOK_SECRET.
Then add this PHP code:
|
|
The important part is hash_equals(): it compares the incoming shared-secret header safely before the endpoint trusts the request. A webhook URL alone is not authentication—URLs have a habit of eventually appearing somewhere they should not!
Update the comment block in the above code with your preferred alert action. For example, you could send an email, log to a database, or post to a chat service.
7. Deploy and monitor
Deploy the Worker:
|
|
Then monitor live logs:
|
|
Expected output below the threshold:
|
|
Expected output above the threshold:
|
|
Cron Trigger updates can take several minutes to propagate globally, so do not panic if the first scheduled invocation is not immediate.
Troubleshooting
“Zone does not have access to the path”
Confirm that the API token has Account Analytics → Read permission and that the intended zone is included in its Zone Resources scope. Also confirm that ZONE_TAG is the Zone ID for that same Cloudflare account.
“Too many zones requested”
Keep the zone query scoped exactly as shown:
|
|
Cloudflare applies limits to GraphQL scopes; zone-scoped queries can include up to 10 zones, but this monitor needs only one.
Too many repeated alerts
This basic version sends an alert every five minutes while traffic remains above the threshold. For production, consider adding a cooldown period—for example, one alert per hour—using Workers KV, a database, or a timestamp stored by your webhook receiver.
Conclusion
You now have a lightweight, free-plan-friendly traffic monitor that runs on Cloudflare’s network, checks recent HTTP request volume, and sends authenticated webhook alerts to your own website.
It is intentionally simple: change the threshold, replace the PHP logging action with your preferred notification method, and add cooldown logic if persistent traffic spikes would otherwise turn your inbox into a small, digital bonfire.
About BigProf Software
BigProf Software creates practical tools for building business applications faster. We are the creators of AppGini, a low-code application builder that helps developers create secure, database-driven web applications without starting every project from a blank page.