Vercel Serverless Function Authentication Issue: 401 Unauthorized Despite Valid CRON_SECRET
Issue Description
I'm experiencing a persistent 401 Unauthorized error with my serverless functions, specifically in a cron job workflow. My setup includes:
-
A cron-triggered endpoint
/api/cron-process-notifications.jsthat successfully authenticates -
This endpoint then calls another endpoint
/api/notification-triggerusing axios -
Both endpoints use the same
CRON_SECRETenvironment variable for authentication -
The token is being correctly passed in the Authorization header
What I've Confirmed
-
Environment Variables: The same
CRON_SECRETvalue is set identically across ALL environments -
Token Freshness: I generated a brand new CRON_SECRET just yesterday
-
Code Review: The authorization headers are properly formatted in the requests
-
Deployment: I've redeployed the application after updating the secret
Error Details
The cron job successfully triggers my first endpoint, but when that endpoint tries to call my second endpoint, I get this error:
[CRON] Error in scheduled notification processing: AxiosError: Request failed with status code 401
The logs show that the Authorization header is being sent correctly:
Authorization: 'Bearer xxxx'
The Confusing Part: Support's Response
Support suggested the token might be "invalid or expired" and that I should:
-
Check if the token is hardcoded (it's not, it uses process.env.CRON_SECRET)
-
Check if the endpoint uses a different auth mechanism (it doesn't, the code clearly shows it checks for the CRON_SECRET)
-
Regenerate the token (I already did this yesterday)
What doesn't make sense is:
-
The token cannot be "expired" since it's a simple string, not a JWT with expiration
-
The code explicitly checks for the exact CRON_SECRET value: `const authHeader = req.headers.authorization; const cronSecret = process.env.CRON_SECRET;
if (cronSecret && (!authHeader || authHeader !== Bearer ${cronSecret})) {
console.warn('[CRON] Authorization failed...');
return res.status(401).json({ error: 'Unauthorized' });
}`
- The same token works for the first endpoint but not the second, despite identical authentication code
Code Flow
-
Cron triggers
/api/cron-process-notifications.js -
This validates
Bearer ${process.env.CRON_SECRET}✅ Success -
It then calls
/api/notification-triggerpassing the same token:const response = await axios.get(${baseUrl}/api/notification-trigger, { headers: { Authorization:Bearer ${process.env.CRON_SECRET || ''}} }); -
This second endpoint returns 401 Unauthorized ❌ Failure

Question
-
What could cause a 401 when the exact same token successfully authenticates against one endpoint but fails on another?
-
Does vercel have a CRON_SECRET outside of the enviromental variables at the project level for?