SDKs overview
FTX provides a comprehensive set of Application Programming Interfaces (APIs) for programmatic interaction with its exchange services. These include a FTX REST API for historical data and transactional operations, along with FTX WebSocket APIs for real-time market data and user account updates. To facilitate developer integration, FTX supports official Software Development Kits (SDKs) and a range of community-contributed libraries. These SDKs abstract the underlying HTTP requests and WebSocket connections, allowing developers to interact with the exchange using idiomatic code in various programming languages.
The primary use cases for FTX SDKs include:
- Automated Trading Strategies: Implementing algorithmic trading bots that can place, modify, and cancel orders based on predefined conditions.
- Portfolio Management: Developing applications to track balances, open positions, and historical trading activity across different FTX subaccounts.
- Market Data Analysis: Building tools to consume real-time price feeds, order book data, and historical candlestick charts for research and analysis.
- Wallet Operations: Facilitating deposits, withdrawals, and internal transfers programmatically.
Developers often choose between official SDKs and community libraries based on language preference, specific feature requirements, and the level of active maintenance. Official SDKs typically offer direct support from the vendor and are kept current with API changes, while community libraries can provide broader language coverage or specialized functionalities.
Official SDKs by language
FTX maintains official SDKs to provide a reliable interface for its API. These libraries are developed and supported directly by FTX, ensuring compatibility with the latest API versions and features. The official SDKs are designed to handle authentication, request signing, and response parsing, reducing the boilerplate code required for integration.
The table below outlines the key official SDKs available for FTX:
| Language | Package Name | Installation Command | Maturity |
|---|---|---|---|
| Python | ftx |
pip install ftx |
Stable, Actively Maintained |
| TypeScript/JavaScript | @ftx-us/websocket, @ftx-us/rest |
npm install @ftx-us/websocket @ftx-us/rest |
Stable, Actively Maintained |
These official libraries are documented within the FTX API documentation portal, which provides usage examples, method references, and guidance on authentication and error handling for each SDK. Developers are encouraged to refer to these resources for the most up-to-date information and best practices.
Installation
Installation of FTX SDKs typically involves using the package manager specific to the programming language. The following subsections detail the installation process for the primary official SDKs.
Python SDK Installation
The Python SDK for FTX is distributed via PyPI. To install it, use the pip package manager:
pip install ftx
After installation, you can import the library into your Python projects. It is recommended to use a virtual environment to manage dependencies and avoid conflicts with other Python packages, as described in Python's official documentation on virtual environments.
TypeScript/JavaScript SDK Installation
The TypeScript/JavaScript SDKs are available through npm. There are separate packages for REST API interactions and WebSocket connections. Install them using npm or yarn:
npm install @ftx-us/websocket @ftx-us/rest
# or
yarn add @ftx-us/websocket @ftx-us/rest
These packages can be used in both Node.js environments and modern web browsers that support module imports. Further details on setting up a development environment for JavaScript and TypeScript projects can be found in the Mozilla Developer Network's guide to JavaScript modules.
Quickstart example
This section provides a quickstart example using the FTX Python SDK to retrieve account information. Before running this code, ensure you have the FTX Python SDK installed and have generated API keys from your FTX account. Keep your API keys secure and never expose them in client-side code or public repositories.
First, obtain your API key and secret from your FTX account settings. For security best practices, store these credentials as environment variables or in a secure configuration file, rather than hardcoding them directly into your script.
import ftx
import os
# Retrieve API key and secret from environment variables
api_key = os.environ.get('FTX_API_KEY')
api_secret = os.environ.get('FTX_API_SECRET')
if not api_key or not api_secret:
print("Error: FTX_API_KEY and FTX_API_SECRET environment variables are not set.")
print("Please set them before running the script.")
exit()
# Initialize the client with your API key and secret
# For FTX.US, you might need to specify the subaccount if applicable:
# client = ftx.FtxClient(api_key=api_key, api_secret=api_secret, subaccount_name='YourSubaccountName')
client = ftx.FtxClient(api_key=api_key, api_secret=api_secret)
try:
# Get account information
account_info = client.get_account_info()
print("\n--- Account Information ---")
print(f"Total Account Value: {account_info['totalAccountValue']}")
print(f"Total Position Size: {account_info['totalPositionSize']}")
print("Positions:")
for position in account_info.get('positions', []):
print(f" - {position['future']}: Size={position['size']}, Recent PNL={position['recentPnl']}")
# Get wallet balances
wallet_balances = client.get_wallet_balances()
print("\n--- Wallet Balances ---")
for balance in wallet_balances:
if balance['total'] > 0:
print(f" - {balance['coin']}: Total={balance['total']}, Available={balance['free']}")
# Example: Get market data - last price of BTC-PERP
market_data = client.get_market("BTC-PERP")
if market_data:
print("\n--- BTC-PERP Market Data ---")
print(f"Last Price: {market_data['last']}")
print(f"Bid: {market_data['bid']}")
print(f"Ask: {market_data['ask']}")
else:
print("\nCould not retrieve BTC-PERP market data.")
except Exception as e:
print(f"An error occurred: {e}")
This example demonstrates how to instantiate the client, retrieve general account information, list wallet balances, and fetch basic market data for a specific perpetual contract. The get_account_info() and get_wallet_balances() methods interact with the REST API endpoints to fetch user-specific data. The get_market() method fetches public market data. For more complex operations like placing orders or subscribing to WebSocket streams, refer to the full documentation for the FTX Python client library.
Community libraries
Beyond the official SDKs, the FTX developer community has contributed various libraries across different programming languages. These libraries often fill gaps in language support or offer alternative implementations with specific features or architectural choices. While not officially supported by FTX, community libraries can be valuable for developers working in environments where an official SDK is not available or for those seeking specialized functionalities.
Examples of community-driven resources often include:
- Go and C# Implementations: Developers needing to integrate with FTX using Go or C# might find third-party libraries on platforms like GitHub. These typically replicate the functionality of the official REST and WebSocket APIs.
- Trading Bot Frameworks: Some community libraries integrate FTX API access into broader trading bot frameworks, providing components for strategy execution, risk management, and backtesting.
- Data Connectors: Libraries focused on connecting FTX data streams to analytical tools or databases for advanced quantitative analysis.
When considering a community library, it is advisable to evaluate its active maintenance, community support, and alignment with the latest FTX API specifications. Key factors for evaluation include:
- Last Update Date: Indicates how recently the library was maintained.
- Issue Tracker Activity: Shows responsiveness to bug reports and feature requests.
- Code Quality and Documentation: Assesses readability and ease of use.
- Security Audit Status: Especially critical for libraries handling API keys and trading operations.
Developers can typically discover these libraries by searching public code repositories like GitHub or through developer forums. Always cross-reference a community library's claims with the official FTX API documentation to ensure compatibility and correctness, especially regarding authentication and data formatting. Furthermore, understanding the security implications of third-party code is paramount, as highlighted in general developer security best practices from Microsoft Azure.