# Quick Start Guide - n8n-nodes-schwab

## Installation

```bash
# In n8n UI
Settings → Community Nodes → Install → n8n-nodes-schwab

# Or via command line
npm install n8n-nodes-schwab
# Then restart n8n
```

## Setup Credentials (One-time)

1. **Get Schwab API Keys**:
   - Visit https://developer.schwab.com/
   - Create an app
   - Note your Client ID and Client Secret

2. **Get Authorization Code**:
   - Visit: `https://api.schwabapi.com/v1/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&response_type=code`
   - Login and authorize
   - Copy the code from the redirect URL

3. **In n8n**:
   - Create new Schwab API credentials
   - Enter your Client ID, Client Secret, Authorization Code, and Redirect URI
   - Test and save

## Example Workflows

### 1. Get All Account Balances

```
Trigger (Schedule/Manual)
  ↓
Schwab Node
  Resource: Accounts
  Operation: Get Accounts
  Fields: positions (optional)
  ↓
Do something with account data
```

### 2. Place a Market Order

```
Trigger (Webhook/Manual)
  ↓
Schwab Node
  Resource: Orders
  Operation: Place Order
  Account Number: {{$json.accountNumber}}
  Order Type: MARKET
  Quantity: 100
  Symbol: AAPL
  Instruction: BUY
  ↓
Notification (email/slack)
```

### 3. Monitor Transactions

```
Schedule Node (every hour)
  ↓
Schwab Node
  Resource: Transactions
  Operation: Get Transactions By Path Param
  Account Number: {{$json.accountNumber}}
  Start Date: {{$now.minus({hours: 1}).toISO()}}
  End Date: {{$now.toISO()}}
  ↓
Filter Node (new transactions only)
  ↓
Send notification
```

### 4. Cancel All Pending Orders

```
Manual Trigger
  ↓
Schwab Node (Get Orders)
  Resource: Orders
  Operation: Get Orders By Path Param
  Status: PENDING
  ↓
Loop Over Items
  ↓
Schwab Node (Cancel Order)
  Resource: Orders
  Operation: Cancel Order
  Order ID: {{$json.orderId}}
```

## Common Use Cases

### Portfolio Monitoring
- Get account balances every morning
- Alert on position changes
- Track daily P&L

### Automated Trading
- Execute trades based on signals
- Rebalance portfolio periodically
- Implement stop-loss strategies

### Transaction Tracking
- Log all trades to database
- Generate trading reports
- Tax reporting preparation

### Order Management
- Batch order placement
- Smart order routing
- Order modification and cancellation

## Tips & Tricks

### Working with Date Formats
Schwab API expects ISO-8601 format:
```javascript
// In n8n expressions
{{$now.toISO()}}
{{$now.minus({days: 7}).toISO()}}
{{$now.plus({hours: 1}).toISO()}}
```

### Error Handling
Always add an error workflow branch:
```
Try Node
  ↓
Schwab Node
  ↓
Success → Continue workflow
  ↓
Error → Log/Alert/Retry
```

### Rate Limiting
- Schwab API has rate limits
- Add delays between bulk operations
- Use Schedule node for periodic checks

### Account Numbers
- Use encrypted account values from "Get Account Numbers"
- Don't hardcode account numbers in workflows
- Store in environment variables if needed

## Available Resources & Operations

### Accounts
- ✅ Get Account Numbers
- ✅ Get All Accounts  
- ✅ Get Specific Account

### Orders
- ✅ Get Orders (by account)
- ✅ Get Orders (all accounts)
- ✅ Get Order by ID
- ✅ Place Order
- ✅ Cancel Order
- ✅ Replace Order
- 🚧 Preview Order (coming soon)

### Transactions
- ✅ Get Transactions
- ✅ Get Transaction by ID

### User Preferences
- ✅ Get User Preferences

## Debugging

### Enable Debug Mode
Add a "Function" node after Schwab node:
```javascript
// Log the full response
console.log(JSON.stringify($input.all(), null, 2));
return $input.all();
```

### Check Credentials
Test with "Get Account Numbers" operation - it's the simplest and requires minimal permissions.

### Common Errors

| Error | Cause | Solution |
|-------|-------|----------|
| 401 Unauthorized | Invalid/expired token | Refresh credentials |
| 403 Forbidden | Insufficient permissions | Check app scopes |
| 404 Not Found | Invalid account/order ID | Verify ID is correct |
| 429 Too Many Requests | Rate limit exceeded | Add delays |
| 500 Server Error | Schwab API issue | Retry after delay |

## Best Practices

1. **Test with small amounts** - Start with test trades if available
2. **Use error handling** - Always handle API failures gracefully
3. **Log everything** - Keep audit trail of all trading actions
4. **Validate inputs** - Check order quantities, prices, symbols
5. **Monitor executions** - Set up alerts for workflow failures
6. **Keep credentials secure** - Never expose in logs or outputs

## Next Steps

1. ✅ Install the node
2. ✅ Set up credentials
3. ✅ Test with "Get Account Numbers"
4. ✅ Build your first workflow
5. 🚀 Automate your trading strategy!

## Support

- **Issues**: Report on GitHub
- **Questions**: n8n Community forum
- **Schwab API**: developer.schwab.com

---

Happy automating! 🚀
