Python SDK Examples
Real-world examples for securing AI agents in e-commerce applications.
Shopping Cart Authorization
Verify an AI agent before allowing it to add items to a shopping cart.
from zeroproof import ZeroProof, ZeroProofError
client = ZeroProof(api_key="zkp_your_api_key")
# Create verification challenge
challenge = client.create_challenge(
agent_id="shopping-assistant-v1",
action="add_to_cart",
context={"item_id": "laptop-123", "price": 999.99}
)
# Generate proof
proof = generate_proof(challenge)
result = client.verify_proof(challenge.challenge_id, proof)
if result.verified and result.confidence > 0.90:
cart_id = add_to_cart("laptop-123", 999.99)
print(f"Added: {cart_id}")
else:
print("Failed")
Secure Payment Processing
Require high-confidence verification (95%+) before processing payments.
from zeroproof import ZeroProof
class SecureCheckoutAgent:
def __init__(self, api_key: str):
self.client = ZeroProof(api_key=api_key)
self.min_confidence = 0.95
def process_payment(self, cart_id: str, amount: float):
challenge = self.client.create_challenge(
agent_id="checkout-agent-v2",
action="process_payment",
context={"cart_id": cart_id, "amount": amount}
)
result = self.client.verify_proof(challenge.challenge_id, proof)
if result.verified and result.confidence >= self.min_confidence:
return {"success": True}
return {"success": False}
checkout = SecureCheckoutAgent("zkp_key")
result = checkout.process_payment("CART-123", 2499.99)
Best Practices
Resource Management
Always use context managers (with
statements) for automatic cleanup of HTTP sessions.
Confidence Thresholds
Set higher confidence thresholds for sensitive operations. Use 95%+ for payments, 90%+ for purchases, 85%+ for other actions.
Context Data
Include relevant context in challenges for better audit trails and debugging.
Error Handling
Handle errors gracefully with try/except blocks and log all verification attempts.
API Key Security
Store API keys securely using environment variables or secret managers. Never commit keys to version control.
Monitoring
Monitor verification patterns, confidence scores, and failure rates to detect anomalies.
Complete Example
For a complete working example with all features, check out the shopping agent demo in the Python SDK repository.
View Full Example