How to make a request to bitFlyer Lightning's Private API in Go language

How to make a request to bitFlyer Lightning's Private API in Go language

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.

Overview

Basically, make a Private request to bitFlyer Lightning API in Go language according to the following procedure.

  1. Log in to bitFlyer and get the access key and secret key.
  2. Create a digital signature with SHA256. The UNIX time stamp, method, api path, and request body are concatenated as a character string, and those character strings are hashed with SHA256 using a secret key as a digital signature.
  3. Create a request header using the access key, digital signature and time stamp.
  4. Send the request using the function to send your own request. (Can't it be implemented with the function included in the http package?)

Sample code

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())
	}

Supplement

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

How to make a request to bitFlyer Lightning's Private API in Go language
How to create a Rest Api in Django
Try to make a Python module in C language
[Go language] How to get terminal input in real time
How to temporarily implement a progress bar in a scripting language
How to make a Japanese-English translation
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
Post to slack in Go language
How to make a deadman's switch
How to make a crawler --Basic
Created a package to support AWS Lambda development in Go language
How to make a string into an array or an array into a string in Python
[C language] How to create, avoid, and make a zombie process
Make a request from Device Farm (appium python) to API Gateway
How to send a request to the DMM (FANZA) API with python
[Python] How to make a class iterable
How to get a stacktrace in python
I tried to make a Web API
How to make a Backtrader custom indicator
How to make a Pelican site map
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
How to make a container name a subdomain and make it accessible in Docker
How to make a dialogue system dedicated to beginners
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to make a dictionary with a hierarchical structure.
How to multi-process exclusive control in C language
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
How to implement a gradient picker in Houdini
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to write a named tuple document in 2020
How to count numbers in a specific range
[Go] How to write or call a function
How to read a file in a different directory
How to make Python Interpreter changes in Pycharm
How to Mock a Public function in Pytest
Create a web server in Go language (net/http) (2)
How to display the modification date of a file in C language up to nanoseconds
You can do it in 3 minutes! How to make a moving QR code (GIF)!
How to limit the API to be published in the C language shared library of Linux
I wrote a CLI tool in Go language to view Qiita's tag feed with CLI
How to specify a schema in Django's database settings
How to convert / restore a string with [] in python
Explain in detail how to make sounds with python
[Python] How to expand variables in a character string
How to make a shooting game with toio (Part 1)
[Go] How to create a custom error for Sentry
A memorandum on how to use keras.preprocessing.image in Keras
How to make an interactive CLI tool in Golang
How to display DataFrame as a table in Markdown
How to make an HTTPS server with Go / Gin
How to deploy a Go application to an ECS instance
Basics of PyTorch (2) -How to make a neural network-
How to use Decorator in Django and how to make it
How to post a ticket from the Shogun API
How to execute a command using subprocess in Python
How to reference static files in a Django project
Go language to see and remember Part 7 C language in GO language