Class AlpacaClientFactory

java.lang.Object
markets.alpaca.client.AlpacaClientFactory

public final class AlpacaClientFactory extends Object
Entry point for creating pre-configured Alpaca API clients (REST and WebSocket).

For most applications, prefer client(AlpacaCredentials) or AlpacaClient.builder(AlpacaCredentials). AlpacaClient is an immutable facade that owns configured generated REST clients internally and exposes narrower workflow helpers. Use the builder when Trading, Market Data, and Broker credentials differ.

The *Client(...) methods return the generated ApiClient for the corresponding REST API, wired with the correct authentication scheme and a shared OkHttp client. Each REST API has its own ApiClient class with a different default base URL and server configuration. Overloads that accept OkHttpClient let applications use distinct transport settings for Broker, Market Data, Trading, WebSocket, or SSE workloads.

Thread safety: the shared OkHttpClient is safe to use concurrently, but the generated REST ApiClient instances are mutable. Configure base paths, default headers, JSON settings, and custom HTTP clients before sharing an ApiClient across threads. Prefer one fully configured ApiClient per API/environment/credential set, then share that instance read-only.

Resiliency: configure HTTP timeouts, logging, retry/backoff interceptors, proxies, and connection-pool settings on the supplied OkHttpClient. Keep application-level retries conservative around non-idempotent operations such as order placement, account creation, and transfers.

Example — Trading REST API


 var creds   = AlpacaCredentials.fromTradingApiEnvironmentVariables();
 var client = AlpacaClientFactory.client(creds);
 // default environment: TradingApiEnvironment.PAPER
 var liveClient = AlpacaClientFactory.client(creds, TradingApiEnvironment.PRODUCTION);

 var openOrders = client.orders().list(ListOrdersRequest.builder()
     .status(ListOrdersRequest.Status.OPEN)
     .symbols("AAPL")
     .limit(50)
     .build());

 // Escape hatch: create a fresh mutable generated client.
 var trading = client.newTradingClient();
 var ordersApi = new markets.alpaca.client.openapi.trading.api.OrdersApi(trading);
 

Example — Broker REST API


 var creds  = AlpacaCredentials.fromBrokerApiEnvironmentVariables();
 var broker = AlpacaClientFactory.brokerClient(creds);
 // default environment: BrokerApiEnvironment.SANDBOX
 var prodBroker = AlpacaClientFactory.brokerClient(creds, BrokerApiEnvironment.PRODUCTION);

 var accountsApi = new markets.alpaca.client.openapi.broker.api.AccountsApi(broker);
 

Example — Stock WebSocket stream


 var stream = AlpacaClientFactory.stockStream(
     creds, StockSource.IEX, AlpacaStreamEnvironment.PRODUCTION,
     new StockStreamListener() {
         public void onTrade(StockTrade t) { System.out.println(t); }
     });
 stream.connect(StockSubscription.builder().trades("AAPL").build());
 // later:
 stream.close();
 

NOTE: This file references classes generated by ./gradlew generateApis. Run that task at least once before opening the project in your IDE.