SDKs overview

Alpaca offers a suite of Software Development Kits (SDKs) and libraries designed to facilitate programmatic interaction with its financial APIs. These tools abstract away the complexities of direct HTTP requests, authentication, and response parsing, allowing developers to focus on building trading applications, algorithmic strategies, and integrated brokerage solutions. The SDKs provide structured access to Alpaca's core products, including the Trading API, Market Data API, and Broker API.

The availability of official SDKs across multiple languages, such as Python, Go, C#, Node.js, and Ruby, addresses diverse development environments. Each SDK is maintained to align with the latest API versions and features, ensuring compatibility and access to new functionalities as they are released. Beyond the official offerings, a developer community contributes additional libraries and tools, expanding the ecosystem and supporting specialized use cases.

Using an SDK can streamline the development process by handling boilerplate code, managing API rate limits (where applicable), and providing language-idiomatic interfaces for API operations. For instance, an SDK might include methods for placing orders, retrieving account information, or subscribing to real-time market data streams, all encapsulated within familiar programming constructs.

Official SDKs by language

Alpaca provides officially supported SDKs for popular programming languages. These SDKs are designed to offer a consistent and reliable interface for interacting with Alpaca's various APIs, including those for trading, market data, and brokerage services. The following table summarizes the key official SDKs:

Language Package Name Installation Command Maturity
Python alpaca-trade-api pip install alpaca-trade-api Stable
Go github.com/alpacahq/alpaca-trade-api-go go get github.com/alpacahq/alpaca-trade-api-go Stable
C# Alpaca.Markets dotnet add package Alpaca.Markets Stable
Node.js @alpacahq/alpaca-trade-api npm install @alpacahq/alpaca-trade-api Stable
Ruby alpaca-trade-api gem install alpaca-trade-api Stable

Each official SDK typically includes modules for handling authentication, making API requests, processing responses, and managing streaming data connections (websockets). Developers can find detailed documentation and usage examples for each SDK on the Alpaca API reference pages.

Installation

Installing an Alpaca SDK generally follows the standard package management practices for each respective programming language. Below are specific instructions for each official SDK:

Python SDK

The Python SDK, named alpaca-trade-api, can be installed using pip, the Python package installer. It provides an interface for both the REST and streaming APIs.

pip install alpaca-trade-api

Go SDK

For Go projects, the Alpaca SDK is available via go get. This command fetches the package from its GitHub repository and adds it to your project's dependencies.

go get github.com/alpacahq/alpaca-trade-api-go

C# SDK

The C# SDK, Alpaca.Markets, is distributed as a NuGet package. Developers can install it using the dotnet CLI tool.

dotnet add package Alpaca.Markets

Node.js SDK

The Node.js SDK, @alpacahq/alpaca-trade-api, is published on npm. It can be installed using npm or yarn.

npm install @alpacahq/alpaca-trade-api

Or with Yarn:

yarn add @alpacahq/alpaca-trade-api

Ruby SDK

The Ruby SDK, alpaca-trade-api, is available as a RubyGems package. It can be installed using the gem command.

gem install alpaca-trade-api

Quickstart example

This Python example demonstrates how to use the Alpaca Python SDK to connect to the Trading API, retrieve account information, and place a market order. Before running, ensure you have set your API key and secret as environment variables (APCA_API_KEY_ID and APCA_API_SECRET_KEY) or pass them directly to the REST client constructor.

from alpaca_trade_api.rest import REST, TimeFrame
import os

# Initialize REST API client
# It's recommended to set API_KEY_ID and SECRET_KEY as environment variables
# For example: export APCA_API_KEY_ID="YOUR_KEY_ID"
#              export APCA_API_SECRET_KEY="YOUR_SECRET_KEY"
#              export APCA_API_BASE_URL="https://paper-api.alpaca.markets"

api = REST(
    key_id=os.environ.get('APCA_API_KEY_ID'),
    secret_key=os.environ.get('APCA_API_SECRET_KEY'),
    base_url=os.environ.get('APCA_API_BASE_URL', 'https://paper-api.alpaca.markets') # Use paper trading URL for testing
)

async def main():
    try:
        # Get account information
        account = api.get_account()
        print(f"Account Status: {account.status}")
        print(f"Equity: {account.equity}")

        # Place a market order for 1 share of AAPL
        order = api.submit_order(
            symbol='AAPL',
            qty=1,
            side='buy',
            type='market',
            time_in_force='day'
        )
        print(f"Placed order for {order.qty} shares of {order.symbol} (ID: {order.id})")

        # Get recent daily bars for SPY
        bars = api.get_bars(
            symbol='SPY',
            timeframe=TimeFrame.Day,
            start='2023-01-01',
            end='2023-01-05'
        ).df
        print("\nRecent SPY daily bars:")
        print(bars.head())

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

if __name__ == '__main__':
    import asyncio
    asyncio.run(main())

This example demonstrates basic functionalities: fetching account details, submitting a simple market order, and retrieving historical market data. For real-time data streaming, the SDKs also support WebSocket connections. Developers should consult the Alpaca Trading API documentation for full details on available endpoints and parameters.

Community libraries

Beyond the official SDKs, the Alpaca developer community has created and maintains several libraries and tools that extend functionality or provide alternative interfaces. These community-contributed resources can offer specialized features, integrations with other platforms, or support for languages not officially covered. While not officially supported by Alpaca, these libraries can be valuable for specific use cases.

  • Trading Bot Frameworks: Several community projects focus on building complete trading bot frameworks on top of Alpaca's APIs, often integrating with popular backtesting libraries or analytics tools.
  • Language Bindings: Occasionally, community members develop bindings for languages not covered by official SDKs, such as Rust or Java, filling gaps for developers working in those ecosystems.
  • Specialized Tools: This can include tools for easier strategy backtesting, advanced order management UIs, or integrations with charting libraries.

Developers interested in exploring community contributions can often find these projects on platforms like GitHub or within developer forums. It is important to note that the stability, maintenance, and security of community libraries can vary, and developers should review the project's documentation and source code before integrating them into production systems. For example, the open-source community maintains numerous open-source projects that extend various API capabilities.