Skip to main content

Python SDK

The LabTrace Python SDK provides a comprehensive interface for integrating blockchain-powered file management into Python applications, data science workflows, and backend services.

Installation

Install the SDK using pip:

Quick Start

Authentication

Basic File Operations

Client Configuration

File Management

Upload Files

Download Files

List Files

Delete Files

Project Management

Get Projects

File Verification

Verify File Content

Error Handling

Exception Handling

Automatic Token Refresh

The SDK automatically handles JWT token expiration and refreshes tokens when needed:

Advanced Features

Custom Request Headers

Bulk Operations

API Reference

Client Methods

Authentication

  • Client(username, password) - Initialize client with credentials
  • get_user_id() - Get current user ID from JWT token

Project Operations

  • get_projects(**filters) - Get all projects with optional filters
  • get_project(project_id) - Get specific project details

Public File Operations

  • upload_public_file(project_id, content, file_type, name, label, **kwargs) - Upload public file
  • get_public_project_files(project_id) - Get all public files in project
  • get_public_file(project_id, file_id, save_path) - Download public file
  • get_public_file_certificate(project_id, file_id, save_path, raw=False) - Download certificate
  • delete_public_file(project_id, file_id, user_id, reason) - Delete public file

Private File Operations

  • upload_private_file(project_id, content, file_type, name, label, **kwargs) - Upload private file
  • get_private_project_files(project_id) - Get all private files in project
  • get_private_file_certificate(project_id, file_id, save_path, raw=False) - Download certificate
  • delete_private_file(project_id, file_id, user_id, reason) - Delete private file

File Verification

  • verify_file_content(content, name) - Check if file already exists

Response Formats

All methods return Python dictionaries with the response data. The SDK automatically handles JSON parsing and error responses.

Environment Variables

The SDK supports the following environment variables:
  • LABTRACE_URL - API base URL (default: https://api.labtrace.io)
  • LABTRACE_USERNAME - Default username for authentication
  • LABTRACE_PASSWORD - Default password for authentication

Best Practices

Security

  • Store credentials securely (use environment variables or secure vaults)
  • Use different accounts for different environments (dev/staging/prod)
  • Regularly rotate passwords and update credentials

Performance

  • Reuse client instances when possible
  • Handle large files in chunks for better memory usage
  • Use appropriate file types (primary vs secondary) for organization

Error Handling

  • Always wrap SDK calls in try-catch blocks
  • Check response status codes for specific error handling
  • Implement retry logic for transient network errors

Examples

Complete Workflow Example