Skip to content

binance/binance-connector-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Binance Java Connectors

Build Status Open Issues Code Style: Spotless Java Version Known Vulnerabilities License: MIT

Collection of auto-generated java connectors for Binance APIs.

Prerequisites

Before using the connectors, ensure you have:

  • Java (version >= 11)

Available Connectors

Documentation

For detailed information, refer to the Binance API Documentation.

Installation

Each connector is published as a separate maven dependency. For example:

<dependency>
  <groupId>io.github.binance</groupId>
  <artifactId>binance-spot</artifactId>
  <version>2.0.0</version>
</dependency>

Contributing

Since this repository contains auto-generated code using OpenAPI Generator, we encourage you to:

  1. Open a GitHub issue to discuss your ideas or report bugs
  2. Allow maintainers to implement necessary changes through the code generation process

Types of Signature Generator

When creating REST clients (such as SpotRestApi), you use one of the following types of Signature Generator to create signatures (for SIGNED endpoints) based on your security preference:

  • HMAC - Use of API Key and Secret Key.
    SignatureConfiguration signatureConfiguration = new SignatureConfiguration();
    signatureConfiguration.setApiKey("apiKey");
    signatureConfiguration.setSecretKey("secretKey");

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setSignatureConfiguration(signatureConfiguration);
    SpotRestApi spotRestApi = new SpotRestApi(clientConfiguration);
  • RSA or ED25519 - use of API Key and RSA/Ed25519 algorithm keys.
    SignatureConfiguration signatureConfiguration = new SignatureConfiguration();
    signatureConfiguration.setApiKey("apiKey");
    signatureConfiguration.setPrivateKey("path/to/private.key");
    // if the private key is protected by a password
    signatureConfiguration.setPrivateKeyPass("privateKeyPass");
    
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setSignatureConfiguration(signatureConfiguration);
    SpotRestApi spotRestApi = new SpotRestApi(clientConfiguration);

When creating WebSocket API clients (such as SpotWebSocketApi), you can follow:

  • HMAC
    SignatureConfiguration signatureConfiguration = new SignatureConfiguration();
    signatureConfiguration.setApiKey("apiKey");
    signatureConfiguration.setSecretKey("secretKey");

    WebSocketClientConfiguration clientConfiguration = new WebSocketClientConfiguration();
    clientConfiguration.setSignatureConfiguration(signatureConfiguration);
    SpotWebSocketApi spotRestApi = new SpotWebSocketApi(clientConfiguration);
  • Ed25519 or RSA
    SignatureConfiguration signatureConfiguration = new SignatureConfiguration();
    signatureConfiguration.setApiKey("apiKey");
    signatureConfiguration.setPrivateKey("path/to/private.key");
    // if the private key is protected by a password
    signatureConfiguration.setPrivateKeyPass("privateKeyPass");
    
    WebSocketClientConfiguration clientConfiguration = new WebSocketClientConfiguration();
    clientConfiguration.setSignatureConfiguration(signatureConfiguration);
    SpotWebSocketApi spotRestApi = new SpotWebSocketApi(clientConfiguration);

How to use WebSocket connection pool

Websocket API

    // Basic client config
    WebSocketClientConfiguration clientConfiguration = SpotWebSocketApiUtil.getClientConfiguration();
    // set usePool flag to true
    clientConfiguration.setUsePool(true);
    // Use the api
    SpotWebSocketApi spotWebSocketApi = new SpotWebSocketApi(connectionWrapper);

Websocket Streams

    // Basic client config
    WebSocketClientConfiguration clientConfiguration = SpotWebSocketStreamsUtil.getClientConfiguration();
    // set usePool flag to true
    clientConfiguration.setUsePool(true);
    // Use the api
    SpotWebSocketStreams api = new SpotWebSocketStreams(clientConfiguration);

How to use a proxy

Rest Api

    // Basic client config
    ClientConfiguration clientConfiguration = SpotRestApiUtil.getClientConfiguration();
    // Create the HTTP proxy
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("host", 123));
    // Add the proxy to the configuration
    clientConfiguration.setProxy(proxy);
    // Create the Proxy Authenticator
    Authenticator proxyAuthenticator = new Authenticator() {
        @Override public Request authenticate(Route route, Response response) throws IOException {
            String credential = Credentials.basic("username", "password");
            return response.request().newBuilder()
                    .header("Proxy-Authorization", credential)
                    .build();
        }
    };
    // Add the proxy authenticator to the configuration
    clientConfiguration.setProxyAuthenticator(proxyAuthenticator);
    // Use with API
    SpotRestApi spotRestApi = new SpotRestApi(clientConfiguration);

Websocket API

    // Basic client config
    WebSocketClientConfiguration clientConfiguration = SpotWebSocketApiUtil.getClientConfiguration();
    // Create the HTTP proxy
    HttpProxy proxy = new HttpProxy("host", 123);
    // Add the proxy to the configuration
    clientConfiguration.setWebSocketProxy(proxy);
    // Create the Proxy Authentication
    BasicAuthentication basicAuthentication = new BasicAuthentication(
            URI.create("http://host:123"),
            Authentication.ANY_REALM,
            "username",
            "password"
    );
    // Add the Proxy Authentication to the configuration
    clientConfiguration.setWebSocketProxyAuthentication(basicAuthentication);
    // Use with API
    SpotWebSocketApi spotWebSocketApi = new SpotWebSocketApi(clientConfiguration);

Websocket Streams

    // Basic client config
    WebSocketClientConfiguration clientConfiguration = SpotWebSocketStreamsUtil.getClientConfiguration();
    // Create the HTTP proxy
    HttpProxy proxy = new HttpProxy("host", 123);
    // Add the proxy to the configuration
    clientConfiguration.setWebSocketProxy(proxy);
    // Create the Proxy Authentication
    BasicAuthentication basicAuthentication = new BasicAuthentication(
            URI.create("http://host:123"),
            Authentication.ANY_REALM,
            "username",
            "password"
    );
    // Add the Proxy Authentication to the configuration
    clientConfiguration.setWebSocketProxyAuthentication(basicAuthentication);
    // Use with API
    SpotWebSocketStreams spotWebSocketApi = new SpotWebSocketStreams(clientConfiguration);

Examples

Algo: Rest API

C2c: Rest API

Convert: Rest API

Copy Trading: Rest API

Crypto Loan: Rest API

Derivatives Trading Coin Futures: Rest API - WebSocket API - WebSocket Streams

Derivatives Trading Options: Rest API - WebSocket Streams

Derivatives Trading Portfolio Margin: Rest API

Derivatives Trading Portfolio Margin Pro: Rest API

Derivatives Trading Usds Futures: Rest API - WebSocket API - WebSocket Streams

Dual Investment: Rest API

Fiat: Rest API

Gift Card: Rest API

Margin Trading: Rest API

Mining: Rest API

Nft: Rest API

Pay: Rest API

Rebate: Rest API

Simple Earn: Rest API

Spot: Rest API - WebSocket API - WebSocket Streams

Staking: Rest API

Sub Account: Rest API

Vip Loan: Rest API

Wallet: Rest API

License

This project is licensed under the MIT License - see the LICENSE file for details.