Skip to main content
To have your coding agent write the integration, install the Dodo Agent Plugin. It adds the Dodo Payments skills and MCP servers to Claude Code, Codex CLI, Cursor, VS Code / GitHub Copilot, Kiro, and OpenCode.
You’ll build NeuralAPI, a tiered AI API where each subscription plan includes a monthly allowance of token credits. Customers who run low buy a top-up pack, and your backend reports the tokens each OpenAI request uses so Dodo Payments deducts them from the customer’s balance.
This tutorial uses Node.js, Express, and the OpenAI SDK. The Dodo Payments concepts (credits, meters, and webhooks) work the same with any framework or AI provider.
When you finish, you’ll know how to:
  • Create a custom credit entitlement for tokens, and a meter that deducts from it.
  • Attach credits to subscription plans, with and without overage, and to a one-time top-up product.
  • Call OpenAI from an endpoint that bills tokens through Dodo Payments.
  • Read a customer’s live credit balance with the SDK.
  • Verify webhook signatures and route Dodo Payments credit events.

What We’re Building

NeuralAPI sells three products: Before you start, you need:
  • A Dodo Payments account. Build everything in test mode.
  • An OpenAI API key.
  • Node.js 22 or later, and working knowledge of TypeScript and Node.js.

Step 1: Create Your Token Credit Entitlement

Create the credit entitlement that both plans and the top-up pack share. It defines the token unit NeuralAPI sells.
Credits listing page showing created credit entitlements

The Credits tab under Products shows all your credit entitlements.

1

Navigate to Credits

  1. Log in to the Dodo Payments dashboard.
  2. Click Products in the sidebar.
  3. Select the Credits tab.
  4. Click Create Credit.
2

Configure the Credit Unit

Enter these values:Credit Name: API TokensCredit Type: Custom UnitUnit Name: tokenDefine Precision: 0. Token counts are whole numbers.Credit Expiry: 30 days. Credits expire 30 days after they’re issued, which matches the monthly billing cycle.
Precision can’t be changed after you create the credit. For token counts, use 0.
3

Skip Overage at the Credit Level

Leave overage disabled on the credit. You configure it per plan when you attach the credit to each product, so the Starter plan can block usage at zero while the Pro plan allows overage.
Overage settings on the credit are defaults. Each product attachment can override them, which Step 3 does for the Pro plan.
4

Save and Copy the Credit ID

Click Create Credit. Open the saved credit and copy its ID, which starts with cde_.
The API Tokens credit entitlement is ready. Next, create a meter so that usage events deduct credits.

Step 2: Create a Meter for Token Usage

A meter aggregates incoming usage events. When you link it to a credit, the aggregated usage is deducted from the customer’s credit balance. Create the meter before the plan products, because you attach it while you create them in Step 3.
1

Open the Meters Section

  1. In the dashboard sidebar, go to Products → Meters.
  2. Click Create Meter.
2

Configure the Meter

Enter these values:Meter Name: Token Usage MeterEvent Name: api.tokens_used. This must match the event_name your app sends.Aggregation Type: Sum, to add up the token count from each event.Over Property: tokens, the metadata key whose value is summed.Measurement Unit: tokens
Event names are case-sensitive: api.tokens_used and Api.Tokens.Used are different events. You can’t edit a meter after you create it, so check every value before you confirm.
Create the meter. You select it by name when you attach it to products.
The meter is created. Next, link it to the credit on each plan product.

Step 3: Create the Plan Products

Create both plans with the Usage Based Billing pricing type, not plain Subscription. Meters attach to Usage Based Billing products, and the meter is what deducts credits as customers call your API. A Usage Based Billing product still charges a recurring base fee ($29 or $99), and usage on top of it is billed in credits.
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 Product

  1. Go to Products and click Add Product.
  2. Under Pricing Type, select Usage Based Billing.
  3. Enter these values:
Product Name: NeuralAPI StarterDescription: 10 million API tokens per month. Perfect for individual developers and small projects.Price: 29.00. This is the recurring base fee, charged every month even before any usage.Repeat payment every: 1 monthCurrency: USD
2

Attach the Meter

In the Select meter section, click + and add Token Usage Meter. Then configure the meter:
  1. Turn on Bill usage in credits.
  2. Select credit: API Tokens
  3. Meter units per credit: 1. Each token in an event deducts one credit.
  4. Free Threshold: 0. The free threshold applies only when a meter bills in money. When it bills in credits, every unit is deducted from the balance.
Meter with Bill usage in Credits enabled and API Tokens selected

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

This link is what makes incoming api.tokens_used events deduct from the customer’s balance.
3

