---
title: "Python Scripting"
description: "Use the TraceRTM Python SDK in CLI scripts"
keywords: [cli, python, scripting, sdk, automation]
---

# Python Scripting

Use the TraceRTM Python SDK in your CLI scripts for advanced automation.

## Using Python SDK

### Basic Script

```python
#!/usr/bin/env python3
from tracertm import TraceRTM
import os

client = TraceRTM(api_key=os.getenv("TRACERTM_API_KEY"))

# List projects
projects = client.projects.list()
for project in projects:
    print(f"{project.id}: {project.name}")
```

### Advanced Script

```python
#!/usr/bin/env python3
from tracertm import TraceRTM
import sys

def main():
    client = TraceRTM(api_key=sys.argv[1])
    project_id = sys.argv[2]
    
    # Query items
    items = client.query_items(
        view="requirements",
        status="active",
        limit=100
    )
    
    # Process items
    for item in items:
        print(f"Processing: {item['title']}")
        # Your logic here

if __name__ == "__main__":
    main()
```

## Combining CLI and SDK

```python
import subprocess
import json

# Use CLI for simple operations
result = subprocess.run(
    ["trace", "project", "list", "--output", "json"],
    capture_output=True,
    text=True
)
projects = json.loads(result.stdout)

# Use SDK for complex operations
from tracertm import TraceRTM
client = TraceRTM()
for project in projects:
    items = client.query_items(project_id=project["id"])
    # Process with SDK
```

## Related Documentation

- [Scripting](./04-scripting) - Scripting basics
- [Python SDK](/docs/api-reference/sdks/python) - SDK documentation
