Skip to main content
Sentra को आपके लिए integration code लिखने दें।
VS Code, Cursor या Windsurf में हमारे AI assistant का उपयोग करके, केवल अपनी आवश्यकता बताकर SDK/API code, webhook handlers और अन्य चीज़ें generate करें।
Sentra आज़माएं: AI-Powered Integration →
इस tutorial में आप MailKit बनाएंगे, एक transactional email platform जहां customers पहले से email credits के pool के लिए भुगतान करते हैं। Plan प्रत्येक महीने email allowance देता है; credits कम होने पर customers अगले cycle का इंतज़ार करने के बजाय top-up pack खरीद सकते हैं। प्रत्येक send पर अपने-आप एक credit deduct होता है।
यह tutorial email provider के रूप में Resend का उपयोग करता है। इसका free tier (3,000 emails/month) paid account के बिना पूरे flow को बनाने और test करने के लिए पर्याप्त है। यह pattern किसी भी provider के साथ काम करता है; resend.emails.send को SendGrid, Postmark, SES या अपने SMTP relay से बदलें।
इस tutorial के अंत तक आप यह कर पाएंगे:
  • अपने dashboard में custom credit entitlement (emails) बनाना
  • Credits को subscription plan और one-time top-up product से जोड़ना
  • Resend के माध्यम से वास्तविक emails भेजना और ledger entry द्वारा प्रत्येक send पर एक credit debit करना
  • अपने frontend से live credit balance query करना
  • Dodo webhooks को सही ढंग से verify करना और customers के zero तक पहुंचने से पहले उन्हें सूचित करने के लिए credit.balance_low संभालना

हम क्या बना रहे हैं

MailKit का pricing model यह है: Unit है one email = one credit। Customers को tokens, batches या weighted units के बारे में सोचने की ज़रूरत नहीं है। उन्हें केवल यह दिखाई देता है: “इस महीने आपके पास 4,231 emails बाकी हैं।”
शुरू करने से पहले सुनिश्चित करें कि आपके पास ये हैं:
  • एक Dodo Payments account (test mode पर्याप्त है)
  • एक free Resend account और API key
  • Node.js 18+ और TypeScript की बुनियादी जानकारी

Step 1: अपना Email Credit Entitlement बनाएं

Credit entitlement उस unit को define करता है जिसे आपका platform बेचता है: इस मामले में, एक email send।
Credits listing page

The Credits tab under Products lists all your credit entitlements.

1

Open the Credits section

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

Configure the credit unit

Credit details भरें:Credit Name: Email CreditsCredit Type: Custom Unit चुनेंUnit Name: emailPrecision: 0 (email हमेशा पूरी unit होता है; आप आधा email नहीं भेज सकते)Credit Expiry: 30 days (प्रत्येक cycle का allowance reset होता है)
Creation के बाद Precision बदला नहीं जा सकता। Emails, messages या sessions जैसी discrete units के लिए 0 सही है।
3

Leave the other defaults as-is

हम इस cookbook में rollover या overage enable नहीं करेंगे; लक्ष्य सबसे सरल CBB flow बनाना है। आप बाद में credit attachment पर इन settings की समीक्षा कर सकते हैं।
4

Save and copy the credit ID

Create Credit पर click करें। Credit खोलकर उसकी ID copy करें। Backend balance queries के लिए इसकी आवश्यकता होगी। यह cent_xxxxxxxxxxxx जैसी दिखाई देती है।
आपका Email Credits entitlement तैयार है। अगला चरण: customers को credits देने वाले products।

Step 2: Plan और Top-Up Pack बनाएं

आप दो products बनाएंगे: एक recurring Subscription plan और एक Single Payment top-up। Plan प्रत्येक cycle में 5,000 emails देता है; top-up मांग पर 5,000 और जोड़ता है। दोनों एक ही Email Credits entitlement से जुड़े होंगे।
यह cookbook usage-based meters के बजाय direct ledger entries द्वारा credits deduct करता है। Ledger entries तुरंत लागू होती हैं (balance milliseconds में update होता है), इनके लिए अतिरिक्त setup की आवश्यकता नहीं होती और जब एक user action ठीक एक credit के बराबर हो, तब ये सही विकल्प हैं। यदि आप ingested usage events से auto-deduction पसंद करते हैं (“tokens” या “MB processed” जैसी weighted units के लिए उपयोगी), तो meter-based pattern के लिए Credit-Based Billing → Usage Billing with Credits देखें।

