Skip to main content

Python SDK for KNAI Wallet provider integration

Project description

KNAI Provider SDK

A Python SDK for integrating with KNAI Wallet, enabling AI providers to implement session-based, per-use charging. KNAI Wallet is an API-first fintech product that allows providers to monetize AI services through a secure, session-based payment system where users authorize spending via session tokens.

Installation

pip install knai-provider

Usage

from knai import KNAIProvider, SessionExpiredError, InsufficientBalanceError

# Initialize the client with your credentials
provider = KNAIProvider(
    provider_api_key="your-api-key",
    provider_slug="your-provider-slug",
)

# Wait for an active session from a user
session = provider.wait_for_session(provider_username="user123")
session_token = session["session_token"]

# Charge for usage
try:
    result = provider.charge(
        session_token=session_token,
        amount_tokens=1500,
        model="gpt-4",
    )
    print(f"Charge successful: {result}")
except SessionExpiredError:
    print("Session has expired, request a new session")
except InsufficientBalanceError:
    print("User has insufficient balance")

API Reference

KNAIProvider

Constructor

KNAIProvider(
    provider_api_key: str,
    provider_slug: str,
    base_url: str = "https://api.conx.ai"
)
  • provider_api_key: Your provider API key for authentication
  • provider_slug: Your unique provider identifier
  • base_url: Optional API base URL (defaults to production)

Methods

wait_for_session(provider_username: str, poll_interval: float = 1.5) -> dict

Polls for an active provider session. Blocks until a session becomes active.

  • provider_username: The username to poll session status for
  • poll_interval: Time in seconds between poll attempts (default: 1.5)
  • Returns: Session payload dict when status becomes "active"
charge(session_token: str, amount_tokens: int, model: str) -> dict

Charges wallet usage for the current session.

  • session_token: The active session token authorizing the charge
  • amount_tokens: Number of tokens to charge
  • model: The model identifier being used
  • Returns: Parsed JSON response from the charge API

Exceptions

  • KNAIError: Base exception for all SDK errors
  • SessionExpiredError: Session has expired or is no longer valid
  • InsufficientBalanceError: Wallet has insufficient balance
  • InvalidAPIKeyError: Provider API key is invalid

Security Warning

⚠️ This SDK is designed for server-side use only.

  • Never expose your provider_api_key in client-side code
  • Never use this SDK in browser environments
  • Store API keys securely using environment variables or secret management
  • Session tokens should only be handled server-side
import os

# Load API key from environment
provider = KNAIProvider(
    provider_api_key=os.environ["KNAI_API_KEY"],
    provider_slug=os.environ["KNAI_PROVIDER_SLUG"],
)

License

MIT License - see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

knai_provider-0.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

knai_provider-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file knai_provider-0.1.0.tar.gz.

File metadata

  • Download URL: knai_provider-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for knai_provider-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c1003819485a49bbccddbe8beb407b7525b31a5b2828692b48aad9f7302e6726
MD5 a1b89ac0f8b8cef892339f6e2d5fc932
BLAKE2b-256 fe51a82a3029f20d8b06ea67be2588935d90f665305564327974036000311406

See more details on using hashes here.

File details

Details for the file knai_provider-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: knai_provider-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for knai_provider-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30754c295eed5c9d331aad74cd6c3f2d073373e733d067c649b7280a1caa3bc7
MD5 3ff02beedd3a329d8a7108c1edeff93b
BLAKE2b-256 4d2b0d4d8d557a1b742fb0326b1c663da9f68de3e51b59652cc312f07c1bbcf3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page