Menu

Metered billing for an AI SaaS: the 402 pattern and the Stripe gotcha that breaks renewals
📰

Metered billing for an AI SaaS: the 402 pattern and the Stripe gotcha that breaks renewals

DEV Community: nextjs·Shipwright·6 days ago
#KdB4mUfK
#devtonextjs#devto#dev#plan#stripe
Reading 0:00
15s threshold

Metering tells you what a user cost you. Billing is turning that number into "upgrade or stop" — and the mechanism for that is smaller than most Stripe tutorials suggest. It comes down to one HTTP status code, a table you keep in sync with webhooks, and one Stripe API detail that will silently break every renewal date if you get it wrong.

Enforce with 402, and put the upgrade in the body

When a user is over their monthly allowance, the route returns HTTP 402 — "Payment Required," the conventional signal for "you need to pay to continue." The status alone is not enough, though: the response body carries everything the client needs to render a real upgrade prompt instead of a dead error.

const quota = await checkQuota ( userId ) ; if ( ! quota . ok ) { return Response . json ( { error : `You've used all ${quota.limit} requests on the ${quota.plan.label} plan this month.` , plan : quota . plan . id , used : quota . used , limit : quota .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.