LUSID SDK for Python
The `lusid-sdk` is the official Python SDK for interacting with the LUSID API, a cloud-native investment data platform. It enables programmatic management of portfolios, holdings, transactions, reference data, and various other financial data entities within the LUSID ecosystem. The SDK is actively maintained by FINBOURNE, with frequent releases often several times a month to reflect API updates and feature enhancements.
Common errors
-
ValueError: Missing configuration for API client factory: LUSID_API_URL
cause The `ApiClientFactory` could not find required environment variables like `LUSID_API_URL` or authentication credentials (e.g., `LUSID_FBN_TOKEN`, `LUSID_CLIENT_ID`, `LUSID_CLIENT_SECRET`).fixEnsure all necessary LUSID environment variables (e.g., `LUSID_API_URL`, `LUSID_APP_NAME`, and appropriate authentication credentials) are correctly set in your environment or provided directly to the `ApiClientFactory`. -
finbourne.lusid.sdk.ApiException: (401) Unauthorized
cause The SDK failed to authenticate with the LUSID API, indicating incorrect or expired credentials.fixVerify that your `LUSID_FBN_TOKEN` or OAuth2 client credentials (`LUSID_CLIENT_ID`, `LUSID_CLIENT_SECRET`, `LUSID_TOKEN_URL`) are correct and have the necessary permissions. Also, check that `LUSID_API_URL` is correct. -
AttributeError: 'ApiClientFactory' object has no attribute 'list_portfolios'
cause You are attempting to call an API method directly on the `ApiClientFactory` instance, instead of on a specific API client built by the factory.fixYou must first build an API client from the factory for the specific service you want to interact with. For example, `portfolios_api = api_factory.build(PortfoliosApi)` and then call `portfolios_api.list_portfolios()`.
Warnings
- gotcha The recommended authentication and client creation mechanism is `ApiClientFactory`. Direct instantiation of `Configuration` and `ApiClient` objects from `lusid_sdk.configuration` and `lusid_sdk.api_client` is still possible but less robust for managing multiple API clients or complex authentication flows.
- gotcha Many LUSID API list methods return paginated results. Directly accessing `response.values` will only give the first page. For comprehensive results, you need to iterate through pages or use the SDK's built-in `get_all` helper methods (if available for the specific API endpoint).
- gotcha LUSID is a rapidly evolving platform. The SDK is frequently updated to reflect changes in the underlying LUSID API (new endpoints, modified models, deprecations). Using an outdated SDK version can lead to `AttributeError` for missing fields/methods or unexpected API behaviour.
- breaking Major version 2.0.0 introduced significant changes to the SDK's internal structure and recommended usage patterns, primarily around how API clients are instantiated and configured. Older code using direct `ApiClient` and `Configuration` setup may break or behave unexpectedly without modification.
Install
-
pip install lusid-sdk
Imports
- ApiClientFactory
from lusid_sdk.configuration import Configuration
from lusid_sdk.utilities import ApiClientFactory
- PortfoliosApi
from lusid_sdk.api import PortfoliosApi
Quickstart
import os
from lusid_sdk.utilities import ApiClientFactory
from lusid_sdk.api import PortfoliosApi
# Configure environment variables for LUSID authentication.
# These can be set in your shell or via a .env file.
# Example for Personal Access Token (PAT):
# export LUSID_FBN_TOKEN='your_finbourne_token'
# export LUSID_API_URL='https://lusid.finbourne.com/api'
# export LUSID_APP_NAME='my-python-app'
#
# Example for OAuth 2.0 Client Credentials:
# export LUSID_CLIENT_ID='your_client_id'
# export LUSID_CLIENT_SECRET='your_client_secret'
# export LUSID_TOKEN_URL='https://identity.finbourne.com/oauth/token'
# export LUSID_API_URL='https://lusid.finbourne.com/api'
# export LUSID_APP_NAME='my-python-app'
try:
# Create an ApiClientFactory instance. It picks up config from environment variables.
# For LUSID_FBN_TOKEN, you would use: ApiClientFactory()
# For OAuth, you might need to pass details if not in env vars, but env vars are preferred.
api_factory = ApiClientFactory()
# Get a specific API client, e.g., for portfolios
portfolios_api: PortfoliosApi = api_factory.build(PortfoliosApi)
# List all portfolios
paginated_response = portfolios_api.list_portfolios(
limit=5,
scope='default'
)
print(f"Successfully connected to LUSID. Found {paginated_response.paging.total_items} portfolios.")
for portfolio in paginated_response.values:
print(f"- {portfolio.display_name} ({portfolio.scope}/{portfolio.code})")
except Exception as e:
print(f"An error occurred: {e}")
if "401 Unauthorized" in str(e):
print("Please check your LUSID authentication details (LUSID_FBN_TOKEN or OAuth credentials).")
elif "Missing configuration" in str(e):
print("Ensure LUSID_API_URL and authentication environment variables are set.")