- एक custom credit entitlement (tokens) और ऐसा meter बनाना जो उससे अपने-आप deduction करे
- Subscription plans (overage के साथ और बिना) और one-time top-up product में credits जोड़ना
- एक वास्तविक OpenAI completion endpoint को जोड़ना, जो Dodo Payments के ज़रिए tokens की billing करे
- SDK के ज़रिए customer का live credit balance query करना
- Webhook signatures verify करना और Dodo Payments credit events को route करना
हम क्या बना रहे हैं
NeuralAPI का pricing model इस प्रकार है:- एक Dodo Payments account (test mode पर्याप्त है)
- OpenAI API key
- Node.js 18+
- TypeScript/Node.js की बुनियादी जानकारी
Step 1: अपना Token Credit Entitlement बनाएं
सबसे पहले वह credit entitlement बनाएं, जिसे दोनों subscription plans और top-up pack साझा करेंगे। इसे आपके platform द्वारा उपयोग की जाने वाली “token” unit की परिभाषा समझें।
The Credits tab under Products shows all your credit entitlements.
Navigate to Credits
- अपने Dodo Payments dashboard में log in करें
- बाएं sidebar में Products पर click करें
- Credits tab चुनें
- Create Credit पर click करें
Configure the credit unit
API TokensCredit Type: Custom Unit चुनेंUnit Name: tokenPrecision: 0 (tokens हमेशा whole numbers होते हैं)Credit Expiry: 30 days (credits हर billing cycle पर reset होते हैं)Skip overage at the credit level
Save and copy the credit ID
cent_xxxxxxxxxxxx जैसी दिखती है।API Tokens credit entitlement तैयार है। अब meter बनाएं, ताकि usage events अपने-आप deductions कर सकें।Step 2: Token Usage के लिए Meter बनाएं
एक meter आने वाले usage events को aggregate करके उन्हें credit deductions में बदलता है। Plan products बनाने से पहले आपको इसकी आवश्यकता होगी, क्योंकि Step 3 में product creation के दौरान इसे attach करेंगे।Open the Meters section
- Dashboard sidebar में Products → Meters पर जाएं
- Create Meter पर click करें
Configure the meter
Token Usage MeterEvent Name: api.tokens_used (यह आपके app द्वारा भेजे जाने वाले नाम से बिल्कुल match होना चाहिए)Aggregation Type: Sum — हम प्रत्येक event से token count का sum लेते हैंOver Property: tokens — प्रत्येक event की वह metadata key, जिसकी value का sum लिया जाएगाMeasurement Unit: tokensMeter save करें और उसकी ID copy करें — products से attach करते समय इसका reference देंगे।Step 3: Plan Products बनाएं
दोनों plans Usage Based Billing products होने चाहिए, साधारण Subscriptions नहीं — meters केवल UBB products से attach हो सकते हैं, और customers के API call करते समय credits को auto-deduct करने के लिए meter आवश्यक है। UBB products recurring base fee ($29 / $99) को support करते हैं; इसके ऊपर का usage credits में billed होता है।

Usage Based Billing pricing type with meter configuration.
Starter Plan ($29/month — 10M tokens, no overage)
Create the Starter UBB product
- Products → Create Product पर जाएं
- Pricing type के रूप में Usage Based Billing चुनें
- यह जानकारी भरें:
NeuralAPI StarterDescription: 10 million API tokens per month. Perfect for individual developers and small projects.Fixed Price: 29.00 (recurring base fee — किसी भी usage से पहले भी monthly billed)Billing Cycle: MonthlyCurrency: USDAttach the meter
Token Usage Meter जोड़ें। फिर meter में:- Bill usage in Credits को on करें
- Credit Entitlement:
API Tokensचुनें - Meter units per credit:
1— event का प्रत्येक token 1 deducted credit से map होता है - Free Threshold:
0— credit allocation ही customer का “free tier” है; अतिरिक्त free band की आवश्यकता नहीं है

