본문 바로가기
카테고리 없음

[비트코인] 비트코인 고팍스 API 사용기

by 미래미래로 2021. 4. 26.
728x90
반응형

1. 고팍스 회원가입

 

2. 계정 로그인 후 API 키 받기

* API 생성시 나타나는 secret key 복사해서 안전한 곳에 저장하기!

계정관리 -> API키 -> 새 API키 등록

3. 개발환경 확인

  - python 설치

https://www.python.org/downloads/

 

Download Python

The official home of the Python Programming Language

www.python.org

  - pycharm 설치

https://www.jetbrains.com/ko-kr/pycharm/download/

 

다운로드 PyCharm: JetBrains가 만든 전문 개발자용 Python IDE

 

www.jetbrains.com

 

4. 예제 돌려보기

gopax.github.io/API/index.html#ce7156229f

 

Gopax REST API

주소 고팍스 REST API 서버 주소는 다음과 같습니다. https://api.gopax.co.kr CCXT를 통하여 고팍스 REST API를 접근할 수 있습니다. CCXT는 서로 다른 거래소들을 단일 인터페이스를 통해 접근할 수 있게 만

gopax.github.io

import base64, hashlib, hmac, json, requests, time

API_KEY = '<입력하세요>'
SECRET = '<입력하세요>'


def call(need_auth, method, path, body_json=None, recv_window=None):
  method = method.upper()
  if need_auth:
    timestamp = str(int(time.time() * 1000))
    include_querystring = method == 'GET' and path.startswith('/orders?')
    p = path if include_querystring else path.split('?')[0]
    msg = 't' + timestamp + method + p
    msg += (str(recv_window) if recv_window else '') + (json.dumps(body_json) if body_json else '')
    raw_secret = base64.b64decode(SECRET)
    raw_signature = hmac.new(raw_secret, str(msg).encode('utf-8'), hashlib.sha512).digest()
    signature = base64.b64encode(raw_signature)
    headers = {'api-key': API_KEY, 'timestamp': timestamp, 'signature': signature}
    if recv_window:
      headers['receive-window'] = str(recv_window)
  else:
    headers = {}
  req_func = {'GET': requests.get, 'POST': requests.post, 'DELETE': requests.delete}[method]
  resp = req_func(url='https://api.gopax.co.kr' + path, headers=headers, json=body_json)
  return {
    'statusCode': resp.status_code,
    'body': resp.json(),
    'header': dict(resp.headers),
  }


post_orders_req_body = {
  'side': 'buy', 'type': 'limit', 'amount': 1,
  'price': 10000, 'tradingPairName': 'BTC-KRW'
}
print(call(True, 'POST', '/orders', post_orders_req_body, 200))
print(call(True, 'GET', '/orders'))
print(call(True, 'GET', '/orders?includePast=true'))
print(call(True, 'GET', '/trades?limit=1'))
print(call(False, 'GET', '/trading-pairs/BTC-KRW/book?level=1'))

성공!!

728x90
반응형

댓글