Bitget Open API V3 SDK

repository·master·Indexed 42 days ago

https://github.com/bitgetlimited/v3-bitget-api-sdk

Official client implementations for interacting with the Bitget Open API V3, enabling integration of trading and account services. The SDK provides support for REST and WebSocket interfaces across multiple languages, including Go, Java, Node.js, and PHP. It includes tools for RSA key pair generation and specialized clients for managing orders and real-time data streams.

Tokens
22K
Snippets
69
Records
79
Agent score
51%

What's inside Bitget

  1. Supported languages for Bitget Open API V3 SDK

    master

    The Bitget Open API V3 SDK is available for multiple programming languages. Depending on your development environment, you can use one of the following language-specific implementations:

    • Java: bitget-java-sdk-api
    • Python: bitget-python-sdk-api
    • Node.js: bitget-node-sdk-api
    • Go: bitget-golang-sdk-api
    • PHP: bitget-php-sdk-api
  2. Configure the BitgetRestClient

    master

    To initialize the SDK, create a BitgetRestClient bean using ClientParameter. You must provide your apiKey, secretKey, passphrase, and baseUrl.

    If your API key uses RSA, you must set the signType to SignTypeEnum.RSA and provide your RSA private key in the secretKey field.

    @Configuration
    @EnableAsync
    public class SdkConfig {
    
      private final String apiKey = "your apiKey";
      private final String secretKey = "your secretKey";
      private final String passphrase = "your passphrase";
      private final String baseUrl = "https://api.bitget.com";
    
      @Bean
      public BitgetRestClient bitgetRestClient() throws Exception {
        ClientParameter parameter = ClientParameter.builder()
                .apiKey(apiKey)
                .secretKey(secretKey) // If RSA type, set RSA private key here
                .passphrase(passphrase)
                .baseUrl(baseUrl)
                //.signType(SignTypeEnum.RSA) // Uncomment if your apikey is RSA type
                .locale(SupportedLocaleEnum.ZH_CN.getName()).build();
        return BitgetRestClient.builder().configuration(parameter).build();
      }
    }
  3. Use the RestAPI for orders and market data

    master

    The RestAPI allows you to place orders and query market information. You can use specialized API classes (like OrderApi) or the base BitgetApi class to send direct POST or GET requests.

    Common Patterns

    • Placing an order via specialized API: Use OrderApi(apiKey, secretKey, passphrase).placeOrder(params).
    • Placing an order via direct POST: Use BitgetApi(apiKey, secretKey, passphrase).post(url, params).
    • Querying market data via GET: Use BitgetApi(apiKey, secretKey, passphrase).get(url, params).

    All RestAPI calls should be wrapped in a try-except block catching BitgetAPIException to handle errors.

    import bitget.v1.mix.order_api as maxOrderApi
    import bitget.bitget_api as baseApi
    from bitget.exceptions import BitgetAPIException
    
    if __name__ == '__main__':
        apiKey = ""
        secretKey = ""
        passphrase = ""
    
        # Demo 1: place order
        maxOrderApi = maxOrderApi.OrderApi(apiKey, secretKey, passphrase)
        try:
            params = {
                "symbol": "BTCUSDT_UMCBL",
                "marginCoin": "USDT",
                "side": "open_long",
                "orderType": "limit",
                "price": "27012",
                "size": "0.01",
                "timInForceValue": "normal"
            }
            response = maxOrderApi.placeOrder(params)
            print(response)
        except BitgetAPIException as e:
            print("error:" + e.message)
    
        # Demo 2: place order by post directly
        baseApi = baseApi.BitgetApi(apiKey, secretKey, passphrase)
        try:
            params = {
                "symbol": "BTCUSDT_UMCBL",
                "marginCoin": "USDT",
                "side": "open_long",
                "orderType": "limit",
                "price": "27012",
                "size": "0.01",
                "timInForceValue": "normal"
            }
            response = baseApi.post("/api/mix/v1/order/placeOrder", params)
            print(response)
        except BitgetAPIException as e:
            print("error:" + e.message)
    
        # Demo 3: send get request
        try:
            params = {"productType": "umcbl"}
            response = baseApi.get("/api/mix/v1/market/contracts", params)
            print(response)
        except BitgetAPIException as e:
            print("error:" + e.message)
    
        # Demo 4: send get request with no params
        try:
            response = baseApi.get("/api/spot/v1/account/getInfo", {})
            print(response)
        except BitgetAPIException as e:
            print("error:" + e.message)
  4. Configure API Key credentials

    master

    Before using the SDK, you must provide your Bitget API credentials. If you do not have an API key, apply for one on the official Bitget website.

    Update the following variables in the provided example files (example_*.py for RestAPI or example_ws_contract.py for WebSocketAPI):

    api_key = ""
    secret_key = ""
    passphrase = ""

    Note for RSA users: If your API key is of the RSA type, you must manually set the signature type to RSA by editing the constans.py file. Set SIGN_TYPE = RSA.

  5. Use the RestAPI for trading and market data

    master

    The RestAPI allows you to place orders and fetch market or account data. You can use specialized API classes (like OrderApi) or the base BitgetApi class to make direct POST or GET requests.

    Important for RSA users: If your API key is of the RSA type, you must manually set the signature type to RSA in the constans.py file by setting SIGN_TYPE = RSA.

    import bitget.v1.mix.order_api as maxOrderApi
    import bitget.bitget_api as baseApi
    from bitget.exceptions import BitgetAPIException
    
    if __name__ == '__main__':
        apiKey = ""
        secretKey = '''your'''
        passphrase = ""
    
        # Demo 1: Place an order using a specialized API class
        maxOrderApi = maxOrderApi.OrderApi(apiKey, secretKey, passphrase)
        try:
            params = {
                "symbol": "BTCUSDT_UMCBL",
                "marginCoin": "USDT",
                "side": "open_long",
                "orderType": "limit",
                "price": "27012",
                "size": "0.01",
                "timInForceValue": "normal"
            }
            response = maxOrderApi.placeOrder(params)
            print(response)
        except BitgetAPIException as e:
            print("error:" + e.message)
    
        # Demo 2: Place an order by calling the POST method directly
        baseApi = baseApi.BitgetApi(apiKey, secretKey, passphrase)
        try:
            params = {
                "symbol": "BTCUSDT_UMCBL",
                "marginCoin": "USDT",
                "side": "open_long",
                "orderType": "limit",
                "price": "27012",
                "size": "0.01",
                "timInForceValue": "normal"
            }
            response = baseApi.post("/api/mix/v1/order/placeOrder", params)
            print(response)
        except BitgetAPIException as e:
            print("error:" + e.message)
    
        # Demo 3: Send a GET request for market data
        try:
            params = {"productType": "umcbl"}
            response = baseApi.get("/api/mix/v1/market/contracts", params)
            print(response)
        except BitgetAPIException as e:
            print("error:" + e.message)
    
        # Demo 4: Send a GET request with no parameters
        try:
            response = baseApi.get("/api/spot/v1/account/getInfo", {})
            print(response)
        except BitgetAPIException as e:
            print("error:" + e.message)
  6. Security best practices for Bitget API keys in Postman

    master

    When using Postman for API testing, follow these security measures to protect your Bitget API keys:

    • Local Use Only: Do not enable cloud sync or use the Postman web version to prevent third parties from accessing your keys. Use a local device only.
    • Principle of Least Privilege:
      • Disable withdrawal permissions on all API keys.
      • For read-only use cases, do not enable trading or withdrawal permissions.
    • Access Control: Enable IP whitelisting to restrict API usage to your known and trusted IP addresses.
    • Key Management:
      • Do not reuse API keys after testing is complete.
      • Change API keys regularly.
      • Immediately remove any keys that may have been exposed or compromised.
    • Version Control: If storing credentials in environment files, ensure they are added to .gitignore and excluded from version control.
  7. Import the Bitget API Collection into Postman

    master

    To use the Bitget API for testing and development, you can import a pre-configured Postman collection. The provided JSON file contains both the API collection and the necessary environment variables.

    1. Download the JSON file from the QuickStartWithPostman repository (using git clone is recommended).
    2. Open the Postman application.
    3. Import the downloaded JSON file into Postman.
    # Recommended way to get the file
    git clone https://github.com/BitgetLimited/QuickStartWithPostman
  8. Configure Bitget API Credentials

    master

    Before making API calls, you must provide your Bitget API credentials. If you do not have an API key, apply for one on the official Bitget website.

    In the provided example files (example_*.py for RestAPI and example_ws_contract.py for WebSocketAPI), populate the following variables:

    • api_key: Your Bitget API key.
    • secret_key: Your Bitget API secret key.
    • passphrase: Your Bitget API passphrase.
    api_key = ""
    secret_key = ""
    passphrase = ""