Skip to main content
Sentra को आपके लिए integration code लिखने दें।
SDK/API code, webhook handlers, credit grants और अन्य चीज़ें generate करने के लिए VS Code, Cursor या Windsurf में हमारे AI assistant का उपयोग करें — बस बताएं कि आपको क्या चाहिए।
Sentra आज़माएं: AI-Powered Integration →
इस tutorial में आप NeuralAPI बनाएंगे — एक tiered AI platform, जिसमें हर subscription plan के साथ monthly token credit allowance मिलता है, tokens कम होने पर customers top-up packs खरीद सकते हैं, और OpenAI द्वारा requests process किए जाने पर आपका backend अपने-आप credits deduct करता है।
इस tutorial में Node.js/Express + OpenAI SDK का उपयोग किया गया है। Dodo Payments concepts (credits, meters, webhooks) किसी भी framework या AI provider पर लागू होते हैं — इन्हें अपनी आवश्यकता के अनुसार अपनाएं।
इस tutorial के अंत तक आप यह करना जान जाएंगे:
  • एक 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 की परिभाषा समझें।
बनाए गए credit entitlements दिखाने वाला Credits listing page

The Credits tab under Products shows all your credit entitlements.

1

Navigate to Credits

  1. अपने Dodo Payments dashboard में log in करें
  2. बाएं sidebar में Products पर click करें
  3. Credits tab चुनें
  4. Create Credit पर click करें
2

Configure the credit unit

अपने token credit की मूल जानकारी भरें:Credit Name: API TokensCredit Type: Custom Unit चुनेंUnit Name: tokenPrecision: 0 (tokens हमेशा whole numbers होते हैं)Credit Expiry: 30 days (credits हर billing cycle पर reset होते हैं)
Credit बनने के बाद Precision बदली नहीं जा सकती। Token counts के लिए 0 (whole numbers) लगभग हमेशा सही होता है।
3

Skip overage at the credit level

यहां overage disabled रखें — credit को products से जोड़ते समय इसे हर plan के लिए configure करेंगे। इससे Starter plan usage को शून्य पर block करेगा, जबकि Pro plan overage की अनुमति देगा।
यहां configure की गई overage settings defaults हैं। प्रत्येक product attachment इन्हें override कर सकता है — और Step 3 में हम यही करेंगे।
4

Save and copy the credit ID

Create Credit पर click करें। Save होने के बाद credit खोलें और उसकी ID copy करें — यह 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 करेंगे।
1

Open the Meters section

  1. Dashboard sidebar में ProductsMeters पर जाएं
  2. Create Meter पर click करें
2

Configure the meter

यह जानकारी भरें:Meter Name: 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: tokens
Event names case-sensitive होते हैं। api.tokens_usedApi.Tokens.Used — एक चुनें और उसी का उपयोग करें।
Meter save करें और उसकी ID copy करें — products से attach करते समय इसका reference देंगे।
Meter बन गया है। अब products configure करते समय इसे credit से जोड़ सकते हैं।

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 configuration

Usage Based Billing pricing type with meter configuration.

Starter Plan ($29/month — 10M tokens, no overage)

1

Create the Starter UBB product

  1. Products → Create Product पर जाएं
  2. Pricing type के रूप में Usage Based Billing चुनें
  3. यह जानकारी भरें:
Product Name: 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: USD
2

Attach the meter

Select meter section में + पर click करें और Token Usage Meter जोड़ें। फिर meter में:
  1. Bill usage in Credits को on करें
  2. Credit Entitlement: API Tokens चुनें
  3. Meter units per credit: 1 — event का प्रत्येक token 1 deducted credit से map होता है
  4. Free Threshold: 0 — credit allocation ही customer का “free tier” है; अतिरिक्त free band की आवश्यकता नहीं है
Meter with Bill usage in Credits enabled and API Tokens selected

Toggle 'Bill usage in Credits' on the meter and pick the credit entitlement.

यही वह wiring है जिससे आने वाले api.tokens_used events customer के balance से वास्तव में deduct होते हैं।
3

Configure credit issuance for Starter

Product पर रहते हुए credit configuration section तक scroll करें, जो credit-billed meter attach होने के बाद दिखाई देता है:Credits issued per billing cycle: 10000000Allow Overage: Disabled — tokens समाप्त होने पर Starter customers blocked हो जाते हैंImport Default Credit Settings: Enabled — credit entitlement की 30-day expiry का उपयोग करें
Credit configuration form with per-cycle amount and overage settings

Configure credit issuance per cycle on the UBB product.

Save पर click करें और product ID copy करें।
Starter Plan: $29/month base fee, 10M tokens/cycle, शून्य पर blocked, meter के ज़रिए auto-deduct होता है।

Pro Plan ($99/month — 40M tokens, overage enabled)

1

Create the Pro UBB product

Starter जैसा ही flow है, बस numbers बड़े हैं:Product Name: NeuralAPI ProDescription: 40 million API tokens per month with overage. Built for production applications.Fixed Price: 99.00Billing Cycle: MonthlyCurrency: USD
2

Attach the meter

