To use the ProjectX Python SDK, you need:
Set environment variables in your shell:
export PROJECT_X_API_KEY="your-api-key-here" # pragma: allowlist secret
export PROJECT_X_USERNAME="your-username"
export PROJECT_X_ACCOUNT_NAME="optional-account-name"
Or create a .env file in your project root:
# .env file
PROJECT_X_API_KEY=your-api-key-here
PROJECT_X_USERNAME=your-username
PROJECT_X_ACCOUNT_NAME=optional-account-name
Create a JSON configuration file at ~/.config/projectx/config.json:
{
"api_key": "your-api-key-here", # pragma: allowlist secret
"username": "your-username",
"account_name": "optional-account-name"
}
from project_x_py import ProjectX
# Using environment variables (recommended)
async with ProjectX.from_env() as client:
await client.authenticate()
print(f"Connected: {client.account_info.name}")
# Direct credentials (not recommended for production)
async with ProjectX(
api_key="your-api-key" # pragma: allowlist secret,
username="your-username"
) as client:
await client.authenticate()
The TradingSuite handles authentication automatically:
from project_x_py import TradingSuite
# Automatically uses environment variables
suite = await TradingSuite.create(["MNQ"])
# Or pass credentials directly
suite = await TradingSuite.create(
["MNQ"],
username="your-username",
api_key="your-api-key", # pragma: allowlist secret
)
The SDK automatically manages JWT tokens:
# Get current JWT token (if needed for custom integrations)
async with ProjectX.from_env() as client:
await client.authenticate()
jwt_token = client.get_session_token()
print(f"JWT Token: {jwt_token[:20]}...")
.env to .gitignore to prevent accidental commitsfrom project_x_py.exceptions import ProjectXAuthenticationError
try:
async with ProjectX.from_env() as client:
await client.authenticate()
except ProjectXAuthenticationError as e:
print(f"Authentication failed: {e}")
Enable debug logging to troubleshoot authentication issues:
import logging
logging.basicConfig(level=logging.DEBUG)
async with ProjectX.from_env() as client:
await client.authenticate()