Python Casino API Integration — Django/Flask Guide with Callback Handler (2026)

Updated October 2026 · 8 min read

Python shops run plenty of real-money platforms — betting back offices, bonus engines, wallet services — and the game layer is usually the last missing piece. This guide wires a game aggregator (GamingAPI at casinoapi.shop, 16,000+ games, 150+ studios) into a Django or Flask app: catalog sync, server-side launch, and a callback view that keeps the seamless wallet correct.

1. Credentials and prerequisites

  • API_TOKEN — sent with every request (keep it server-side).
  • API_SECRET — verifies the X-Signature HMAC on incoming callbacks.
  • One fixed play currency for the account (e.g. BDT, USD).
  • Your server IP whitelisted and an HTTPS callback URL registered in the client panel.

A free sandbox key gives you the full flow without real money — build against it first.

2. Catalog sync (management command or cron)

import requests

BASE = "https://casinoapi.shop/api/public/v1"
TOKEN = "YOUR_TOKEN"

def sync_games():
    offset, games = 0, []
    while True:
        r = requests.get(f"{BASE}/games",
                         params={"token": TOKEN, "limit": 500, "offset": offset},
                         timeout=30).json()
        if r.get("code") != 0:
            break
        batch = r["data"]["games"] or []
        games += batch
        if len(batch) < 500:
            break
        offset += len(batch)

    # Upsert into your own Game table (external_uid is the launch key).
    return games

3. Launch a game (Django view example)

import requests
from django.http import JsonResponse
from django.views.decorators.http import require_POST

@require_POST
def launch(request):
    body = json.loads(request.body)
    user_id = request.user.id          # YOUR authentication
    game_uid = body["game_uid"]        # external_uid from the catalog
    balance = Wallet.objects.get(user_id=user_id).balance

    r = requests.post(f"{BASE}/launch", json={
        "token": TOKEN,
        "user_id": str(user_id),       # digits only, unique per player
        "balance": float(balance),
        "game_uid": game_uid,
        "currency_code": "BDT",
        "language": "en",
        "return": "https://your-casino.com/lobby",
    }, timeout=30).json()

    # data.url -> iframe or redirect; data.session_id -> store for support/exit sync
    return JsonResponse({"url": r["data"]["url"], "session_id": r["data"]["session_id"]})

4. The callback view (the part that must be right)

The provider POSTs verify_player before launch, then one round per bet and win. Every request carries X-Signature: HMAC-SHA256(raw_body, API_SECRET). Three hard rules: verify the signature, treat callback_id as idempotent, and apply the delta (wallet − bet + win) atomically — never SET the balance from the payload.

import hashlib, hmac, json
from django.http import JsonResponse, HttpResponse
from django.db import transaction
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def callback(request):
    raw = request.body
    expected = hmac.new(b"YOUR_API_SECRET", raw, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, request.headers.get("X-Signature", "")):
        return HttpResponse(status=401)

    data = json.loads(raw)

    if data.get("event") == "verify_player":
        bal = Wallet.objects.get(user_id=data["member_account"]).balance
        return JsonResponse({"code": 0, "msg": "Success", "balance": float(bal)})

    serial = data.get("callback_id")
    with transaction.atomic():
        # Idempotency: get_or_create so a retry returns success without moving money twice.
        round_row, created = Round.objects.get_or_create(
            serial=serial,
            defaults={
                "player": data["member_account"],
                "bet": data.get("bet_amount", 0),
                "win": data.get("win_amount", 0),
                "game_uid": data.get("game_uid"),
            },
        )
        if created:
            wallet = Wallet.objects.select_for_update().get(user_id=data["member_account"])
            wallet.balance = round(wallet.balance
                                   - float(data.get("bet_amount", 0))
                                   + float(data.get("win_amount", 0)), 2)
            wallet.save()

    return JsonResponse({"code": 0, "balance": float(wallet.balance)})

(In Flask the same logic is a @app.route("/casino/callback", methods=["POST"]) view with SELECT ... FOR UPDATE inside your transaction.)

5. Launch checklist

  • Respond HTTP 200 to callbacks in under a second; latency causes retries and stuck balances.
  • Log every round row — serial, bet, win, player — for support and reconciliation.
  • Use the exit-sync endpoint (/session?user_id=...) to verify balances on return; never re-credit there.
  • Test the full flow with the sandbox key before switching to the live token.

Pricing at GamingAPI is $350 one-time setup plus 10% of positive GGR — the same contract as the PHP and Node.js flows, so your stack choice never locks you out of new studios. See the full casino API guide and the pre-go-live checklist.