Skip to main content
Sentra को आपके लिए integration code लिखने दें।
SDK/API code, LLM Blueprint integration code, webhooks और बहुत कुछ generate करने के लिए VS Code, Cursor या Windsurf में हमारे AI assistant का उपयोग करें—बस बताएँ कि आपको क्या चाहिए।
Sentra आज़माएँ: AI-Powered Integration →
इस tutorial में आप automatic usage-based billing वाला एक AI chat application बनाएँगे। हम सब कुछ शुरू से बनाएँगे: billing meter, product configuration और वह application code जो conversations को संचालित करता है तथा real-time में token usage track करता है।
यह tutorial backend और frontend दोनों के साथ एक complete working application प्रदान करता है। chat app Google के Gemini AI का उपयोग करता है और बिना किसी manual counting के token usage को automatically track करता है।
इस tutorial के अंत तक आपके पास एक working chat application होगा, जो:
  • Google Gemini (AI SDK) का उपयोग करके AI conversations संचालित करता है
  • token usage को automatically track करता है (किसी manual code की आवश्यकता नहीं)
  • actual token consumption के आधार पर customers से charge करता है
  • एक सुंदर chat interface शामिल करता है
AI Chat Demo

हम क्या बनाएँगे

पहले अपनी AI chat service को समझते हैं:
  • Service: Google Gemini (AI SDK) का उपयोग करने वाली AI-powered chat
  • Pricing Model: Pay-per-token ($0.01 प्रति 1,000 tokens)
  • Free Tier: प्रति customer प्रति माह 10,000 free tokens
  • Features: Conversation history, automatic token tracking
शुरू करने से पहले सुनिश्चित करें कि आपके पास:

Step 1: अपना Usage Meter बनाएँ

हम आपके Dodo Payments dashboard में एक meter बनाकर शुरू करेंगे, जो AI token usage को track करेगा।
हम क्या बना रहे हैं: “AI Token Usage Meter” नाम का एक meter, जो chat conversations में consume हुए सभी tokens का योग करेगा।
1

Open the Meters section

  1. अपने Dodo Payments dashboard में log in करें
  2. बाएँ sidebar में Products पर click करें
  3. Meters पर click करें
  4. Create Meter button पर click करें
Create Meter
आपको एक form दिखाई देगा, जहाँ हम अपना token tracking configure करेंगे।
2

Fill in the basic meter information

अब अपनी AI chat service के specific details दर्ज करें:Meter NameAI Token Usage MeterDescriptionTracks token consumption from AI chat conversations using AI SDKEvent Nameai_chat_usage
Event name ai_chat_usage बाद में application code से भेजे जाने वाले नाम से exactly match होना चाहिए। Event names case-sensitive होते हैं!
3

Configure how we count tokens

Aggregation सेट करें (meter हमारे events को कैसे count करेगा):Aggregation Type: dropdown से Sum चुनेंAggregate Over: Type → totalTokensMeasurement Unit: Type → tokens
हम “Sum” का उपयोग कर रहे हैं क्योंकि हम अनेक chat messages में consume हुए सभी tokens को जोड़ना चाहते हैं। SDK प्रत्येक event में automatically totalTokens भेजता है।
4

Create your meter

  1. जाँच लें कि आपकी सभी settings ऊपर दिए गए values से match करती हैं
  2. Create Meter पर click करें
Meter Configuration
Meter बन गया! आपका “AI Token Usage Meter” अब tokens count करने के लिए तैयार है। अब हम इसे billing product से connect करेंगे।

Step 2: अपनी API Keys प्राप्त करें

App बनाने से पहले आवश्यक API keys एकत्र करते हैं।
1

Get Dodo Payments API Key

  1. अपने Dodo Payments dashboard में DevelopersAPI Keys पर जाएँ
  2. Create API Key पर click करें
  3. API key copy करें—यह test_abc123... जैसी दिखाई देगी
इस API key को save करें—हम इसे बाद में अपनी .env file में जोड़ेंगे।
2

Get Google AI API Key

  1. aistudio.google.com पर जाएँ
  2. Get API Key पर click करें
  3. नई API key बनाएँ या मौजूदा key का उपयोग करें
  4. Key copy करें
इस key को सुरक्षित रखें—हम इसे भी अपनी .env file में जोड़ेंगे।

Step 3: अपना Billing Product बनाएँ

अब हमें ऐसा product बनाना है जो हमारी pricing ($0.01 प्रति 1,000 tokens और 10,000 free tokens) define करे। इससे हमारा meter actual billing से connect होगा।
हम क्या बना रहे हैं: “AI Chat Service” नाम का एक product, जो generous free tier के साथ token consumption के आधार पर charge करता है।
1

