Skip to main content

Python SDK for working with TargetAI tokens

Project description

TargetAI Python SDK

Python SDK for working with TargetAI tokens. Provides simple tools for retrieving tokens from TOS backend and deploying token distribution endpoints.

Installation

pip install targetai

Quick Start

TargetAITokenClient - Token Retrieval

from targetai import TargetAITokenClient

# Simplest way - uses default URL https://app.targetai.ai
async def example_simple():
    async with TargetAITokenClient() as client:
        token_response = await client.get_token()
        print(f"Token: {token_response.token}")

# With API key
async def example_with_api_key():
    async with TargetAITokenClient(api_key="your-api-key") as client:
        token_response = await client.get_token()
        print(f"Token: {token_response.token}")

# With custom URL
async def example_custom_url():
    async with TargetAITokenClient("https://your-tos.example.com", api_key="your-api-key") as client:
        token_response = await client.get_token()
        print(f"Token: {token_response.token}")

TargetAITokenServer - Token Distribution Server

from targetai import TargetAITokenServer

# Simplest startup (uses default URL)
def run_server():
    server = TargetAITokenServer(port=8001)
    server.run()  # Blocking startup

# With API key for TOS backend
def run_server_with_key():
    server = TargetAITokenServer(api_key="your-tos-api-key", port=8001)
    server.run()

# With custom TOS backend URL
def run_server_custom():
    server = TargetAITokenServer(
        tos_base_url="https://your-tos.example.com",
        api_key="your-api-key",
        port=8001
    )
    server.run()

# Or asynchronously
async def run_server_async():
    async with TargetAITokenServer(port=8001) as server:
        await server.start()

After starting the server, the following endpoints are available:

  • POST /token - token retrieval
  • GET /health - health check
  • GET /docs - Swagger documentation

Using the /token endpoint

# Simple token request
curl -X POST http://localhost:8001/token

# Response:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

API Reference

TargetAITokenClient

class TargetAITokenClient:
    def __init__(self, 
                 tos_base_url: str = "https://app.targetai.ai",
                 api_key: Optional[str] = None)
    async def get_token(self) -> TokenResponse
    async def close(self)

TargetAITokenServer

class TargetAITokenServer:
    def __init__(self, 
                 tos_base_url: str = "https://app.targetai.ai", 
                 api_key: Optional[str] = None,
                 host: str = "0.0.0.0",
                 port: int = 8001)
    async def start(self)
    async def stop(self)
    def run(self)  # synchronous version

Data Schemas

class TokenResponse:
    token: str

Error Handling

from targetai import TargetAITokenClient, TargetAITokenClientError

try:
    async with TargetAITokenClient() as client:  # Uses default URL
        token = await client.get_token()
except TargetAITokenClientError as e:
    print(f"Error: {e}")

Possible errors:

  • TargetAITokenClientError - base client error
  • TargetAITokenServerError - base server error

Requirements

  • Python 3.8+
  • aiohttp >= 3.8.0
  • pydantic >= 2.0.0
  • fastapi >= 0.100.0
  • uvicorn >= 0.20.0

License

MIT

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

targetai-0.0.4.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

targetai-0.0.4-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file targetai-0.0.4.tar.gz.

File metadata

  • Download URL: targetai-0.0.4.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.9

File hashes

Hashes for targetai-0.0.4.tar.gz
Algorithm Hash digest
SHA256 7092e0f9ae611584b00f1a697deffcf8a6e06b2ef22b738491a057f917ccf553
MD5 5bc23b14f19187f982fbc26f7443dde3
BLAKE2b-256 3e2c920701ab41ff8b48ec30b63f667a83e223bb706dd5b4130227a645d6593c

See more details on using hashes here.

File details

Details for the file targetai-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: targetai-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.9

File hashes

Hashes for targetai-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a3d1be3be0b3e6c1d8e7ed498288280bca421c1b507f1aae0105f852a2dae1e4
MD5 2d9834000303ade94e6da89877c7c1a1
BLAKE2b-256 9d45af1a0182c00f6c6fb56e5cac0bfe9e118ef091b05676a5e351dd9e84ae44

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