Skip to content

Schwab API Setup Instructions

✅ Your Configuration is Ready!

Your Schwab credentials are properly configured: - API Key: XmMkbKAQvjPC8SyxeC3u... - App Secret: nqBhlSgTLP2eZapGeQdZ... - Callback URL: https://127.0.0.1:5000/callback

🔐 One-Time Authentication Required

You need to authenticate once to create a token file. After that, it will work automatically.

Open a new terminal window and run:

cd /Users/bertfrichot/mem-agent-mcp
uv run python3 tools/schwab_setup.py

This will: 1. Open your browser to Schwab login 2. Ask you to approve the app 3. Redirect you to a URL (will show SSL error - that's OK!) 4. Copy the ENTIRE URL from browser address bar 5. Paste it back in terminal

Option 2: Manual Python Authentication

If you prefer, run this in a Python terminal:

cd /Users/bertfrichot/mem-agent-mcp
uv run python3

Then paste this code:

from schwab import auth
from dotenv import load_dotenv
import os

# Load credentials
load_dotenv('/Users/bertfrichot/Claude-Micro-Cap-Experiment/.env')

api_key = os.getenv('SCHWAB_CLIENT_ID')
app_secret = os.getenv('SCHWAB_CLIENT_SECRET')
callback_url = os.getenv('SCHWAB_CALLBACK_URL')
token_path = os.path.expanduser('~/.schwab_tokens.json')

# Authenticate (will open browser)
client = auth.client_from_manual_flow(
    api_key=api_key,
    app_secret=app_secret,
    callback_url=callback_url,
    token_path=token_path
)

# Test connection
accounts = client.get_account_numbers().json()
print(f"✅ Success! Found {len(accounts)} account(s)")
print(f"✅ Tokens saved to: {token_path}")

What to Expect:

  1. Browser opens - Log in to Schwab
  2. Approve access - Click "Allow" or "Approve"
  3. Redirect page - You'll see an SSL error page (this is normal!)
  4. Copy URL - Copy the ENTIRE URL from address bar, looks like:
    https://127.0.0.1:5000/callback?code=ABC123...&session_id=xyz...
    
  5. Paste back - Paste into terminal when prompted
  6. Done! - Token file created at ~/.schwab_tokens.json

After Authentication:

Once you have the token file, you can use Schwab API in any script:

from schwab import auth
import os

api_key = os.getenv('SCHWAB_CLIENT_ID')
app_secret = os.getenv('SCHWAB_CLIENT_SECRET')
token_path = os.path.expanduser('~/.schwab_tokens.json')

client = auth.client_from_token_file(token_path, api_key, app_secret)

# Now you can use the client!
accounts = client.get_account_numbers().json()

Troubleshooting:

SSL Certificate Error in Browser: - This is NORMAL! Just copy the URL from address bar anyway

"Callback URL mismatch": - Make sure your Schwab app has callback URL: https://127.0.0.1:5000/callback - Must match exactly (including port 5000)

Token expires: - Tokens expire after ~7 days of inactivity - Just run setup again to refresh

🚀 Next Steps After Setup:

Once authenticated, you can:

  1. Test connection:

    uv run python3 tools/schwab_setup.py --check
    

  2. Fetch live portfolio data:

    uv run python3 tools/portfolio_fetcher.py --schwab
    

  3. Run trading analytics with real data:

    uv run python3 tools/trading_analytics.py --live
    

📝 Token File Location:

Your tokens will be saved at:

~/.schwab_tokens.json

Keep this file safe! It contains your authentication credentials.


Need Help?

Run the setup script and follow the prompts. If you get stuck, let me know what error you see!