SnapTrade Python SDK

11.0.177 · active · verified Thu Apr 16

The SnapTrade Python SDK provides a client library to easily interact with the SnapTrade API, enabling applications to connect brokerage accounts for live positions and trading. It abstracts away direct API calls, handling authentication and data formatting. The library is actively maintained with frequent updates, currently at version 11.0.177, and supports Python versions 3.8 and newer.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the SnapTrade client, check the API status, and interact with user registration/listing. You will need your SnapTrade `CLIENT_ID` and `CONSUMER_KEY` (obtained from your SnapTrade Dashboard) configured as environment variables. For actual user management and connections, you would register a user with a unique `user_id` from your application, obtain a `user_secret`, and then use these credentials to generate a connection portal URL.

import os
from pprint import pprint
from snaptrade_client import SnapTrade, ApiException

CLIENT_ID = os.environ.get('SNAPTRADE_CLIENT_ID', 'YOUR_CLIENT_ID')
CONSUMER_KEY = os.environ.get('SNAPTRADE_CONSUMER_KEY', 'YOUR_CONSUMER_KEY')

if not CLIENT_ID or not CONSUMER_KEY:
    print("Please set SNAPTRADE_CLIENT_ID and SNAPTRADE_CONSUMER_KEY environment variables.")
else:
    try:
        # 1. Initialize the SnapTrade client
        snaptrade = SnapTrade(
            consumer_key=CONSUMER_KEY,
            client_id=CLIENT_ID,
        )

        # 2. Check API status
        api_status_response = snaptrade.api_status.check()
        print("API Status:")
        pprint(api_status_response)

        # 3. Register a new user (or use an existing one)
        # In a real application, you'd associate this user_id with your own user system.
        # user_id = "unique-user-id-from-your-app"
        # register_response = snaptrade.authentication.register_snap_trade_user(user_id=user_id)
        # user_secret = register_response.user_secret
        # print(f"Registered user: {register_response.user_id}, Secret: {user_secret}")

        # Example: List existing users (for demonstration)
        list_users_response = snaptrade.authentication.list_snap_trade_users()
        if list_users_response and list_users_response.users:
            print("\nExisting SnapTrade users:")
            for user_info in list_users_response.users:
                pprint(user_info)
        else:
            print("\nNo SnapTrade users found yet. Register one to proceed with connections.")

    except ApiException as e:
        print(f"An API error occurred: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

view raw JSON →