├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── korbit ├── __init__.py ├── private_api.py └── public_api.py ├── setup.py └── tests ├── __init__.py └── test.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | #Ipython Notebook 62 | .ipynb_checkpoints 63 | 64 | .idea -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.4" 4 | - "3.5" 5 | - "3.6" 6 | - "3.7" 7 | install: "pip install requests" 8 | script: "python -m unittest" 9 | notifications: 10 | slack: test1030team:nXUuJifoc4Rg8VFYmdW9cd1T 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 HoonJin(Daniel) Ji 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # korbit API wrapper for Python 2 | 3 | You can get detail information of API in [API Reference](https://apidocs.korbit.co.kr/) 4 | 5 | 6 | ## Installation 7 | Install from Git Repository 8 | ```sh 9 | pip install git+https://github.com/HoonJin/korbit-python.git 10 | ``` 11 | 12 | ## Basic Usage 13 | You can use public API very easily. 14 | ```python 15 | import korbit 16 | korbit.ticker() 17 | ``` 18 | 19 | ## If you want to use Exchange 20 | You have to get API key in [this page](https://www.korbit.co.kr/my/api) 21 | ```python 22 | import korbit 23 | api = korbit.PrivateAPI('your key', 'your secret') 24 | api.create_token_directly() 25 | 26 | api.market_ask_order(0.01) 27 | ``` 28 | 29 | 30 | ## License 31 | The MIT License (MIT) 32 | 33 | Copyright (c) 2016-2019 HoonJin(Daniel) Ji bwjhj1030@gmail.com 34 | 35 | Permission is hereby granted, free of charge, to any person obtaining a copy 36 | of this software and associated documentation files (the "Software"), to deal 37 | in the Software without restriction, including without limitation the rights 38 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 39 | copies of the Software, and to permit persons to whom the Software is 40 | furnished to do so, subject to the following conditions: 41 | 42 | The above copyright notice and this permission notice shall be included in all 43 | copies or substantial portions of the Software. 44 | 45 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 46 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 47 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 48 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 49 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 50 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 51 | SOFTWARE. 52 | -------------------------------------------------------------------------------- /korbit/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from .private_api import PrivateAPI, PublicAPI 4 | 5 | __public = PublicAPI() 6 | ticker = __public.ticker 7 | detailed_ticker = __public.detailed_ticker 8 | all_detailed_ticker = __public.all_detailed_ticker 9 | orderbook = __public.orderbook 10 | asks_orderbook = __public.asks_orderbook 11 | bids_orderbook = __public.bids_orderbook 12 | list_of_filled_orders = __public.list_of_filled_orders 13 | constants = __public.constants 14 | -------------------------------------------------------------------------------- /korbit/private_api.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import time 3 | from .public_api import PublicAPI 4 | 5 | 6 | class PrivateAPI(PublicAPI): 7 | def __init__(self, client_id, secret, production=True, version="v1", timeout=20): 8 | try: 9 | super(self.__class__, self).__init__(production, version, timeout) 10 | except TypeError: 11 | PublicAPI.__init__(self, production, version, timeout) 12 | self.__client_id = client_id 13 | self.__secret = secret 14 | self.__token = {} 15 | 16 | # https://apidocs.korbit.co.kr/#authentication 17 | def create_token_directly(self): 18 | payload = { 19 | 'client_id': self.__client_id, 20 | 'client_secret': self.__secret, 21 | 'grant_type': "client_credentials" 22 | } 23 | self.__token = self.request_post("oauth2/access_token", data=payload) 24 | return self.__token 25 | 26 | def set_token(self, token): 27 | self.__token = token 28 | 29 | def refresh_token(self): 30 | payload = { 31 | 'client_id': self.__client_id, 32 | 'client_secret': self.__secret, 33 | 'refresh_token': self.__token['refresh_token'], 34 | 'grant_type': "refresh_token" 35 | } 36 | self.__token = self.request_post("oauth2/access_token", data=payload) 37 | return self.__token 38 | 39 | def get_user_info(self): 40 | return self.request_get("user/info", headers=self.headers) 41 | 42 | @property 43 | def headers(self): 44 | return { 45 | 'Accept': 'application/json', 46 | 'Authorization': "{} {}".format(self.__token['token_type'], self.__token['access_token']) 47 | } 48 | 49 | # https://apidocs.korbit.co.kr/#exchange 50 | def bid_order(self, bid_type, coin_amount=None, price=None, fiat_amount=None, currency_pair="btc_krw"): 51 | payload = { 52 | 'type': bid_type, 53 | 'currency_pair': currency_pair, 54 | 'price': price, 55 | 'coin_amount': coin_amount, 56 | 'fiat_amount': fiat_amount, 57 | 'nonce': self.nonce 58 | } 59 | return self.request_post("user/orders/buy", headers=self.headers, data=payload) 60 | 61 | def market_bid_order(self, fiat_amount, currency_pair="btc_krw"): 62 | return self.bid_order('market', fiat_amount=fiat_amount, currency_pair=currency_pair) 63 | 64 | def limit_bid_order(self, coin_amount, price, currency_pair="btc_krw"): 65 | return self.bid_order('limit', coin_amount=coin_amount, price=price, currency_pair=currency_pair) 66 | 67 | def ask_order(self, ask_type, coin_amount, price=None, currency_pair="btc_krw"): 68 | payload = { 69 | 'type': ask_type, 70 | 'currency_pair': currency_pair, 71 | 'price': price, 72 | 'coin_amount': coin_amount, 73 | 'nonce': self.nonce 74 | } 75 | return self.request_post("user/orders/sell", headers=self.headers, data=payload) 76 | 77 | def market_ask_order(self, coin_amount, currency_pair="btc_krw"): 78 | return self.ask_order('market', coin_amount=coin_amount, currency_pair=currency_pair) 79 | 80 | def limit_ask_order(self, coin_amount, price, currency_pair="btc_krw"): 81 | return self.ask_order('limit', coin_amount, price, currency_pair) 82 | 83 | def cancel_order(self, ids, currency_pair="btc_krw"): 84 | payload = { 85 | 'id': ids, 86 | 'currency_pair': currency_pair, 87 | 'nonce': self.nonce 88 | } 89 | return self.request_post("user/orders/cancel", headers=self.headers, data=payload) 90 | 91 | def list_open_orders(self, offset=0, limit=10, currency_pair="btc_krw"): 92 | params = { 93 | 'currency_pair': currency_pair, 94 | 'offset': offset, 95 | 'limit': limit 96 | } 97 | return self.request_get("user/orders/open", headers=self.headers, params=params) 98 | 99 | def view_exchange_orders(self, offset=0, limit=10, currency_pair="btc_krw"): 100 | params = { 101 | 'currency_pair': currency_pair, 102 | 'offset': offset, 103 | 'limit': limit 104 | } 105 | return self.request_get("user/orders", headers=self.headers, params=params) 106 | 107 | def view_transfers(self, offset=0, limit=10, currency="btc"): 108 | params = { 109 | 'currency': currency, 110 | 'offset': offset, 111 | 'limit': limit 112 | } 113 | return self.request_get("user/transfers", headers=self.headers, params=params) 114 | 115 | def trading_volume_and_fees(self, currency_pair="all"): 116 | params = { 117 | 'currency_pair': currency_pair 118 | } 119 | return self.request_get("user/volume", headers=self.headers, params=params) 120 | 121 | # https://apidocs.korbit.co.kr/#wallet 122 | def user_balances(self): 123 | return self.request_get("user/balances", headers=self.headers) 124 | 125 | def user_accounts(self): 126 | return self.request_get("user/accounts", headers=self.headers) 127 | 128 | def retrieve_wallet_status(self, currency_pair="btc_krw"): 129 | params = { 130 | 'currency_pair': currency_pair 131 | } 132 | return self.request_get("user/wallet", headers=self.headers, params=params) 133 | 134 | def assign_btc_address(self, currency="btc"): 135 | payload = { 136 | 'currency': currency, 137 | 'nonce': self.nonce 138 | } 139 | return self.request_post("user/coins/address/assign", headers=self.headers, data=payload) 140 | 141 | def request_btc_withdrawal(self, address, amount, currency="btc"): 142 | payload = { 143 | 'address': address, 144 | 'amount': amount, 145 | 'currency': currency, 146 | 'nonce': self.nonce 147 | } 148 | return self.request_post("user/coins/out", headers=self.headers, data=payload) 149 | 150 | def status_of_btc_deposit_and_transfer(self, transfer_id="", currency="btc"): 151 | params = { 152 | 'currency': currency 153 | } 154 | if transfer_id != "": 155 | params['id'] = transfer_id 156 | 157 | return self.request_get("user/coins/status", headers=self.headers, params=params) 158 | 159 | def cancel_btc_transfer_request(self, transfer_id, currency="btc"): 160 | payload = { 161 | 'id': transfer_id, 162 | 'currency': currency, 163 | 'nonce': self.nonce 164 | } 165 | return self.request_post("user/coins/out/cancel", headers=self.headers, data=payload) 166 | 167 | @property 168 | def nonce(self): 169 | return int(time.time() * 1000) 170 | -------------------------------------------------------------------------------- /korbit/public_api.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import requests 4 | import json 5 | import logging 6 | try: 7 | from urllib.parse import urljoin 8 | except ImportError: 9 | from urlparse import urljoin 10 | 11 | 12 | class PublicAPI: 13 | def __init__(self, production=True, version="v1", timeout=20): 14 | self.__host = production and "https://api.korbit.co.kr/%s/" % version \ 15 | or "https://api.korbit-test.com/%s/" % version 16 | self.__timeout = timeout 17 | 18 | # https://apidocs.korbit.co.kr/#public 19 | def ticker(self, currency_pair="btc_krw"): 20 | params = { 21 | 'currency_pair': currency_pair 22 | } 23 | return self.request_get("ticker", params=params) 24 | 25 | def detailed_ticker(self, currency_pair="btc_krw"): 26 | params = { 27 | 'currency_pair': currency_pair 28 | } 29 | return self.request_get("ticker/detailed", params=params) 30 | 31 | def all_detailed_ticker(self): 32 | return self.request_get("ticker/detailed/all") 33 | 34 | def orderbook(self, currency_pair="btc_krw", category="all", group=True): 35 | params = { 36 | 'group': group, 37 | 'category': category, 38 | 'currency_pair': currency_pair 39 | } 40 | return self.request_get("orderbook", params=params) 41 | 42 | def bids_orderbook(self, currency_pair="btc_krw", group=True): 43 | return self.orderbook(currency_pair=currency_pair, category="bid", group=group) 44 | 45 | def asks_orderbook(self, currency_pair="btc_krw", group=True): 46 | return self.orderbook(currency_pair=currency_pair, category="ask", group=group) 47 | 48 | def list_of_filled_orders(self, currency_pair="btc_krw", interval="hour"): 49 | params = { 50 | 'time': interval, 51 | 'currency_pair': currency_pair 52 | } 53 | return self.request_get("transactions", params=params) 54 | 55 | def request_get(self, path, headers=None, params=None): 56 | response = requests.get(urljoin(self.host, path), headers=headers, params=params, timeout=self.__timeout) 57 | try: 58 | return response.json() 59 | except json.decoder.JSONDecodeError as e: 60 | logging.error("exception: {}, response_text: {}".format(e, response.text)) 61 | return response.text 62 | 63 | def request_post(self, path, headers=None, data=None): 64 | response = requests.post(urljoin(self.host, path), headers=headers, data=data, timeout=self.__timeout) 65 | try: 66 | return response.json() 67 | except json.decoder.JSONDecodeError as e: 68 | logging.error("exception: {}, response_text: {}".format(e, response.text)) 69 | return response.text 70 | 71 | @property 72 | def host(self): 73 | return self.__host 74 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | 2 | try: 3 | from setuptools import setup 4 | except ImportError: 5 | from distutils.core import setup 6 | 7 | setup( 8 | name='korbit-python', 9 | packages=['korbit'], 10 | version='0.6.0', 11 | description='korbit API wrapper for Python', 12 | url='http://github.com/Hoonjin/korbit-python/', 13 | author='Daniel Ji', 14 | author_email='bwjhj1030@gmail.com', 15 | install_requires=['requests'], 16 | classifiers=[ 17 | "Programming Language :: Python", 18 | "Programming Language :: Python :: 3", 19 | "License :: OSI Approved :: MIT License", 20 | "Operating System :: OS Independent", 21 | "Topic :: Software Development :: Libraries :: Python Modules" 22 | ], 23 | license='MIT', 24 | ) 25 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HoonJin/korbit-python/496131d1320361e8ddf4e60ec8d7269ca8451afe/tests/__init__.py -------------------------------------------------------------------------------- /tests/test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import korbit 3 | 4 | 5 | class TestKorbit(unittest.TestCase): 6 | def setUp(self): 7 | pass 8 | 9 | def test_ticker(self): 10 | ticker = korbit.ticker() 11 | self.assertTrue('last' in ticker.keys()) 12 | self.assertTrue('timestamp' in ticker.keys()) 13 | --------------------------------------------------------------------------------