Skip to main content

Configuration

Binance4s uses a simple case class for configuration. No external config files or frameworks required.

BinanceConfig

case class BinanceConfig(
mode: BinanceMode = BinanceMode.Testnet,
credentials: Option[Credentials] = None,
recvWindow: Option[Long] = None
)

case class Credentials(apiKey: String, secretKey: String)

Construction Methods

Direct

val config = BinanceConfig(
mode = BinanceMode.Live,
credentials = Some(Credentials("api-key", "secret-key")),
recvWindow = Some(5000L)
)

Builder

val config = BinanceConfig.builder
.live // or .testnet
.credentials("api-key", "secret-key")
.recvWindow(5000L)
.build

From Environment Variables

val config: IO[BinanceConfig] = BinanceConfig.fromEnv[IO]

Reads from:

VariableDescriptionDefault
BINANCE_MODElive or testnettestnet
BINANCE_API_KEYYour API keyNone
BINANCE_SECRET_KEYYour secret keyNone

Modes

ModeBase URIWebSocket URI
BinanceMode.Livehttps://api.binance.comwss://stream.binance.com:9443
BinanceMode.Testnethttps://testnet.binance.visionwss://testnet.binance.vision

recvWindow

The recvWindow parameter specifies the maximum milliseconds a request can be ahead of the server time. Binance rejects requests where timestamp + recvWindow < serverTime. Default is 5000ms on the Binance side.

tip

If you get -1021 Timestamp for this request was 1000ms ahead of the server's time errors, your system clock may be out of sync. Either sync your clock with NTP or increase recvWindow.

Security

  • Never commit credentials to version control
  • Use environment variables or a secrets manager
  • The Testnet has separate API keys from production. Generate them at testnet.binance.vision
  • Only enable the permissions you need (e.g., read-only for market data)