Official SDK
Online Support avatar
Written by Online Support
Updated over a week ago

Official SDK

Signed example

1

/**

2

* 生成sign

3

**/

4

private String toSign(String timestamp, String method, String requestPath,

5

String queryString, String body, String secretKey) throws Exception {

6

// 签名串

7

String preHash = preHash(timestamp, method, requestPath, queryString, body);

8

byte[] secretKeyBytes = secretKey.getBytes("UTF-8");

9

SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes, "HmacSHA256");

10

Mac mac = (Mac) MAC.clone();

11

mac.init(secretKeySpec);

12

return Hex.encodeHexString(mac.doFinal(preHash.getBytes("UTF-8")));

13

}

14

15

/**

16

* 签名串

17

**/

18

private String preHash(String timestamp, String method, String requestPath, String queryString, String body) {

19

20

StringBuilder preHash = new StringBuilder();

21

preHash.append(timestamp);

22

preHash.append(method.toUpperCase());

23

preHash.append(requestPath);

24

if (org.apache.commons.lang3.StringUtils.isNotEmpty(queryString)) {

25

preHash.append("?").append(queryString);

26

}

27

if (org.apache.commons.lang3.StringUtils.isNotEmpty(body)) {

28

preHash.append(body);

29

}

30

return preHash.toString();

31

}

32

33

/**

34

* queryString

35

**/

36

private String queryString(ServerHttpRequest request) {

37

String url = request.getURI().toString();

38

String queryString = "";

39

if (url.contains("?")) {

40

queryString = url.substring(url.lastIndexOf("?") + 1);

41

}

42

return queryString;

43

}

44

Copied!

Here is a example of how to create order

  • Java

1

OkHttpClient client = new OkHttpClient().newBuilder()

2

.build();

3

MediaType mediaType = MediaType.parse("application/json");

4

RequestBody body = RequestBody.create(mediaType, "{\"symbol\":\"BTCUSDT\",\"volume\":1,\"side\":\"BUY\",\"type\":\"LIMIT\",\"price\":10000,\"newClientOrderId\":\"\",\"recvWindow\":5000}");

5

Request request = new Request.Builder()

6

7

.method("POST", body)

8

.addHeader("X-CH-APIKEY", "Your API key")

9

.addHeader("X-CH-TS", "1596543296058")

10

.addHeader("Content-Type", "application/json")

11

.addHeader("X-CH-SIGN", "encrypt sign")

12

.build();

13

Response response = client.newCall(request).execute();

Copied!

  • Go

1

package main

2

3

import (

4

"fmt"

5

"strings"

6

"net/http"

7

"io/ioutil"

8

)

9

10

func main() {

11

12

13

method := "POST"

14

15

payload := strings.NewReader("{\"symbol\":\"BTCUSDT\",\"volume\":1,\"side\":\"BUY\",\"type\":\"LIMIT\",\"price\":10000,\"newClientOrderId\":\"\",\"recvWindow\":5000}")

16

17

client := &http.Client {

18

}

19

req, err := http.NewRequest(method, url, payload)

20

21

if err != nil {

22

fmt.Println(err)

23

}

24

req.Header.Add("X-CH-APIKEY", "Your API key")

25

req.Header.Add("X-CH-TS", "1596543881257")

26

req.Header.Add("Content-Type", "application/json")

27

req.Header.Add("X-CH-SIGN", "encrypt sign")

28

29

res, err := client.Do(req)

30

defer res.Body.Close()

31

body, err := ioutil.ReadAll(res.Body)

32

33

fmt.Println(string(body))

34

}

Copied!

  • Python

1

import requests

2

3

4

5

payload = "{\"symbol\":\"BTCUSDT\",\"volume\":1,\"side\":\"BUY\",\"type\":\"LIMIT\",\"price\":10000,\"newClientOrderId\":\"\",\"recvWindow\":5000}"

6

headers = {

7

'X-CH-APIKEY': 'Your API key',

8

'X-CH-TS': '1596543881257',

9

'Content-Type': 'application/json',

10

'X-CH-SIGN': 'encrypt sign'

11

}

12

13

response = requests.request("POST", url, headers=headers, data = payload)

14

15

print(response.text.encode('utf8'))

16

Copied!

  • Php

1

<?php

2

require_once 'HTTP/Request2.php';

3

$request = new HTTP_Request2();

4

$request->setUrl('https://openapi.xxx.com');

5

$request->setMethod(HTTP_Request2::METHOD_POST);

6

$request->setConfig(array(

7

'follow_redirects' => TRUE

8

));

9

$request->setHeader(array(

10

'X-CH-APIKEY' => 'Your API key',

11

'X-CH-TS' => '1596543881257',

12

'Content-Type' => 'application/json',

13

'X-CH-SIGN' => 'encrypt sign'

14

));

15

$request->setBody('{"symbol":"BTCUSDT","volume":1,"side":"BUY","type":"LIMIT","price":10000,"newClientOrderId":"","recvWindow":5000}');

16

try {

17

$response = $request->send();

18

if ($response->getStatus() == 200) {

19

echo $response->getBody();

20

}

21

else {

22

echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .

23

$response->getReasonPhrase();

24

}

25

}

26

catch(HTTP_Request2_Exception $e) {

27

echo 'Error: ' . $e->getMessage();

28

}

Copied!

  • NodeJs

1

var request = require('request');

2

var options = {

3

'method': 'POST',

4

5

'headers': {

6

'X-CH-APIKEY': 'Your API key',

7

'X-CH-TS': '1596543881257',

8

'Content-Type': 'application/json',

9

'X-CH-SIGN': 'encrypt sign'

10

},

11

body:JSON.stringify({"symbol":"BTCUSDT","volume":1,"side":"BUY","type":"LIMIT","price":10000,"newClientOrderId":"","recvWindow":5000})

12

13

};

14

request(options, function (error, response) {

15

if (error) throw new Error(error);

16

console.log(response.body);

17

});

Did this answer your question?