Configure Credit Issuance for Starter

After you attach a credit-billed meter, the product shows a credit configuration section. Enter:Credits issued per billing cycle: 10000000Import Default Credit Settings: on, so the product uses the 30-day expiry from the credit entitlement.Allow Overage: off. The default from Step 1 keeps overage disabled, so Starter customers stop at zero.
Credit configuration form with per-cycle amount and overage settings

Configure credit issuance per cycle on the UBB product.

Save the product and copy its ID, which starts with pdt_.
Starter Plan: $29/month base fee, 10M tokens per cycle, blocked at zero, and deducted through the meter.

Pro Plan ($99/month — 40M Tokens, Overage Enabled)

1

Create the Pro Product

Follow the Starter flow with these values:Product Name: NeuralAPI ProDescription: 40 million API tokens per month with overage. Built for production applications.Price: 99.00Repeat payment every: 1 monthCurrency: USD
2

Attach the Meter

Configure the meter as you did for Starter: add Token Usage Meter, turn on Bill usage in credits, select API Tokens, and set Meter units per credit to 1 and Free Threshold to 0.
3

Configure Credit Issuance with Overage

Configure credit issuance, this time with overage enabled:Credits issued per billing cycle: 40000000Import Default Credit Settings: off, so you can set overage for this product.Allow Overage: onPrice Per Unit: 0.000005 USD per token. That is $0.005 per 1K tokens, or $5 per 1M tokens, which is above the plan’s effective per-token rate and discourages overage.Overage Behavior: Bill overage at billing. Overage is charged on the next invoice, and then the balance resets.Save the product and copy its ID.
Pro Plan: $99/month base fee, 40M tokens per cycle, overage at $0.005 per 1K tokens, and deducted through the meter.

Step 4: Create the Token Top-Up Pack

The top-up pack is a one-time purchase that adds 5,000,000 tokens to an existing customer’s balance.
Product pricing section with Single Payment selected

One-time pricing selected for a credit product.

1

Create a One-Time Product

  1. Go to Products and click Add Product.
  2. Under Pricing Type, select One Time.
  3. Enter these values:
Product Name: Token Top-Up PackDescription: Add 5 million tokens to your NeuralAPI balance.Price: 19.00Currency: USD
2

Attach the Token Credit

  1. In the Entitlements section, click Attach next to Credits.
  2. Select API Tokens.
  3. Set No of credits issued to 5000000.
  4. Turn off Import Default Credit Settings to override the default 30-day expiry.
  5. Set Credit Expiry to Custom and enter 365 days.
  6. Save the product.
Copy the product ID.
टॉप-अप पर लंबी expiry क्यों? Subscription credits 30 दिनों के बाद expire होते हैं क्योंकि यही billing cycle है। Top-up एक prepaid purchase है: customer ने पहले ही $19 का भुगतान किया है और उम्मीद करता है कि tokens एक महीने से अधिक समय तक चलें। 365 दिनों की expiry इस बात से मेल खाती है कि OpenAI और Anthropic पर prepaid API credits कैसे काम करते हैं, जहाँ खरीदे गए credits खरीदारी के एक वर्ष बाद expire होते हैं। साथ ही, इससे आपकी liability सीमित रहती है और customers अनिश्चितकाल तक credits जमा नहीं कर सकते।
The Top-Up Pack is configured. Buying it grants 5,000,000 tokens that stay valid for 365 days.

Step 5: Build the Backend

Build the Express server. It creates subscription and top-up checkouts, calls OpenAI and bills the tokens, reads balances, and receives credit webhook events.
1

Set Up Your Project

Create a tsconfig.json:
tsconfig.json
Update package.json scripts:
package.json
2

Set Up Environment Variables

Create .env with a test mode API key from Developer → API Keys and the IDs from the previous steps:
.env
Never commit .env to version control. Add it to .gitignore before your first commit.
You fill in DODO_PAYMENTS_WEBHOOK_KEY in Step 7, after you register the webhook endpoint.
3

Implement the Server

src/server.ts बनाएँ। Completion endpoint OpenAI के gpt-6-luna model को call करता है, जो high-volume requests के लिए उपयुक्त है। package.json tab पूरी dependency list दिखाता है:
Backend तैयार है: subscription checkout, top-up checkout, metered token billing के साथ OpenAI completion, balance read, और verified webhook handler।
@dodopayments/ingestion-blueprints ऐसे trackers प्रदान करता है जो आपके लिए usageEvents.ingest call करते हैं, जिसमें LLM Blueprint, API gateway, object storage, streams, और time-range usage शामिल हैं।
4

How Deductions Happen

