Python Quick Start

Get started with the Autoken Python client library. Supports both synchronous and asynchronous usage.

Installation

pip install autoken

Configuration

Set your API key as an environment variable (recommended):

export AUTOKEN_API_KEY="sk_your_api_key_here"

Or pass it directly when creating the client:

from autoken import Autoken

client = Autoken(api_key="sk_your_api_key")

Synchronous Usage

Execute a Task

from autoken import Autoken

client = Autoken()

# Execute a task
result = client.task("translate-text", {
    "text": "Hello world",
    "language": "es"
})

print(result.output["translatedText"])  # "Hola mundo"
print(result.token_cost)  # Cost in tokens

List Available Tasks

tasks = client.list_tasks()

for task in tasks:
    print(f"{task.name}: {task.description}")

Get Task Details

task = client.get_task("translate-text")

print(task.name)
print(task.description)
print(task.input)   # Input schema
print(task.output)  # Output schema

Asynchronous Usage

import asyncio
from autoken import AsyncAutoken

async def main():
    client = AsyncAutoken()
    
    result = await client.task("translate-text", {
        "text": "Hello world",
        "language": "es"
    })
    
    print(result.output["translatedText"])

asyncio.run(main())

Working with Files (Artifacts)

Upload and Use a File

# Read and upload a file
with open("audio.mp3", "rb") as f:
    artifact = client.upload_artifact(
        f.read(),
        filename="audio.mp3",
        mime_type="audio/mpeg"
    )

# Use the artifact in a task
result = client.task("transcribe-audio", {
    "artifactHash": artifact.content_hash
})

print(result.output["text"])

Download an Artifact

# Generate audio
result = client.task("text-to-speech", {
    "text": "Hello world",
    "voice": "alloy"
})

# Download the generated audio
audio_data = client.download_artifact(result.output["artifactHash"])

with open("output.mp3", "wb") as f:
    f.write(audio_data)

Error Handling

from autoken import Autoken, AutokenError

client = Autoken()

try:
    result = client.task("translate-text", {
        "text": "Hello",
        "language": "es"
    })
except AutokenError as e:
    print(f"Autoken error: {e.message}")
    print(f"Status code: {e.status_code}")

Token Management

Check Balance

balance = client.get_balance()
print(f"Balance: {balance.balance}")

View Transaction History

transactions = client.get_transactions()

for tx in transactions:
    print(f"{tx.type}: {tx.amount} - {tx.description}")

Execution History

# Get execution history
history = client.get_history()

for execution in history:
    print(f"{execution.task_name}: {execution.status}")

# Get a specific execution
execution = client.get_execution("exec_abc123")
print(execution.output)

Complete Example

from autoken import Autoken

def main():
    client = Autoken()

    # Check balance
    balance = client.get_balance()
    print(f"Current balance: {balance.balance}")

    # Translate text
    translation = client.task("translate-text", {
        "text": "Hello, how are you?",
        "language": "fr"
    })
    print(f"Translation: {translation.output['translatedText']}")

    # Generate speech
    speech = client.task("text-to-speech", {
        "text": translation.output["translatedText"],
        "voice": "alloy"
    })

    # Download and save the audio
    audio = client.download_artifact(speech.output["artifactHash"])
    with open("greeting.mp3", "wb") as f:
        f.write(audio)
    print("Audio saved to greeting.mp3")

if __name__ == "__main__":
    main()