Navigate to Products

  1. अपने Dodo Payments dashboard में बाएँ sidebar के Products पर click करें
  2. Create Product पर click करें
  3. Product type के रूप में Usage-Based चुनें
इससे Dodo Payments को पता चलता है कि billing meter usage पर आधारित होगी, fixed subscription पर नहीं।
2

Enter product details

आवश्यक details भरें:Product Name: → AI Chat ServiceDescription: → AI-powered chat service with automatic token-based billingProduct Image: संबंधित image upload करें
ये customer invoices पर दिखाई देंगे, इसलिए इन्हें स्पष्ट और professional रखें।
3

Connect your meter

Meter connect करने से पहले सुनिश्चित करें कि आपने अपने product के price type के रूप में Usage Based Billing चुना है।इसके अतिरिक्त, Fixed Price को 0 पर set करें, ताकि customers से केवल उनके usage के आधार पर charge हो और कोई base fee न हो।अब आपके द्वारा बनाए गए meter को link करें:
  1. नीचे scroll करके Associated Meter section पर जाएँ
  2. Add Meters पर click करें
  3. dropdown से “AI Token Usage Meter” चुनें (जिसे आपने पहले बनाया था)
  4. पुष्टि करें कि यह आपके product configuration में दिखाई दे रहा है
आपका meter अब इस product से successfully connect हो गया है।
4

Set your pricing

यहाँ हम अपना business model define करते हैं:Price Per Unit: → 0.00001 दर्ज करें (यह 1,000 tokens के लिए 0.01याप्रतिtoken0.01 या प्रति token 0.00001 है)Free Threshold: → 10000 दर्ज करें (customers को प्रति माह 10,000 free tokens मिलेंगे)
Product Pricing
Billing कैसे काम करती है: यदि कोई customer एक महीने में 25,000 tokens का उपयोग करता है, तो उससे 15,000 tokens (25,000 - 10,000 free) के लिए charge लिया जाएगा = 15,000 × 0.00001=0.00001 = 0.15
5

Save your product

  1. अपनी सभी settings की समीक्षा करें:
    • Name: AI Chat Service
    • Meter: AI Token Usage Meter
    • Price: $0.01 प्रति 1,000 tokens
    • Free tier: 10,000 tokens
  2. Save Changes पर click करें
Product बन गया! आपकी billing अब configure हो चुकी है। Customers से उनके token usage के आधार पर automatically charge लिया जाएगा।

Step 4: Test Purchase करें

App बनाना शुरू करने से पहले purchase करके एक test customer बनाएँ।
1

Get your payment link

  1. अपने Dodo Payments dashboard में Products पर जाएँ
  2. अपना “AI Chat Service” product खोजें
  3. Product के पास Share button पर click करें
  4. दिखाई देने वाले payment link को copy करें
2

Complete a test purchase

  1. Payment link को नए browser tab में खोलें
  2. Test payment details दर्ज करके purchase पूरा करें
Successful payment के बाद आपको एक customer ID मिलेगी, जिसका उपयोग हम अपने application code में करेंगे।
3

Find your customer ID

  1. अपने Dodo Payments dashboard पर वापस जाएँ
  2. बाएँ sidebar में Sales -> Customers पर जाएँ
  3. अभी बनाए गए customer को खोजें (test email वाला)
  4. Customer ID copy करें—यह cus_123 जैसी दिखाई देगी
इस customer ID को save करें—chat application को test करते समय इसका उपयोग करेंगे।

Step 5: Chat Application बनाएँ

अब हमारा billing setup complete है और एक test customer भी बनाया जा चुका है। अब automatic token tracking वाली AI chat application बनाएँ।
1

Set up your project

नई directory बनाएँ और project initialize करें:
2

Install dependencies

आवश्यक packages install करें:
3

Configure TypeScript

tsconfig.json बनाएँ:
tsconfig.json
module type और scripts जोड़ने के लिए package.json update करें:
package.json
4

Create project structure

Folders और files बनाएँ:
5

Set up environment variables

अपने project root में एक .env file बनाएँ:
.env
Placeholder values को Step 2 से प्राप्त अपनी actual API keys से replace करें।
6

Create the backend server

src/server.ts बनाएँ और यह complete server code copy करें:
यह integrated billing वाला complete AI chat server है:

Step 6: Chat Interface जोड़ें

अब full conversation history वाला सुंदर chat interface जोड़ें! public/index.html बनाएँ:
public/index.html

Step 7: अपनी Chat Application Test करें