Server कभी भी “deduct N credits” endpoint को call नहीं करता। Meter deduction करता है:
  1. आपका handler OpenAI को call करता है और usage.total_tokens पढ़ता है, उदाहरण के लिए 1532।
  2. आप event_name: api.tokens_used और metadata: { tokens: 1532 } के साथ एक usage event ingest करते हैं।
  3. Token Usage Meter प्रत्येक customer के events को aggregate करता है। Background worker हर मिनट नए events को process करता है।
  4. क्योंकि meter API Tokens credit को Bill usage in credits के माध्यम से bill करता है, Dodo Payments 1532 credits deduct करता है और शुरुआत customer के उस grant से करता है जिसकी expiry सबसे पहले है (FIFO)।
  5. यदि overage enabled है और balance समाप्त हो जाता है, तो deficit को track करके अगली invoice पर bill किया जाता है।
आपका code केवल events ingest करता है।

Step 6: Demo Frontend जोड़ें

अपने browser में हर flow को test करने के लिए public/index.html बनाएँ। यह page customer ID को localStorage में save करता है, इसलिए subscribe, generate और top-up एक ही identity share करते हैं, जैसा कि logged-in app में होता है:

Step 7: Webhook जोड़ें

Webhooks आपके server को balance changes पर react करने देते हैं, उदाहरण के लिए ऐसे customer को email भेजने के लिए जिसका balance कम हो रहा है।
1

Expose Your Local Server

Webhooks के लिए public URL आवश्यक है। Local development के लिए ngrok या किसी अन्य tunnel का उपयोग करें:
HTTPS forwarding URL copy करें, जो ngrok-free.app पर समाप्त होता है।
2

Register the Webhook in Dodo Payments

  1. Dashboard में Developer → Webhooks पर जाएँ और Add endpoint पर click करें।
  2. URL https://your-tunnel.ngrok-free.app/webhooks/dodo दर्ज करें और अपने tunnel host का उपयोग करें।
  3. कम से कम ये events select करें:
    • credit.added
    • credit.deducted
    • credit.overage_charged
  4. Create endpoint पर click करें, फिर endpoint के Overview tab से signing secret copy करें।
  5. इसे .env में DODO_PAYMENTS_WEBHOOK_KEY के रूप में paste करें, फिर npm run dev restart करें।
SDK का dodo.webhooks.unwrap() आपके signing secret के साथ webhook-id, webhook-timestamp और webhook-signature headers की जाँच करता है, फिर payload parse करता है। अपना HMAC check न लिखें: Dodo Payments Standard Webhooks का पालन करता है, जो केवल body नहीं बल्कि id.timestamp.body को sign करता है।

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

1

Subscribe a Test Customer

  1. npm run dev चलाएँ।
  2. http://localhost:3000 खोलें।
  3. Pro चुनें, test email address और name दर्ज करें, और Get Checkout Link पर click करें। test card details का उपयोग करके checkout पूरा करें।
  4. Dashboard में Customers पर जाएँ, सबसे नए customer को खोलें, और उसकी ID copy करें, जो cus_ से शुरू होती है।
  5. Demo के Logged-in customer ID field में ID paste करें और Save पर click करें।
Customer के पास 40,000,000 tokens हैं। पुष्टि करने के लिए Refresh Balance पर click करें।
2

Generate an AI Response

एक prompt type करें और Generate पर click करें। Server OpenAI को call करता है, वास्तविक total_tokens पढ़ता है, usage event ingest करता है और response लौटाता है।
Background worker हर मिनट usage events process करता है, इसलिए balance तुरंत कम नहीं होगा। एक या दो मिनट प्रतीक्षा करें, फिर Refresh Balance पर दोबारा click करें। पहली refresh पर balance में बदलाव न होने का अर्थ यह नहीं है कि metering विफल हो गई।
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 enabled है।
  • metadata.tokens key meter के Over Property से match नहीं करती।
  • Customer का grant expire हो चुका है। Customer का credit history जाँचें।
क्या जाँचें:
  1. Products → Meters में meter खोलें और पुष्टि करें कि product attachment में linked credit name दिखाई दे रहा है।
  2. Meter का Events tab खोलें। Ingest किए गए events किसी भी deduction से पहले वहाँ दिखाई देते हैं।
  3. Customers में customer खोलें और Credits tab चुनें। Ledger entries एक या दो मिनट के भीतर दिखाई देती हैं।
संभावित कारण:
  • Customer ने checkout पूरा नहीं किया है। Credits केवल successful payment के बाद जारी होते हैं।
  • आप गलत customer_id से query कर रहे हैं। Dashboard से cus_ से शुरू होने वाली ID का उपयोग करें, अपनी database की ID का नहीं।
  • .env में मौजूद CREDIT_ENTITLEMENT_ID product से attached credit से match नहीं करता।
