Python SDK
Python is the primary automation language for many MolTrace users, so every example on this page must be complete, runnable, and verified against the sandbox API before customer release.
Installation
Section titled “Installation”pip install moltraceAuthentication
Section titled “Authentication”from moltrace import MolTrace
client = MolTrace(api_key="mt_sk_live_YOUR_KEY")For production scripts, prefer environment-based configuration:
from moltrace import MolTrace
client = MolTrace() # reads MOLTRACE_API_KEYAnalyse a spectrum
Section titled “Analyse a spectrum”analysis = client.analyses.create( fid_path="./data/batch_001/fid", acqus_path="./data/batch_001/acqus", nucleus="1H", instrument="bruker",)
result = client.analyses.wait(analysis.id, timeout=120)
print(f"Confidence: {result.confidence}%")print(f"Peaks detected: {len(result.peaks)}")print(f"Impurities: {len([p for p in result.peaks if p.category == 'impurity'])}")Download a regulatory report
Section titled “Download a regulatory report”client.reports.download( analysis_id=result.id, format="pdf", jurisdiction="FDA", # FDA, EMA, PMDA, or Health Canada after verification output_path="./reports/batch_001_report.pdf",)
print("Report saved.")Examples to add before release
Section titled “Examples to add before release”- Upload and process one Bruker FID.
- Poll an analysis until complete.
- Retrieve the peak table.
- Download a report.
- Batch upload a directory of spectra with retry handling.
Common errors
Section titled “Common errors”| Error | Likely cause | Fix |
|---|---|---|
Unauthorized | Missing or invalid API key. | Rotate the key and verify MOLTRACE_API_KEY. |
ValidationError | Missing acqus file or invalid enum. | Upload complete instrument metadata and use documented enum values. |
RateLimitExceeded | Too many concurrent uploads. | Reduce parallel jobs or request a higher limit. |