MailKit Plan ($19/month, 5,000 emails)

1

Create the subscription

  1. Products → Create Product पर जाएं
  2. Product details भरें:
Product Name: MailKit PlanDescription: 5,000 transactional emails per month.
  1. Product type के रूप में Subscription चुनें
  2. Recurring price सेट करें:
Recurring Price: 19.00Billing Cycle: MonthlyCurrency: USD
2

Attach the email credit entitlement

Entitlements → Credits → Attach तक scroll करें और configure करें:Credit Entitlement: Email CreditsCredits issued per billing cycle: 5000Low Balance Threshold: 20 (percent; जब balance cycle allowance के 20% से कम, यानी 1,000 emails, हो जाता है तब credit.balance_low fire होता है)Import Default Credit Settings: enabled (Step 1 से 30-day expiry का उपयोग करता है)Add to Product पर click करें, फिर product को Save करें। Product ID (pdt_xxxxxxxxxxxx) copy करें।
Plan: $19/month → प्रत्येक cycle में 5,000 emails refresh।

Top-Up Pack ($9 one-time, 5,000 emails)

1

Create a one-time product

  1. Products → Create Product पर जाएं
  2. Product details भरें:
Product Name: Email Top-Up PackDescription: Add 5,000 emails to your MailKit balance instantly.
  1. Product type के रूप में Single Payment चुनें
  2. Pricing सेट करें:
Price: 9.00Currency: USD
2

Attach the credit grant

Entitlements → Credits → Attach में:
  • Credit Entitlement: Email Credits
  • Credits issued: 5000
One-time products अपनी expiry (Step 1 के अनुसार purchase से 30 days) के साथ credits देते हैं। Top-ups subscription credits के ऊपर जुड़ते हैं; उन्हें replace नहीं करते।
Save करें और product ID copy करें।
Top-Up Pack: $9 → +5,000 emails, तुरंत उपलब्ध।

Step 3: Backend सेट अप करें

अब वह Express server बनाएं जो checkout, sending, balance queries और webhooks को handle करता है।
1

Initialize the project

package.json में dev script जोड़ें:
tsx बिना build step या tsconfig.json के सीधे TypeScript चलाता है, जो tutorial के लिए उपयुक्त है। Production के लिए tsconfig.json और build script जोड़ें।
2

Configure environment variables

.env बनाएं:
.env
Endpoint बनाने के बाद Step 4 में आप DODO_WEBHOOK_KEY भरेंगे। Resend API key resend.com/api-keys से प्राप्त होती है।
.env को तुरंत .gitignore में जोड़ें। API keys को कभी commit न करें।
3

Build the server

Project root में server.ts बनाएं:
Webhook body raw होना चाहिए। express.json() body को parse और re-serialize करता है, जिससे signature verification टूट जाती है। /webhooks/dodo को express.raw() के साथ, app.use(express.json()) line से पहले define करें।
Backend तैयार है: subscribe, top-up, balance, send और webhook handler सभी wired up हैं।
4

Add a demo UI

public/index.html बनाएं:

Step 4: Webhook Endpoint जोड़ें

credit.balance_low event customers के credits खत्म होने से पहले उन्हें सूचित करने देता है। इसके बिना, उन्हें समस्या का पहली बार तब पता चलेगा जब email भेजने में failure होगा।
1

Expose your local server

