Since there was no sample code for making a request in Go language on the Official Document of the bitFlyer Lightning API, I made a note of the survey results. In this article, I will explain the procedure for issuing a childOrder.
Basically, make a Private request to bitFlyer Lightning API in Go language according to the following procedure.
sample.go
//Function for sending a POST request with its own header and body
//Use this function for requests that cannot be handled by the http package
func NewRequest(method, url string, header map[string]string, body []byte) (*http.Request, error) {
req, err := http.NewRequest(method, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
for key, value := range header {
req.Header.Set(key, value)
}
return req, nil
}
//Functions for creating digital signatures with SHA256
//The concatenated character string and secret key are used as arguments, and the return value is a digital signature.
func MakeHMAC(text, secretKey string) string {
key := secretKey
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(text))
return hex.EncodeToString(mac.Sum(nil))
}
//Function to convert map type to string type
func MapToString(bytes map[string]interface{}) string {
b, err := json.Marshal(bytes)
if err != nil {
fmt.Println("JSON marshal error: ", err)
return "error"
}
string := string(b)
return string
}
//A function for placing child orders.
func POSTChildOrder(){
//Please enter the access key and secret key you obtained.
accessKey := XXXX
secretKey := YYYY
//Get UNIX time stamps.
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
//The method is basically GET or POST only. Use POST to place child orders.
method := "POST"
path := "/v1/me/sendchildorder"
//Request body. Change the key and value as needed.
body := map[string]interface{}{
"product_code": "ETH_JPY",
"child_order_type": "LIMIT",
"side": "BUY",
"price": 10000,
"size": 1,
"minute_to_expire": 10000,
"time_in_force": "GTC",
}
//Change the request body to string type.
sbody := MapToString(body)
//Create a string to create a digital signature with sha256.
text := timestamp + method + path + sbody
//Create a digital signature.
sign := MakeHMAC(text, secretKey)
//Create request header
header := map[string]string{
"ACCESS-KEY": accessKey,
"ACCESS-TIMESTAMP": timestamp,
"ACCESS-SIGN": sign,
"Content-Type": "application/json",
}
//Send a request
url := ”https://api.bitflyer.com” + path
//In the case of POST, the request is sent by the original request sending function.
//In case of GET, http package request can be used.
req, err := NewRequest(method, url, header, []byte(sbody))
if err != nil {
fmt.Println(err.Error())
}
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err.Error())
}
For details on how to use Private API other than child order, refer to Official Document. (I think that other APIs can be used by changing the method, path, and body in the sample code posted in this article.)
Recommended Posts