alpaca-trade-api-go is the Alpaca Go SDK for the Trading API and Market Data API. It supports both REST and streaming interfaces for trading application development. For details of each API behavior, see the Alpaca API documentation.
go get -u github.com/alpacahq/alpaca-trade-api-go/v3/alpacaTo call the Alpaca Trading API, obtain a paper-account API key pair from the Alpaca dashboard and set the APCA_API_KEY_ID and APCA_API_SECRET_KEY environment variables as described in Authentication. The inline Trading examples below explicitly use the paper-trading endpoint.
package main
import (
"fmt"
"github.com/alpacahq/alpaca-trade-api-go/v3/alpaca"
)
func main() {
client := alpaca.NewClient(alpaca.ClientOpts{
BaseURL: "https://paper-api.alpaca.markets",
})
acct, err := client.GetAccount()
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", *acct)
}The following example shows how you can stream your own trade updates. First, we register a handler function that prints the received trade updates, then we submit a single AAPL buy order through the paper-trading endpoint. You should see two updates: a new event as soon as you submit the order, and a fill event soon after that, provided that the market is open.
client := alpaca.NewClient(alpaca.ClientOpts{
BaseURL: "https://paper-api.alpaca.markets",
})
// Listen to trade updates in the background (with unlimited reconnect)
client.StreamTradeUpdatesInBackground(context.TODO(), func(tu alpaca.TradeUpdate) {
log.Printf("TRADE UPDATE: %+v\n", tu)
})
// Send a single AAPL order
qty := decimal.NewFromInt(1)
if _, err := client.PlaceOrder(alpaca.PlaceOrderRequest{
Symbol: "AAPL",
Qty: &qty,
Side: "buy",
Type: "market",
TimeInForce: "day",
}); err != nil {
log.Fatalf("failed place order: %v", err)
}
log.Println("order sent")
select {}Failed API calls return an *alpaca.APIError. Include RequestID (from Alpaca's X-Request-ID header) when opening a support ticket:
acct, err := client.GetAccount()
if err != nil {
var apiErr *alpaca.APIError
if errors.As(err, &apiErr) {
log.Printf("request %s failed: %v", apiErr.RequestID, apiErr)
return
}
log.Fatal(err)
}See the examples directory for further examples:
- algo-trading examples
- bull-spread
- long-short
- martingale
- mean-reversion
- marketdata examples
- crypto-perp-stream
- crypto-stream
- data-stream
- marketdata
The Alpaca API requires an API key ID and secret key. The SDK reads them from the APCA_API_KEY_ID and APCA_API_SECRET_KEY environment variables:
export APCA_API_KEY_ID=xxxxx
export APCA_API_SECRET_KEY=yyyyyApplications that load credentials securely at runtime can instead pass them through the APIKey and APISecret fields of alpaca.ClientOpts. Do not commit credentials to source control.
For paper trading, set the APCA_API_BASE_URL environment variable or set the BaseURL field when constructing the client:
export APCA_API_BASE_URL=https://paper-api.alpaca.marketsThe v3 client preserves its historical default: if neither ClientOpts.BaseURL nor APCA_API_BASE_URL is set, it uses the live Trading API at https://api.alpaca.markets. Applications that can place orders should always select the Trading endpoint explicitly and keep paper and live credentials in separate configuration.
This SDK does not currently expose first-class Broker API resource methods for account creation, funding, or account-scoped trading. Use the Broker API documentation for those workflows.
The marketdata client supports broker-authenticated Market Data. The recommended approach is to obtain a short-lived access token through the client credentials authentication flow and pass it through marketdata.ClientOpts.OAuth. This setup fragment uses APCA_BROKER_ACCESS_TOKEN as an application-defined environment variable; the SDK does not read it automatically:
accessToken := os.Getenv("APCA_BROKER_ACCESS_TOKEN")
if accessToken == "" {
log.Fatal("APCA_BROKER_ACCESS_TOKEN is required")
}
client := marketdata.NewClient(marketdata.ClientOpts{
OAuth: accessToken,
BaseURL: "https://data.sandbox.alpaca.markets",
})For legacy Basic Auth on Market Data requests, use the BrokerKey and BrokerSecret fields and leave both OAuth and APCA_API_OAUTH unset because Bearer authentication takes precedence. These fields select the request authentication method; they do not add Broker API resource methods.
Broker integrations use sandbox or production environments:
https://broker-api.sandbox.alpaca.marketsfor sandbox Broker API resourceshttps://broker-api.alpaca.marketsfor production Broker API resourceshttps://data.sandbox.alpaca.marketsfor sandbox broker-authenticated Market Datahttps://data.alpaca.marketsfor production broker-authenticated Market Data
Build and test the SDK with the repository's standard commands:
go build -v ./...
make test
make lintThe make lint target installs the linter version pinned by the repository workflow before running it. When entity definitions change, regenerate easyjson files:
make generateFor failed API calls, share the RequestID from *alpaca.APIError only through private Alpaca Support channels. It contains the X-Request-ID response header (see Handling API errors).
- Library / SDK issues: Bugs, feature requests, or questions specific to this SDK → GitHub Issues.
- Security vulnerabilities: Do not report them publicly. Follow our Security Policy.
- Alpaca API bugs: Bugs in the Alpaca API itself, its OpenAPI specs, or its documentation → alpacahq/alpaca-api.
- Community: API feature requests, general questions, and dashboard bugs → Alpaca Community Forum or Slack.
- Account help: Questions about your account, funding, or orders → Support request.
- Outages: Service status and incident updates → Status page.
Contributions are welcome. See CONTRIBUTING.md for setup, contribution scope, generated-code boundaries, quality requirements, and the pull-request workflow. Participation is governed by our Code of Conduct.