Skip to main content

Python Quickstart

Add verifiable attestations to any Python application in under 5 minutes.

Installation

pip install treeship-sdk

Setup

Get your API key from treeship.dev and set it as an environment variable:
export TREESHIP_API_KEY=your-api-key
export TREESHIP_AGENT=my-agent

Basic Usage

from treeship_sdk import Treeship

# Initialize the client
ts = Treeship()

# Create an attestation
result = ts.attest(
    action="Processed customer request #12345",
    inputs_hash=ts.hash({"customer_id": "cust_123", "request": "refund"})
)

print(f"Attestation created: {result.id}")
print(f"Verify at: {result.verify_url}")

With Explicit Configuration

from treeship_sdk import Treeship

ts = Treeship(
    api_key="your-api-key",
    agent="my-agent"
)

result = ts.attest(
    action="Made trading decision",
    inputs_hash=ts.hash(market_data),
    metadata={"confidence": 0.95, "model_version": "v2.1"}
)

Hashing Inputs

Always hash your inputs to create a fingerprint without exposing sensitive data:
import json

# Hash any data structure
inputs_hash = ts.hash({
    "user_id": user.id,
    "query": user_query,
    "context": relevant_context
})

# Or hash a string directly
inputs_hash = ts.hash("simple string input")

# Or hash bytes
inputs_hash = ts.hash(file_contents)

Verifying Attestations

# Verify an attestation
result = ts.verify("attestation-id-here")

if result["valid"]:
    print("✓ Attestation is valid")
    print(f"  Action: {result['action']}")
    print(f"  Time: {result['timestamp']}")
else:
    print("✗ Attestation is INVALID")

Error Handling

Attestation failures should never block your agent:
try:
    result = ts.attest(action="...", inputs_hash="...")
    log.info(f"Attested: {result.url}")
except Exception as e:
    log.warning(f"Attestation failed (non-blocking): {e}")
    # Continue with agent operation

Full Example

from treeship_sdk import Treeship

class MyAgent:
    def __init__(self):
        self.ts = Treeship()
    
    def process_request(self, request):
        # Do the actual work
        result = self._internal_process(request)
        
        # Attest to what happened
        try:
            attestation = self.ts.attest(
                action=f"Processed {request.type} request",
                inputs_hash=self.ts.hash(request.to_dict()),
                metadata={
                    "request_id": request.id,
                    "outcome": result.status
                }
            )
            result.attestation_url = attestation.verify_url
        except Exception:
            pass  # Don't block on attestation failure
        
        return result

Next Steps