vercel-killswitch v1.0.0-beta
Vercel Killswitch
Why
We've all heard AWS horror stories, you wake up one day and see a $XX,XXX bill, I rather not, thanks.
Vercel recently anounced Spend Management which is a great start but they give you this warning:
Setting a spend amount does not automatically stop usage. If you want to pause your project at a certain amount, you must configure this through a webhook .
And while they Promise()
Next, we're working on powerful anomaly detection for your spend to proactively alert you when spikes happen, rather than manually adding spend amounts. Stay tuned for more product updates to help you have confidence in your usage on Vercel.
I wanted to have a solution now, so we can actually ship on Fridays 😜
What
Creates an /api/pause webhook in your Next.js App router Vercel hosted site:
https://your-project-url.whatever/api/pause
If it receives the (spend management post payload message)https://vercel.com/docs/accounts/spend-management#spend-amount, your project will automatically pause.
⚠️Caution
I have tested only in postman and my project was paused successfully several times, and while I think this works, there might be unknown circumstances in which it doesn't:
- If your endpoint doesn't work
- If you misspell anything
If I missunderstood how Vercel will send the post JSON.
I urge you to double-check everything, and let me know if it does or it doesn't work so I can update this.
I'm not responsible for any charge you have using this.
How
- Install this component in your next.js 14 app.
a. npm
npm i vercel-killswitch
b. manually/app/api/pause/route.ts
->/app/api/pause/route.ts
- Create the following env variables:
VERCEL_PROJECT_ID
VERCEL_TEAM_ID
VERCEL_PANIC_TOKEN
VERCEL_PANIC_SECRET
- Set spend Management on your vercel account
Settings
>Billing
>Spend Management
- set an amount
- set the webhook
https://your-project-url.whatever/api/pause
- copy the secret and set it as
VERCEL_PANIC_SECRET
That's it.
Feel free to modify env variable names.
Testing
- Go to Postman.
- Set up a new request:
- Method:
POST
- URL:
https://your-project-url.whatever/api/pause
- Method:
- In the request Headers:
- Key:
x-vercel-signature
- Value:
{{signature}}
(This is a variable)
- Key:
- In the request Body (select
raw
andJSON
):{ "budgetAmount": 50, "currentSpend": 50, "teamId": "YOUR TEAM ID HERE!!!!" }
In the Pre-request Script:
const payload = JSON.stringify({ "budgetAmount": 50, "currentSpend": 50, "teamId": "YOUR TEAM ID HERE!!!!" }); const secret = 'YOUR SECRET HERE!!!!'; const signature = CryptoJS .HmacSHA1(payload, secret) .toString(CryptoJS.enc.Hex); pm.environment.set('signature', signature);
Hit send. Your project should be paused.
To-do
- Test using postman
- Test on a real vercel trigger(let me know if it happens to you, or if it doesn't!)
- Publish on npm
- Publish as a template on Vercel❓
- Blog post
- YT video
- interactive install
- letting user choose where
route.ts
get's copied - leading user through the rest of the set-up
- letting user choose where
vercel
2 years ago