मुख्य सामग्री पर जाएं

Documentation Index

Fetch the complete documentation index at: https://docs.dodopayments.com/llms.txt

Use this file to discover all available pages before exploring further.

Python SDK Dodo Payments API के लिए एक Pythonic इंटरफेस प्रदान करता है, जो अनुरोधों और प्रतिक्रियाओं के लिए प्रकार परिभाषाएँ के साथ समकालिक और असमकालिक क्लाइंट दोनों को प्रदान करता है। यह Python 3.9+ का समर्थन करता है और इसमें व्यापक परीक्षण कवरेज शामिल है।

स्थापना

SDK को pip का उपयोग करके स्थापित करें:
pip install dodopayments
aiohttp के साथ बेहतर असमकालिक प्रदर्शन के लिए:
pip install dodopayments[aiohttp]
SDK को Python 3.9 या उससे ऊपर की आवश्यकता होती है। सर्वोत्तम अनुभव और सुरक्षा अपडेट्स के लिए हम Python का नवीनतम स्थिर संस्करण उपयोग करने की सलाह देते हैं।

त्वरित प्रारंभ

समकालिक क्लाइंट

import os
from dodopayments import DodoPayments

client = DodoPayments(
    bearer_token=os.environ.get("DODO_PAYMENTS_API_KEY"),  # This is the default and can be omitted
    environment="test_mode",  # defaults to "live_mode"
)

checkout_session_response = client.checkout_sessions.create(
    product_cart=[
        {
            "product_id": "product_id",
            "quantity": 1
        }
    ],
)
print(checkout_session_response.session_id)

असमकालिक क्लाइंट

import os
import asyncio
from dodopayments import AsyncDodoPayments

client = AsyncDodoPayments(
    bearer_token=os.environ.get("DODO_PAYMENTS_API_KEY"),
    environment="test_mode",
)

async def main() -> None:
    checkout_session_response = await client.checkout_sessions.create(
        product_cart=[
            {
                "product_id": "product_id",
                "quantity": 1,
            }
        ],
    )
    print(checkout_session_response.session_id)

asyncio.run(main())
हमेशा अपने API कुंजियों को पर्यावरण चर का उपयोग करके सुरक्षित रूप से संग्रहीत करें। इन्हें कभी भी संस्करण नियंत्रण में कमिट न करें।

मुख्य विशेषताएँ

Pythonic Interface

PEP 8 दिशानिर्देशों और Python रीतियों का पालन करने वाला साफ़-सुथरा, स्वाभाविक Python कोड

Async/Await

asyncio के साथ पूर्ण समर्थन और वैकल्पिक aiohttp एकीकरण

Type Hints

बेहतर IDE समर्थन और mypy के साथ प्रकार जाँच के लिए पूर्ण प्रकार संकेत

Auto-Pagination

सरल पुनरावृति के साथ सूची प्रतिक्रियाओं के लिए स्वचालित पेजिनेशन

कॉन्फ़िगरेशन

पर्यावरण चर

पर्यावरण चर का उपयोग करके कॉन्फ़िगर करें:
.env
DODO_PAYMENTS_API_KEY=your_api_key_here

टाइमआउट

वैश्विक रूप से या प्रति अनुरोध अनुरोध टाइमआउट कॉन्फ़िगर करें:
import httpx
from dodopayments import DodoPayments

# Configure default for all requests (default is 1 minute)
client = DodoPayments(
    timeout=20.0,  # 20 seconds
)

# More granular control
client = DodoPayments(
    timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0),
)

# Override per-request
client.with_options(timeout=5.0).checkout_sessions.create(
    product_cart=[
        {
            "product_id": "product_id",
            "quantity": 1,
        }
    ],
)

पुनः प्रयास

स्वचालित पुनः प्रयास व्यवहार कॉन्फ़िगर करें:
from dodopayments import DodoPayments

# Configure default for all requests (default is 2)
client = DodoPayments(
    max_retries=0,  # disable retries
)

# Override per-request
client.with_options(max_retries=5).checkout_sessions.create(
    product_cart=[
        {
            "product_id": "product_id",
            "quantity": 1,
        }
    ],
)

सामान्य संचालन

चेकआउट सत्र बनाएँ

एक चेकआउट सत्र उत्पन्न करें:
session = client.checkout_sessions.create(
    product_cart=[
        {
            "product_id": "prod_123",
            "quantity": 1
        }
    ],
    return_url="https://yourdomain.com/return"
)

print(f"Checkout URL: {session.url}")

ग्राहकों का प्रबंधन करें

ग्राहक जानकारी बनाएँ और पुनः प्राप्त करें:
# Create a customer
customer = client.customers.create(
    email="customer@example.com",
    name="John Doe",
    metadata={
        "user_id": "12345"
    }
)

# Retrieve customer
customer = client.customers.retrieve("cus_123")
print(f"Customer: {customer.name} ({customer.email})")

सदस्यताओं को संभालें

आवर्ती सदस्यताएँ बनाएँ और प्रबंधित करें:
# Create a subscription
subscription = client.subscriptions.create(
    billing={
        "country": "US",
        "city": "San Francisco",
        "state": "CA",
        "street": "1 Market St",
        "zipcode": "94105",
    },
    customer={"customer_id": "cus_123"},  # or {"email": "...", "name": "..."} for a new customer
    product_id="pdt_456",
    quantity=1,
)

# Charge an on-demand subscription
# product_price is in the lowest currency denomination (e.g., 2500 = $25.00 USD)
charge_response = client.subscriptions.charge(
    subscription_id=subscription.subscription_id,
    product_price=2500,
)

