Need help with alpaca-trade-api-go?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

154 Stars 48 Forks Apache License 2.0 209 Commits 11 Opened issues


Go client for Alpaca's trade API

Services available


Need anything else?

Contributors list


CircleCI Status Go Report Card

is a Go library for the Alpaca trade API. It allows rapid trading algo development easily, with support for the both REST and streaming interfaces. For details of each API behavior, please see the online API document.


$ go get
$ go get
$ go get
$ go get


In order to call Alpaca's trade API, you need to obtain an API key pair. Replace and with what you get from the web console.

REST example

import (



func init() { os.Setenv(common.EnvApiKeyID, "") os.Setenv(common.EnvApiSecretKey, "")

fmt.Printf("Running w/ credentials [%v %v]\n", common.Credentials().ID, common.Credentials().Secret)



func main() { alpacaClient := alpaca.NewClient(common.Credentials()) acct, err := alpacaClient.GetAccount() if err != nil { panic(err) }



Streaming example

The SDK provides a unified streaming interface for both data updates (from Alpaca or Polygon), and Alpaca's trade/account updates. The following example subscribes to trade updates, and prints any messages received, and subscribes to live quotes for AAPL, and prints any quotes received. The main function also ends with an empty

statement which causes the program to run indefinitely.

In order to use Polygon streaming, you need to call

. This requires your Alpaca account to be eligible for Polygon integration (for details of the setup, please read Alpaca API document). ```go package main

import ( "fmt" "os"



func main() { os.Setenv(common.EnvApiKeyID, "yourkeyid") os.Setenv(common.EnvApiSecretKey, "yoursecretkey")

if err := stream.Register(alpaca.TradeUpdates, tradeHandler); err != nil {

if err := stream.Register("Q.AAPL", quoteHandler); err != nil { panic(err) }

select {}


func tradeHandler(msg interface{}) { tradeupdate := msg.(alpaca.TradeUpdate) fmt.Printf("%s event received for order %s.\n", tradeupdate.Event, tradeupdate.Order.ID) }

func quoteHandler(msg interface{}) { quote := msg.(alpaca.StreamQuote)

fmt.Println(quote.Symbol, quote.BidPrice, quote.BidSize, quote.AskPrice, quote.AskSize)

} ```


You could also deregister from a channel. e.g:

   if err := stream.Deregister("Q.AAPL"); err != nil {

API Document

The HTTP API document is located at


The Alpaca API requires API key ID and secret key, which you can obtain from the web console after you sign in. This key pair can then be applied to the SDK either by setting environment variables (

), or hardcoding them into the Go code directly as shown in the examples above.
$ export APCA_API_KEY_ID=xxxxx
$ export APCA_API_SECRET_KEY=yyyyy


For paper trading, set the environment variable


You can also instead use the function

to configure the endpoint.

Running Multiple Strategies

There's a way to execute more than one algorithm at once.
The websocket connection is limited to 1 connection per account.
For that exact purpose this project was created
The steps to execute this are: * Run the Alpaca Proxy Agent as described in the project's README * Define this env variable:

to be the address of the proxy agent. (e.g:
) * execute your algorithm. it will connect to the servers through the proxy agent allowing you to execute multiple strategies

note: this env variable could be used to proxy the data websocket through a custom server too.


For a more in-depth look at the SDK, see the GoDoc

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.