अब अपनी AI chat app को test करने और billing को action में देखने का समय है! सुनिश्चित करें कि सब कुछ end-to-end काम कर रहा है।
हम क्या test कर रहे हैं: हम AI के साथ कुछ conversations करेंगे, verify करेंगे कि token events Dodo Payments तक पहुँच रहे हैं, और पुष्टि करेंगे कि billing calculations सही हैं।
1

Start the server

पहले सुनिश्चित करें कि सब कुछ set up है:
  1. Verify करें कि आपकी .env file में Step 2 की सभी API keys हैं
  2. Development server शुरू करें:
आपको यह दिखाई देना चाहिए:
Server चल रहा है! अब chat करने का समय है।
2

Open the chat interface

  1. अपना browser खोलें
  2. http://localhost:3000 पर जाएँ
  3. आपको सुंदर chat interface दिखाई देना चाहिए
सुनिश्चित करें कि server.ts में CUSTOMER_ID को Step 4 की अपनी actual test customer ID से update करें।
3

Have your first conversation

इसे test करते हैं! ये messages आज़माएँ:
  1. “Artificial intelligence क्या है?”
  2. “Machine learning कैसे काम करती है?”
  3. “क्या आप neural networks समझा सकते हैं?”
हर response के बाद token usage display को update होते देखें!
यदि आपको AI का response और token counts दिखाई दे रहे हैं, तो आपका app काम कर रहा है!
4

Check your Dodo Payments dashboard

अब verify करते हैं कि events receive हो रहे हैं:
  1. अपना Dodo Payments dashboard खोलें
  2. Usage BillingAI Token Usage Meter पर जाएँ
  3. Events tab पर click करें
  4. आपको अपने chat events की सूची दिखाई देनी चाहिए
क्या देखना है:
  • Event names: ai_chat_usage
  • Customer ID: आपकी test customer id
Meter Events
आपके द्वारा भेजे गए प्रत्येक message के लिए एक event दिखाई देना चाहिए!
5

Verify token counting

कुछ और messages भेजकर जाँचें कि token aggregation काम कर रही है:
  1. अपने meter में Customers tab पर जाएँ
  2. अपना test customer खोजें
  3. “Consumed Units” column देखें—इसमें उपयोग किए गए total tokens दिखाई देने चाहिए
Meter Customer Tokens
Meter सभी totalTokens values का योग automatically कर रहा है!
6

Test the free tier

Free tier से अधिक tokens का उपयोग करें:
  1. कई और conversations करें (कुल ~15,000+ tokens का लक्ष्य रखें)
  2. Meter dashboard में अपने Customers tab को फिर से देखें
  3. अब आपको यह दिखाई देना चाहिए:
    • Consumed Units: 15,000+ tokens
    • Chargeable Units: 5,000 (10,000 free tokens लागू)
    • Total Price: ~$0.05
Free Tier Test
सफलता! आपकी usage-based billing पूरी तरह काम कर रही है। Customers से उनके actual token consumption के आधार पर automatically charge लिया जाएगा।

Troubleshooting

सामान्य समस्याएँ और उनके solutions:
संभावित कारण:
  • Event name meter configuration से exactly match नहीं करता
  • Customer ID आपके account में मौजूद नहीं है
  • API key invalid या expired है
  • Network connectivity issues
Solutions:
  1. Verify करें कि event name meter configuration से exactly match करता है (case-sensitive: ai_chat_usage)
  2. जाँचें कि customer ID Dodo Payments dashboard में मौजूद है
  3. Simple API call से API key test करें
  4. Error messages के लिए server logs देखें
संभावित कारण:
  • Model usage information return नहीं कर रहा
  • Incorrect SDK version
Solutions:
  1. Test करें कि model usage return करता है या नहीं:
  1. Latest Blueprints SDK पर update करें: npm install @dodopayments/ingestion-blueprints@latest
संभावित कारण:
  • Environment के लिए गलत API key
  • .env file में extra spaces या quotes
Solutions:
  • सुनिश्चित करें कि test key test_ से शुरू होती है और live key live_ से
  • .env file में keys के आसपास के quotes हटाएँ
  • आवश्यकता होने पर नई key generate करें
मदद चाहिए?

बधाई हो! आपने AI Chat App बना लिया

अब आपके पास Dodo Payments द्वारा powered automatic token usage tracking और billing वाला एक fully functional AI chat application है। 🎉

और जानें

LLM Blueprint

LLM Ingestion Blueprint के बारे में और जानें

Ingestion Blueprints SDK

अधिक examples के लिए GitHub repository देखें

Join Community

Discord पर मदद पाएँ और अपने projects share करें
अंतिम संशोधन 31 जुलाई 2026