Webhooks के लिए public URL आवश्यक है। Development के दौरान ngrok (या कोई भी tunnel) उपयोग करें:
HTTPS forwarding URL copy करें (जैसे https://1234abcd.ngrok-free.app)।
2

Register the endpoint in Dodo

  1. Developers → Webhooks → Add Endpoint पर जाएं
  2. URL: https://1234abcd.ngrok-free.app/webhooks/dodo
  3. Events: credit.added, credit.balance_low और credit.rolled_over को subscribe करें
  4. Save करें, फिर signing key को .env में DODO_WEBHOOK_KEY के रूप में copy करें
  5. अपना server restart करें

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

1

Start the server

आपको MailKit running on http://localhost:3000 दिखाई देगा। इसे अपने browser में खोलें।
2

Subscribe a test customer

  1. Section 1 में test email और name दर्ज करें, Get checkout link पर click करें
  2. Link खोलें और test card से checkout पूरा करें
  3. Payment के बाद, अपने dashboard में Customers के अंतर्गत customer_id खोजें
अब customer के balance में 5,000 emails होने चाहिए। Customers → [Customer] → Credits देखें।
3

Send a real email

  1. customer_id को section 3 में paste करें
  2. to को delivered@resend.dev पर ही रहने दें (Resend का sandbox inbox जो सब कुछ स्वीकार करता है)
  3. Send पर click करें
आपको Resend message id वापस मिलेगी। Section 2 में balance refresh करें और count तुरंत 4,999 हो जाएगा। प्रत्येक ledger debit लिखे जाते ही live balance में reflect होता है।
4

Trigger the low-balance webhook

Threshold 20% (5,000-email allowance में से 1,000) है। 4,000 वास्तविक emails भेजे बिना इसे trigger करने के लिए dashboard से manually debit the balance करें:
  1. Customers → [Customer] → Credits → Email Credits पर जाएं
  2. Adjust Balance पर click करें और 4000 debit करें
  3. Demo के माध्यम से एक और email भेजें
आपके server को कुछ seconds में यह log करना चाहिए:
आपके server ने webhook प्राप्त और verify कर लिया। Production में यहीं से आप customer को email भेजेंगे या in-app banner दिखाएंगे।
5

Buy a top-up pack

  1. customer_id को section 4 में paste करें
  2. Buy 5,000 emails पर click करें और test checkout पूरा करें
  3. Balance refresh करें; यह 5,000 बढ़ जाएगा
एक credit.added event grant_source: one_time के साथ fire होता है। Top-up subscription credits के ऊपर जुड़ता है; दोनों pools FIFO (सबसे पुराना non-expired grant पहले) consume होते हैं।
6

Test the hard stop

Balance को manually zero पर debit करें, फिर एक और email भेजने का प्रयास करें। आपको यह मिलेगा:
यह 402 आपके application-level enforcement को दर्शाता है। Dodo balance API ही source of truth है; इसे client पर कभी cache न करें।

Troubleshooting

Signature raw HTTP body पर compute होता है। express.json() payload को parse और re-serialize करता है, जिससे HMAC टूट जाता है। सुनिश्चित करें कि /webhooks/dodo को express.raw({ type: 'application/json' }) के साथ app.use(express.json()) line से ऊपर register किया गया है और DODO_WEBHOOK_KEY endpoint detail page पर दिखाई गई signing key से match करता है।
इन तीन चीज़ों को इसी क्रम में verify करें:
  1. Customer ने checkout पूरा किया (credits session creation पर नहीं, successful payment पर issue होते हैं)
  2. आपके .env में CREDIT_ENTITLEMENT_ID product से जुड़े credit से match करता है (mismatched IDs चुपचाप गलत credit में लिखते हैं)
  3. आप जो customer_id भेज रहे हैं, वह Dodo से आया है (dashboard में customers table), आपके अपने database से नहीं
Sandbox sender onboarding@resend.dev केवल आपके Resend account के email या delivered@resend.dev पर deliver करता है। किसी अन्य को भेजने के लिए verify a domain करें और उस domain पर from address का उपयोग करें।

आपने क्या बनाया

One reusable credit unit

Email Credits, जिसे एक बार define करके subscription plan और top-up pack दोनों से attach किया गया।

Subscription with prepaid allowance

$19/month पर प्रत्येक cycle में 5,000 emails मिलते हैं। Customers जानते हैं कि वे किस चीज़ के लिए भुगतान कर रहे हैं और आप अपना worst-case cost जानते हैं।

Top-up pack

एक one-time product जो 5,000 emails देता है। बिना plan change के subscription credits के ऊपर जुड़ता है।

Instant ledger debits

प्रत्येक send के बाद एक createLedgerEntry call। कोई meter नहीं, aggregation lag नहीं, और Resend के message id के माध्यम से retry पर idempotent।

Credit-Based Billing Reference

Rollover, overage modes, ledger management और complete API surface के लिए पूरा CBB documentation पढ़ें।
सहायता चाहिए?
अंतिम संशोधन 31 जुलाई 2026