क्या जाँचें: Customers में customer खोलें और Credits tab चुनें। यदि कोई credits दिखाई नहीं देते, तो credit product से attached नहीं था या payment पूरा नहीं हुआ।
संभावित कारण:
  • Pro product’s credit attachment पर overage enabled नहीं है। Credit पर मौजूद setting केवल default है।
  • Customer Pro पर नहीं, Starter पर है।
  • Overage Limit 0 पर set है।
क्या जाँचें: Pro product edit करें, Entitlements में credit खोलें और पुष्टि करें कि Allow Overage enabled है तथा Price Per Unit 0.000005 (प्रति million tokens $5) है। Leading zeros जाँचें: यह field प्रति token की price लेती है, प्रति 1K tokens की नहीं।
संभावित कारण:
  • Body parsing order: express.json() ने express.raw() से पहले /webhooks/dodo पर run किया। SDK को parsed JSON नहीं, request के raw bytes चाहिए।
  • DODO_PAYMENTS_WEBHOOK_KEY में गलत signing secret है।
  • Reverse proxy request headers को rewrite कर रहा है।
क्या जाँचें: पुष्टि करें कि app.use('/webhooks/dodo', express.raw(...)) line, server.ts में app.use(express.json()) से पहले आती है।

सहायता चाहिए?

बधाई हो! आपने NeuralAPI के लिए Credit-Based Billing बना लिया है

अब NeuralAPI checkout से deduction तक credits में bill करता है:

Token Credit Entitlement

30-day expiry वाला reusable API Tokens credit, जिसे दोनों plans और top-up pack share करते हैं।

Tiered Plans, One Credit

Starter (10M tokens, hard limit) और Pro (40M tokens plus overage), दोनों को credit duplicate किए बिना per product configure किया गया है।

One-Time Top-Up Pack

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

Deduction Through a Meter

वास्तविक OpenAI token counts events के रूप में ingest किए जाते हैं और meter बिना किसी manual tracking के credits को FIFO के आधार पर deduct करता है।

Live Balance API

वर्तमान balance, जिसे SDK के माध्यम से read करके आपकी app में access नियंत्रित किया जा सकता है, usage दिखाया जा सकता है या customers को चेतावनी दी जा सकती है।

Verified Webhook Pipeline

Credit ledger events (credit.added, credit.deducted, credit.overage_charged) ऐसे handler के माध्यम से route किए जाते हैं जो SDK के Standard Webhooks helper से signatures verify करता है।
Production में जा रहे हैं? इन settings को मजबूत करें:
  • /credits/:customerId और /api/generate में authentication जोड़ें। वर्तमान रूप में कोई भी व्यक्ति इन्हें किसी भी customer ID के साथ call कर सकता है। Users को authenticate करें और server पर उनकी customer ID lookup करें।
  • Stable event_id values का उपयोग करें। Example में Date.now() और एक random string का उपयोग किया गया है। Production में अपनी request ID का उपयोग करें ताकि retries idempotent हों: Dodo Payments ऐसे event को ignore करता है जिसका event_id वह पहले ही ingest कर चुका है।
  • Customer-to-user mapping store करें। पहले checkout के बाद अपनी database में customer_id save करें, ताकि users को इसे manually paste न करना पड़े।
  • तय करें कि subscription समाप्त होने पर क्या होगा। Plan credits customer के ledger में issuance के 30 दिन बाद expire होने तक बने रहते हैं और top-up credits 365 दिनों तक valid रहते हैं। Tutorial का /api/generate केवल balance check करता है, subscription status नहीं, इसलिए cancelled customer अपने बचे हुए tokens का उपयोग कर सकता है। यह customer-friendly default है। अधिक सख्त access के लिए या तो subscription.cancelled webhook सुनें और subscription status के आधार पर /api/generate को gate करें, या (b) cancellation पर ledger API से unused plan credits debit करें। Debits उस grant से लिए जाते हैं जिसकी expiry सबसे पहले है, इसलिए 30-day plan credits 365-day top-up credits से पहले उपयोग होंगे।
  • Usage Billing dashboard monitor करें ताकि metering anomalies का जल्दी पता चल सके।

Credit-Based Billing Reference

Rollover, overage modes, ledger management और प्रत्येक credit API endpoint।

Credit Webhook Events

आपका server प्राप्त कर सकने वाले प्रत्येक credit event के payload schemas।
अंतिम संशोधन 26 सितंबर 2026