# Retrieve usage history (for metered subscriptions)
usage_history = client.subscriptions.retrieve_usage_history(
    subscription_id=subscription.subscription_id,
    start_date="2024-01-01T00:00:00Z",
)
billing कम से कम दो-अक्षर का ISO देश कोड आवश्यक है। customer या तो मौजूदा ग्राहक को संलग्न करने के लिए {"customer_id": ...} या एक नया बनाने के लिए {"email": ..., "name": ...} को स्वीकार करता है। product_price सबसे छोटे मुद्रा संप्रदाय में है।

उपयोग-आधारित बिलिंग

उपयोग इवेंट संकलित करें

उपयोग-आधारित बिलिंग के लिए कस्टम इवेंट को ट्रैक करें:
response = client.usage_events.ingest(
    events=[
        {
            "event_id": "api_call_12345",
            "customer_id": "cus_abc123",
            "event_name": "api_request",
            "timestamp": "2024-01-15T10:30:00Z",
            "metadata": {
                "endpoint": "/api/v1/users",
                "method": "GET",
                "tokens_used": "150"
            }
        }
    ]
)

इवेंट सूचीबद्ध और पुनः प्राप्त करें

# Get a specific event
event = client.usage_events.retrieve("api_call_12345")

# List events with filtering
events = client.usage_events.list(
    customer_id="cus_abc123",
    event_name="api_request",
    limit=20
)

for event in events.data:
    print(f"Event: {event.event_id} at {event.timestamp}")

पेजिनेशन

ऑटो-पेजिनेशन

सभी आइटमों के माध्यम से स्वत: पुनरावृत्त करें:
from dodopayments import DodoPayments

client = DodoPayments()
all_payments = []

# Automatically fetches more pages as needed
for payment in client.payments.list():
    all_payments.append(payment)
print(all_payments)

Async पेजिनेशन

import asyncio
from dodopayments import AsyncDodoPayments

client = AsyncDodoPayments()

async def main() -> None:
    all_payments = []
    # Iterate through items across all pages
    async for payment in client.payments.list():
        all_payments.append(payment)
    print(all_payments)

asyncio.run(main())

मैनुअल पेजिनेशन

पेजिनेशन पर अधिक नियंत्रण के लिए:
# Access items from current page
first_page = client.payments.list()
for payment in first_page.items:
    print(payment.brand_id)

# Check for more pages
if first_page.has_next_page():
    next_page = first_page.get_next_page()
    print(f"Fetched {len(next_page.items)} more items")

HTTP क्लाइंट कॉन्फ़िगरेशन

आधारभूत httpx क्लाइंट को अनुकूलित करें:
import httpx
from dodopayments import DodoPayments, DefaultHttpxClient

client = DodoPayments(
    base_url="http://my.test.server.example.com:8083",
    http_client=DefaultHttpxClient(
        proxy="http://my.test.proxy.example.com",
        transport=httpx.HTTPTransport(local_address="0.0.0.0"),
    ),
)

Async के साथ aiohttp

बेहतर async प्रदर्शन के लिए aiohttp का उपयोग करें:
import asyncio
from dodopayments import DefaultAioHttpClient
from dodopayments import AsyncDodoPayments

async def main() -> None:
    async with AsyncDodoPayments(
        bearer_token="My Bearer Token",
        http_client=DefaultAioHttpClient(),
    ) as client:
        checkout_session_response = await client.checkout_sessions.create(
            product_cart=[
                {
                    "product_id": "product_id",
                    "quantity": 1,
                }
            ],
        )
        print(checkout_session_response.session_id)

asyncio.run(main())

लॉगिंग

पर्यावरण वेरिएबल सेट करके लॉगिंग सक्षम करें:
export DODO_PAYMENTS_LOG=info
या डिबग-स्तर लॉगिंग के लिए:
export DODO_PAYMENTS_LOG=debug

फ्रेमवर्क इंटीग्रेशन

FastAPI

from fastapi import FastAPI, HTTPException
from dodopayments import AsyncDodoPayments
from pydantic import BaseModel
import os

app = FastAPI()
dodo = AsyncDodoPayments(bearer_token=os.getenv("DODO_API_KEY"))

class CheckoutRequest(BaseModel):
    product_id: str
    quantity: int

@app.post("/create-checkout")
async def create_checkout(request: CheckoutRequest):
    try:
        session = await dodo.checkout_sessions.create(
            product_cart=[{
                "product_id": request.product_id,
                "quantity": request.quantity
            }],
            return_url="https://yourdomain.com/return"
        )
        return {"checkout_url": session.url}
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

Django

from django.http import JsonResponse
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
from dodopayments import DodoPayments
import os
import json

client = DodoPayments(bearer_token=os.getenv("DODO_PAYMENTS_API_KEY"))

@csrf_exempt
@require_POST
def create_checkout(request):
    try:
        data = json.loads(request.body)
        session = client.checkout_sessions.create(
            product_cart=[{
                "product_id": data.get("product_id"),
                "quantity": data.get("quantity", 1)
            }],
            return_url="https://yourdomain.com/return"
        )
        return JsonResponse({
            "status": "success",
            "checkout_url": session.url,
            "session_id": session.session_id
        })
    except Exception as e:
        return JsonResponse({
            "status": "error",
            "message": str(e)
        }, status=400)

संसाधन

GitHub Repository

स्रोत कोड देखें और योगदान दें

API Reference

पूर्ण API प्रलेखन

Discord Community

सहायता प्राप्त करें और डेवलपर्स से जुड़ें

Report Issues

बग रिपोर्ट करें या फीचर का अनुरोध करें

समर्थन

Python SDK के साथ सहायता चाहिए?

योगदान

हम योगदानों का स्वागत करते हैं! प्रारंभ करने के लिए योगदान दिशानिर्देश देखें।
Last modified on May 14, 2026