Toggle 'Bill usage in Credits' on the meter and pick the credit entitlement.
api.tokens_used events customer के balance से वास्तव में deduct होते हैं।Configure credit issuance for Starter
10000000Allow Overage: Disabled — tokens समाप्त होने पर Starter customers blocked हो जाते हैंImport Default Credit Settings: Enabled — credit entitlement की 30-day expiry का उपयोग करें
Configure credit issuance per cycle on the UBB product.
Pro Plan ($99/month — 40M tokens, overage enabled)
Create the Pro UBB product
NeuralAPI ProDescription: 40 million API tokens per month with overage. Built for production applications.Fixed Price: 99.00Billing Cycle: MonthlyCurrency: USDAttach the meter
Token Usage Meter जोड़ें, Bill usage in Credits को on करें, API Tokens चुनें, Meter units per credit 1 और Free Threshold 0 रखें।Configure credit issuance with overage
40000000Import Default Credit Settings: Disable — हमें हर product के लिए overage settings customize करनी हैंAllow Overage: EnabledPrice Per Unit: 0.000005 USD per token (अर्थात 5 per 1M tokens — plan की effective per-token rate से अधिक, ताकि spillover हतोत्साहित हो)Overage Behavior: Bill overage at billing — overage अगले invoice पर charge होता है, फिर balance reset हो जाता हैProduct save करें और product ID copy करें।Step 4: Token Top-Up Pack बनाएं
Top-up pack एक one-time purchase है, जो मौजूदा customer के balance में 5,000,000 tokens जोड़ता है।
Single Payment pricing selected for a one-time credit product.
Create a one-time product
- Products → Create Product पर जाएं
- Pricing type के रूप में Single Payment चुनें
- यह जानकारी भरें:
Token Top-Up PackDescription: Instantly add 5 million tokens to your NeuralAPI balance.Price: 19.00Currency: USDAttach the token credit
- Entitlements section में Credits के पास Attach पर click करें
API Tokensचुनें- Credits issued सेट करें:
5000000 - Import Default Credit Settings को Disable करें — default 30-day expiry को override करना है
- Credit Expiry सेट करें:
365 days - Product save करें
Step 5: Backend बनाएं
अब Express server बनाएं, जो subscription checkout, top-up checkout, token billing के साथ वास्तविक OpenAI completions, balance queries और credit webhook events संभालेगा।Set up your project
tsconfig.json बनाएं:package.json scripts को update करें:Set up environment variables
.env बनाएं:DODO_PAYMENTS_WEBHOOK_KEY भरेंगे।Implement the server
src/server.ts:A note on how deductions actually happen
- आपका handler OpenAI को call करता है और
usage.total_tokens(जैसे 1532) प्राप्त करता है। - आप एक single usage event ingest करते हैं:
event_name: api.tokens_used,metadata: { tokens: 1532 }। Token Usage Metercustomer के आधार पर events aggregate करता है।- Meter
API Tokenscredit से Bill usage in Credits के साथ जुड़ा होने के कारण, Dodo Payments customer के सबसे पुराने non-expired grant से 1532 credits deduct करता है (FIFO)। - Overage enabled होने पर यदि customer शून्य से नीचे चला जाता है, तो deficit track होकर अगले invoice में billed होता है।
Step 6: Demo Frontend जोड़ें
अपने browser में सभी flows test करने के लिएpublic/index.html बनाएं। हम customer ID को localStorage में persist करते हैं, ताकि subscribe → generate → top-up में एक ही identity साझा हो और logged-in app का अनुकरण हो:
Step 7: Webhook जोड़ें
Webhooks आपके server को balance changes पर react करने देते हैं — customers के शून्य तक पहुंचने से पहले “running low” emails भेजने के लिए इनका उपयोग करेंगे।Expose your local server
https://...ngrok-free.app URL copy करें।Register the webhook in Dodo Payments
- In the dashboard, go to Developers → Webhooks → Add Endpoint
- URL:
https://your-tunnel.ngrok-free.app/webhooks/dodo - Subscribe to (at minimum):
credit.addedcredit.deductedcredit.overage_charged
- Save and copy the Signing Secret
- Paste it into
.envasDODO_PAYMENTS_WEBHOOK_KEY, then restartnpm run dev
Step 8: पूरा Flow Test करें
Subscribe a test customer
npm run devचलाएंhttp://localhost:3000खोलें- Pro Plan चुनें, test email + name दर्ज करें, Get Checkout Link पर click करें और test card details के साथ checkout पूरा करें
- Dashboard में Customers → most recent पर जाएं और
cus_...ID copy करें - Demo के “Logged-in customer ID” field में इसे paste करके Save पर click करें
Generate a real AI response
total_tokens प्राप्त करता है, usage event ingest करता है और response लौटाता है।Test the top-up flow
credit.added event दिखना चाहिए।Troubleshooting
Credits not deducting after usage events
Credits not deducting after usage events
- Meter का event name आपके भेजे जा रहे
event_nameसे match नहीं करता (api.tokens_usedcase-sensitive है) - Meter product पर
API Tokenscredit से linked नहीं है — product की meter configuration में जाकर पुष्टि करें कि Bill usage in Credits on है metadata.tokenskey meter के “Over Property” field से match नहीं करती- Customer का grant expire हो गया है (customer की credit history जांचें)
- Products → Meters: meter खोलें और पुष्टि करें कि product attachment पर linked credit name दिख रहा है
- Meter का Events tab — deduction से पहले भी ingested events यहां दिखाई देने चाहिए
- Customers → [Customer] → Credits: ledger entries एक या दो मिनट में दिखनी चाहिए
Balance always shows 0 or 'customer not found'
Balance always shows 0 or 'customer not found'
- Customer ने अभी checkout पूरा नहीं किया है — credits केवल successful payment के बाद जारी होते हैं
- आप गलत
customer_idसे query कर रहे हैं (अपने DB ID के बजाय dashboard सेcus_...ID का उपयोग करें) CREDIT_ENTITLEMENT_IDin.envproduct से attached credit से match नहीं करता
Overage not working for Pro plan customers
Overage not working for Pro plan customers
- Pro product के credit attachment पर overage enabled नहीं था (credit-level setting केवल default है)
- Customer वास्तव में Pro पर नहीं, Starter पर है
- Overage limit 0 पर set है
0.000005 (= $5 per million tokens; leading zeros दोबारा जांचें — field per-token price लेता है, per-1K नहीं)।`Webhook verification failed` in logs
`Webhook verification failed` in logs
- Body parsing order:
express.json()was applied to/webhooks/dodobeforeexpress.raw()— the SDK needs the raw bytes of the request, not parsed JSON - Wrong signing secret in
DODO_PAYMENTS_WEBHOOK_KEY - Reverse proxy is rewriting headers
app.use('/webhooks/dodo', express.raw(...)) line comes before app.use(express.json()) in server.ts.सहायता चाहिए?
बधाई हो! आपने NeuralAPI के लिए Credit-Based Billing बना ली है
अब आपके platform में एक complete, production-ready credit billing system है:Token Credit Entitlement
API Tokens credit, जिसे सभी plans और top-up pack साझा करते हैंTiered Plans, One Credit
One-Time Top-Up Pack
Auto-Deduction via Meter
Live Balance API
Verified Webhook Pipeline
credit.added, credit.deducted, credit.overage_charged) routed through a signature-verified handler using the SDK’s Standard Webhooks helper- Auth on
/credits/:customerIdand/api/generate— currently anyone can hit these with any customer ID. Authenticate users and look up their customer ID server-side. - Stable
event_ids — the example usesDate.now() + random. In production, use your request ID so retries are idempotent (Dodo Payments deduplicates byevent_id). - Persist the customer↔user mapping — store
customer_idin your DB after the first checkout so you don’t need a manual paste step. - Decide what happens when a subscription ends. Plan credits remain in the customer’s ledger until their natural expiry (30 days from issuance) and top-up credits stay valid for 365 days — but the cookbook’s
/api/generateonly checks balance, not subscription status. So a cancelled customer can still consume their remaining tokens. That’s the consumer-friendly default. If you want stricter access control, either (a) listen to thesubscription.cancelledwebhook and gate/api/generateon subscription status, or (b) call Dodo’s ledger API to debit unused plan credits on cancel while leaving top-up credits intact. - Monitor the Usage Billing dashboard to catch metering anomalies early.