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.3.tar.gz (6.5 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.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: targetai-0.0.3.tar.gz
  • Upload date:
  • Size: 6.5 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.3.tar.gz
Algorithm Hash digest
SHA256 c89e12e4dba5c904c11c0633aaa2134f08f4e4d2b124283f94d6037a39ad2476
MD5 1e19e9ba15b2049a59002de82d6fe8a1
BLAKE2b-256 e346087087def5eee057da0e336b91b82fb7cac67237d4763a67732e41488c43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: targetai-0.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 59c284b004c4e1af382d7bae00a1868ad565e500bcaa887067fea8f10e3bc8a3
MD5 f09764f871543e126b8ab410b65581e6
BLAKE2b-256 e2320ea8f79ee1f57ab563bed4e94d281abf4de3460a65fa6bb3b4280f1dc01f

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