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.5.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.5-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for targetai-0.0.5.tar.gz
Algorithm Hash digest
SHA256 565d5923b81969e9dc93367602ee1f3fd143f325bd3bd13005a264b233c38d05
MD5 800f3214c93c8a2f0381b71e5f690a9b
BLAKE2b-256 b4e1d42f5255c1d26f555825f7d65066a4c9cc3c47016657f46e747a940ca1e8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for targetai-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2ab0843c8f6eb0fecfddfa71d42e69b961020796d75fd66d43ed03436e771065
MD5 2fe16259d77d8bcc2810bffcef8bba40
BLAKE2b-256 1cacb09976795061a0673acef445a597e1ac143c56eacfb7050fb74e814b409b

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