Starter के समान: Token Usage Meter जोड़ें, Bill usage in Credits को on करें, API Tokens चुनें, Meter units per credit 1 और Free Threshold 0 रखें।
3

Configure credit issuance with overage

अब credit issuance configure करें और इस बार overage enable करें:Credits issued per billing cycle: 40000000Import Default Credit Settings: Disable — हमें हर product के लिए overage settings customize करनी हैंAllow Overage: EnabledPrice Per Unit: 0.000005 USD per token (अर्थात 0.005per1Ktokensया0.005 per 1K tokens या 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 करें।
Pro Plan: 99/monthbasefee,40Mtokens/cycle,99/month base fee, 40M tokens/cycle, 0.005/1K tokens पर overage, meter के ज़रिए auto-deduct होता है।

Step 4: Token Top-Up Pack बनाएं

Top-up pack एक one-time purchase है, जो मौजूदा customer के balance में 5,000,000 tokens जोड़ता है।
Product pricing section with Single Payment selected

Single Payment pricing selected for a one-time credit product.

1

Create a one-time product

  1. Products → Create Product पर जाएं
  2. Pricing type के रूप में Single Payment चुनें
  3. यह जानकारी भरें:
Product Name: Token Top-Up PackDescription: Instantly add 5 million tokens to your NeuralAPI balance.Price: 19.00Currency: USD
2

Attach the token credit

  1. Entitlements section में Credits के पास Attach पर click करें
  2. API Tokens चुनें
  3. Credits issued सेट करें: 5000000
  4. Import Default Credit Settings को Disable करें — default 30-day expiry को override करना है
  5. Credit Expiry सेट करें: 365 days
  6. Product save करें
Product ID copy करें।
Top-ups के लिए अधिक लंबी expiry क्यों? Subscription credits हर 30 दिनों में reset होते हैं क्योंकि यही cycle है। Top-ups prepaid purchases हैं — customer ने $19 पहले ही दिए हैं और स्वाभाविक रूप से अपेक्षा करता है कि tokens एक महीने से अधिक समय तक चलें। 365 दिन OpenAI, AWS और Anthropic के वास्तविक prepaid credits के उपयोग से मेल खाते हैं, और आपकी liability को भी सीमित रखते हैं, ताकि customers अनिश्चितकाल तक credits जमा न कर सकें।
Top-Up Pack configure हो गया है — इसे खरीदने पर 5,000,000 tokens मिलते हैं, जो 365 दिनों तक valid रहते हैं।

Step 5: Backend बनाएं

अब Express server बनाएं, जो subscription checkout, top-up checkout, token billing के साथ वास्तविक OpenAI completions, balance queries और credit webhook events संभालेगा।
1

Set up your project

एक tsconfig.json बनाएं:
tsconfig.json
package.json scripts को update करें:
package.json
2

Set up environment variables

पिछले steps से प्राप्त credentials और IDs के साथ .env बनाएं:
.env
.env को कभी भी version control में commit न करें। इसे तुरंत .gitignore में जोड़ें।
Webhook endpoint register करने के बाद Step 7 में DODO_PAYMENTS_WEBHOOK_KEY भरेंगे।
3

Implement the server

Create src/server.ts:
Backend तैयार है: subscription checkout, top-up checkout, metered token billing के साथ OpenAI completion, balance query और verified webhook handler।
@dodopayments/ingestion-blueprints provides drop-in trackers that automate the usageEvents.ingest call for you — including the LLM Blueprint, API gateway, object storage, streams, and time-range usage.
4

A note on how deductions actually happen

आपने शायद ध्यान दिया होगा कि कोई स्पष्ट “deduct N credits” call नहीं है। यह जानबूझकर किया गया है:
  1. आपका handler OpenAI को call करता है और usage.total_tokens (जैसे 1532) प्राप्त करता है।
  2. आप एक single usage event ingest करते हैं: event_name: api.tokens_used, metadata: { tokens: 1532 }
  3. Token Usage Meter customer के आधार पर events aggregate करता है।
  4. Meter API Tokens credit से Bill usage in Credits के साथ जुड़ा होने के कारण, Dodo Payments customer के सबसे पुराने non-expired grant से 1532 credits deduct करता है (FIFO)।
  5. Overage enabled होने पर यदि customer शून्य से नीचे चला जाता है, तो deficit track होकर अगले invoice में billed होता है।
Meter यह सब संभालता है। आपका code केवल events ingest करता है।

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 भेजने के लिए इनका उपयोग करेंगे।
1

Expose your local server

Webhooks के लिए public URL आवश्यक है। Local development के लिए ngrok या कोई अन्य tunnel इस्तेमाल करें:
https://...ngrok-free.app URL copy करें।
2

Register the webhook in Dodo Payments

  1. In the dashboard, go to Developers → Webhooks → Add Endpoint
  2. URL: https://your-tunnel.ngrok-free.app/webhooks/dodo
  3. Subscribe to (at minimum):
    • credit.added
    • credit.deducted
    • credit.overage_charged
  4. Save and copy the Signing Secret
  5. Paste it into .env as DODO_PAYMENTS_WEBHOOK_KEY, then restart npm run dev
The SDK’s dodo.webhooks.unwrap() validates the webhook-id, webhook-timestamp, and webhook-signature headers using your signing secret. You don’t need to hand-roll HMAC verification — and you shouldn’t, because Dodo Payments uses Standard Webhooks, which signs id.timestamp.body rather than just the body.

Step 8: पूरा Flow Test करें

1

Subscribe a test customer

  1. npm run dev चलाएं
  2. http://localhost:3000 खोलें
  3. Pro Plan चुनें, test email + name दर्ज करें, Get Checkout Link पर click करें और test card details के साथ checkout पूरा करें
  4. Dashboard में Customers → most recent पर जाएं और cus_... ID copy करें
  5. Demo के “Logged-in customer ID” field में इसे paste करके Save पर click करें
Customer के पास 40,000,000 tokens होने चाहिए। पुष्टि करने के लिए Refresh Balance पर click करें।
2

Generate a real AI response

एक prompt लिखकर Generate पर click करें। Server OpenAI को call करता है, वास्तविक total_tokens प्राप्त करता है, usage event ingest करता है और response लौटाता है।
Usage events लगभग हर मिनट background worker द्वारा process किए जाते हैं। Balance तुरंत कम नहीं होगा — 30–90 seconds प्रतीक्षा करें और फिर Refresh Balance पर click करें। पहली refresh में बदलाव न दिखे तो इसे broken न समझें।
3

Test the top-up flow

Buy 5M Tokens — $19 पर click करके checkout पूरा करें। Payment सफल होने के बाद balance refresh करें — इसमें 5,000,000 tokens की वृद्धि दिखनी चाहिए। आपके server log में credit.added event दिखना चाहिए।

Troubleshooting

संभावित कारण:
  • Meter का event name आपके भेजे जा रहे event_name से match नहीं करता (api.tokens_used case-sensitive है)
  • Meter product पर API Tokens credit से linked नहीं है — product की meter configuration में जाकर पुष्टि करें कि Bill usage in Credits on है
  • metadata.tokens key meter के “Over Property” field से match नहीं करती
  • Customer का grant expire हो गया है (customer की credit history जांचें)
क्या जांचें:
  1. Products → Meters: meter खोलें और पुष्टि करें कि product attachment पर linked credit name दिख रहा है
  2. Meter का Events tab — deduction से पहले भी ingested events यहां दिखाई देने चाहिए
  3. Customers → [Customer] → Credits: ledger entries एक या दो मिनट में दिखनी चाहिए
संभावित कारण:
  • Customer ने अभी checkout पूरा नहीं किया है — credits केवल successful payment के बाद जारी होते हैं
  • आप गलत customer_id से query कर रहे हैं (अपने DB ID के बजाय dashboard से cus_... ID का उपयोग करें)
  • CREDIT_ENTITLEMENT_ID in .env product से attached credit से match नहीं करता
क्या जांचें: Customers → [Customer] → Credits खोलें। यदि वहां कोई credits नहीं दिखते, तो product entitlement attach नहीं किया गया या payment पूरा नहीं हुआ।
संभावित कारण:
  • Pro product के credit attachment पर overage enabled नहीं था (credit-level setting केवल default है)
  • Customer वास्तव में Pro पर नहीं, Starter पर है
  • Overage limit 0 पर set है
क्या जांचें: Pro → Entitlements → Credits edit करें → पुष्टि करें कि Allow Overage on है और Price Per Unit 0.000005 (= $5 per million tokens; leading zeros दोबारा जांचें — field per-token price लेता है, per-1K नहीं)।
Possible causes:
  • Body parsing order: express.json() was applied to /webhooks/dodo before express.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
What to check: Confirm the 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

30-day expiry वाला reusable API Tokens credit, जिसे सभी plans और top-up pack साझा करते हैं

Tiered Plans, One Credit

Starter (10M, hard limit) और Pro (40M + overage), credit को duplicate किए बिना per-product configure किए गए

One-Time Top-Up Pack

Customers अपनी subscription बदले बिना $19 में 5M tokens जोड़ सकते हैं

Auto-Deduction via Meter

वास्तविक OpenAI token counts events के रूप में ingest होते हैं; meter बिना manual tracking के FIFO में credits deduct करता है

Live Balance API

Access नियंत्रित करने, usage दिखाने या app में customers को चेतावनी देने के लिए SDK के ज़रिए real-time balance

Verified Webhook Pipeline

Credit ledger events (credit.added, credit.deducted, credit.overage_charged) routed through a signature-verified handler using the SDK’s Standard Webhooks helper
Going to production? Tighten these:
  • Auth on /credits/:customerId and /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 uses Date.now() + random. In production, use your request ID so retries are idempotent (Dodo Payments deduplicates by event_id).
  • Persist the customer↔user mapping — store customer_id in 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/generate only 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 the subscription.cancelled webhook and gate /api/generate on 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.

Credit-Based Billing Reference

Complete CBB documentation: rollover, overage modes, ledger management और सभी API endpoints।

Credit Webhook Events

आपके server को प्राप्त होने वाले प्रत्येक credit event के payload schemas।
अंतिम संशोधन 31 जुलाई 2026