https://github.com/designmeme/python-upbit-api
๐ Python Upbit API Wrapper
https://github.com/designmeme/python-upbit-api
python-api-wrapper upbit upbit-api
Last synced: about 2 months ago
JSON representation
๐ Python Upbit API Wrapper
- Host: GitHub
- URL: https://github.com/designmeme/python-upbit-api
- Owner: designmeme
- License: mit
- Created: 2023-04-28T03:14:53.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-08-13T02:28:30.000Z (7 months ago)
- Last Synced: 2025-11-27T16:56:16.768Z (3 months ago)
- Topics: python-api-wrapper, upbit, upbit-api
- Language: Python
- Homepage:
- Size: 219 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Python Upbit API
๐ Python Upbit API๋ ๊ฐ๋จํ๊ณ ๋ช
๋ฃํ Upbit API Wrapper ์
๋๋ค. ๐
[][pypi]
[][pypi]
[][license]
[][downloads]
## Installation
pip ๋ช
๋ น์ด๋ก ๊ฐ๋จํ๊ฒ ์ค์นํ ์ ์์ต๋๋ค.
```shell
pip install python-upbit-api
```
## Quick Start
### ์ธ์ฆ ์์ด ์ฌ์ฉํ๊ธฐ
๊ฐ์ข
์์ธ ์ ๋ณด๋ฅผ ์ป๊ธฐ ์ํ **QUOTATION API๋ง** ์ฌ์ฉํ๋ค๋ฉด ์ธ์ฆ ์ ๋ณด ์์ด ์ด๊ธฐํ ํ ์ฌ์ฉํฉ๋๋ค.
โ ๏ธ EXCHANGE API๋ฅผ ์ฌ์ฉํ๋ฉด `upbit.exceptions.ApiKeyError` ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
```python
# example.py
from upbit import Upbit
upbit = Upbit()
res = upbit.get_markets()
data = res.json()
upbit.get_accounts() # upbit.exceptions.ApiKeyError ๋ฐ์
```
### ์ธ์ฆ ์ฌ์ฉํ๊ธฐ
์ธ์ฆ์ด ํ์ํ **EXCHANGE API**๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๋ฉด ์ธ์ฆ ์ ๋ณด๋ฅผ ์ด๊ธฐํ์ ๋ฑ๋ก ํ ์ฌ์ฉํฉ๋๋ค.
๐๐ปโโ๏ธ QUOTATION API๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
`.env` ํ๊ฒฝ์ค์ ํ์ผ์ ๋ฐ๊ธ ๋ฐ์ ์ธ์ฆ ์ ๋ณด๋ฅผ ์ค์ ํฉ๋๋ค.
```
UPBIT_OPEN_API_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
UPBIT_OPEN_API_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```
```python
# example.py
import os
from upbit import Upbit
access_key = os.environ.get('UPBIT_OPEN_API_ACCESS_KEY')
secret_key = os.environ.get('UPBIT_OPEN_API_SECRET_KEY')
upbit = Upbit(access_key, secret_key)
res = upbit.get_accounts()
data = res.json()
```
## Features
### ์
๋นํธ REST API๋ฅผ ๋ชจ๋ ๋ฉ์๋๋ก ์ ๊ณต
[์
๋นํธ REST API][upbit docs api]๋ฅผ `Upbit` ํด๋์ค์ ๋ฉ์๋๋ก ์ ๊ณตํฉ๋๋ค.
์์ธํ ๋ด์ฉ์ [upbit.py](https://github.com/designmeme/python-upbit-api/blob/main/upbit/upbit.py) ํ์ผ ๋ด docstring ์ฐธ์กฐ.
| | | ๋ฉ์๋๋ช
|
|----|:-----------------------------|:---------------------------|
| ์์ฐ | ์ ์ฒด ๊ณ์ข ์กฐํ | get_accounts |
| ์ฃผ๋ฌธ | ์ฃผ๋ฌธ ๊ฐ๋ฅ ์ ๋ณด | get_order_chance |
| | ๊ฐ๋ณ ์ฃผ๋ฌธ ์กฐํ | get_order |
| | ์ฃผ๋ฌธ ๋ฆฌ์คํธ ์กฐํ | get_orders |
| | id๋ก ์ฃผ๋ฌธ๋ฆฌ์คํธ ์กฐํ | get_orders_by_id |
| | ์ฒด๊ฒฐ ๋๊ธฐ ์ฃผ๋ฌธ (Open Order) ์กฐํ | get_open_orders |
| | ์ข
๋ฃ๋ ์ฃผ๋ฌธ (Closed Order) ์กฐํ | get_closed_orders |
| | ์ฃผ๋ฌธ ์ทจ์ ์ ์ | delete_order |
| | ์ฃผ๋ฌธ ์ผ๊ด ์ทจ์ ์ ์ | delete_orders |
| | id๋ก ์ฃผ๋ฌธ๋ฆฌ์คํธ ์ทจ์ ์ ์ | delete_orders_by_id |
| | ์ฃผ๋ฌธํ๊ธฐ | create_order |
| ์ถ๊ธ | ์ถ๊ธ ๋ฆฌ์คํธ ์กฐํ | get_withdraws |
| | ๊ฐ๋ณ ์ถ๊ธ ์กฐํ | get_withdraw |
| | ์ถ๊ธ ๊ฐ๋ฅ ์ ๋ณด | get_withdraw_chance |
| | ๋์งํธ ์์ฐ ์ถ๊ธํ๊ธฐ | create_withdraw_coin |
| | ์ํ ์ถ๊ธํ๊ธฐ | create_withdraw_krw |
| | ์ถ๊ธ ํ์ฉ ์ฃผ์ ๋ฆฌ์คํธ ์กฐํ | get_withdraw_addresses |
| ์
๊ธ | ์
๊ธ ๋ฆฌ์คํธ ์กฐํ | get_deposits |
| | ๊ฐ๋ณ ์
๊ธ ์กฐํ | get_deposit |
| | ์
๊ธ ์ฃผ์ ์์ฑ ์์ฒญ | create_coin_address |
| | ์ ์ฒด ์
๊ธ ์ฃผ์ ์กฐํ | get_coin_addresses |
| | ๊ฐ๋ณ ์
๊ธ ์ฃผ์ ์กฐํ | get_coin_address |
| | ์ํ ์
๊ธํ๊ธฐ | create_deposit_krw |
| | ๊ณ์ ์ฃผ ํ์ธ(ํธ๋๋ธ๋ฃฐ ๊ฒ์ฆ)๊ฐ๋ฅ ๊ฑฐ๋์ ๋ฆฌ์คํธ ์กฐํ | get_vasps |
| | ์
๊ธ UUID๋ก ํธ๋๋ธ๋ฃฐ ๊ฒ์ฆํ๊ธฐ | verify_travel_rule_by_uuid |
| | ์
๊ธ TxID๋ก ํธ๋๋ธ๋ฃฐ ๊ฒ์ฆํ๊ธฐ | verify_travel_rule_by_txid |
| ์๋น์ค ์ ๋ณด | ์
์ถ๊ธ ํํฉ | get_wallet_status |
| | API ํค ๋ฆฌ์คํธ ์กฐํ | get_api_keys |
| ์์ธ ์ข
๋ชฉ ์กฐํ | ๋ง์ผ ์ฝ๋ ์กฐํ | get_markets |
| ์์ธ ์บ๋ค ์กฐํ | ์ด(Second) ์บ๋ค | get_candles_second |
| | ๋ถ(Minute) ์บ๋ค | get_candles_minute |
| | ์ผ(Day) ์บ๋ค | get_candles_day |
| | ์ฃผ(Week) ์บ๋ค | get_candles_week |
| | ์(Month) ์บ๋ค | get_candles_month |
| | ์ฐ(Year) ์บ๋ค | get_candles_year |
| ์์ธ ์ฒด๊ฒฐ ์กฐํ | ์ต๊ทผ ์ฒด๊ฒฐ ๋ด์ญ | get_trades_ticks |
| ์์ธ ํ์ฌ๊ฐ ์กฐํ | ํ์ฌ๊ฐ ์ ๋ณด | get_ticker |
| ์์ธ ํ์ฌ๊ฐ ์กฐํ | ๋ง์ผ ๋จ์ ํ์ฌ๊ฐ ์ ๋ณด | get_tickers_by_quote |
| ์์ธ ํธ๊ฐ ์กฐํ | ํธ๊ฐ ์ ๋ณด | get_orderbook |
| | ํธ๊ฐ ๋ชจ์๋ณด๊ธฐ ๋จ์ ์ ๋ณด | get_orderbook_levels |
| | ํธ๊ฐ ์ ์ฑ
์กฐํ | get_orderbook_instruments |
### Requests ์ฌ์ฉ
[Requests][requests] ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
1. `requests.Response` ๊ฐ์ฒด๋ฅผ ๊ทธ๋๋ก ๋ฐํํฉ๋๋ค.
```python
# example.py
from upbit import Upbit
upbit = Upbit()
res = upbit.get_markets()
data = res.json()
status_code = res.status_code
```
2. `requests.adapters.HTTPAdapter` ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์๋ ํ์ ๋ฑ์ ์ค์ ํ ์ ์์ต๋๋ค.
```python
# example.py
from upbit import Upbit
import requests
adapter = requests.adapters.HTTPAdapter(max_retries=3)
upbit = Upbit(http_adapter=adapter)
```
3. `requests.Session.request` ์์ฒญ ํ๋ผ๋ฏธํฐ `timeout` ๊ฐ์ ์ค์ ํ ์ ์์ต๋๋ค.
```python
# example.py
from upbit import Upbit
upbit = Upbit(timeout=(10, 5)) # ๋ชจ๋ ์์ฒญ์ ์ค์ ํ timeout
res = upbit.get_markets(timeout=10) # ์ด ์์ฒญ์๋ง ์ค์ ํ timeout
```
### ์์ธ ํด๋์ค ์ ๊ณต
๋ชจ๋ API ์์ฒญ์์ ๋ฐ์ํ `requests.exceptions.HTTPError` ์์ธ๋ ์ด ์์ธ๋ฅผ ์์ํ `upbit.exceptions.UpbitHTTPError` ์์ธ๋ก ๋ฐ์ํฉ๋๋ค.
์๋ Upbit ๊ณต์ ๋ฌธ์์ ๊ณต๊ฐํ ์๋ฌ ์ ํ์ ๋ง๋ ์์ธ ์์ ๊ฐ์ง๋๋ค.
* [์์ฒญ ์ ์ ํ](https://docs.upbit.com/docs/user-request-guide)
* [API ์ฃผ์ ์๋ฌ ์ฝ๋ ๋ชฉ๋ก](https://docs.upbit.com/docs/api-%EC%A3%BC%EC%9A%94-%EC%97%90%EB%9F%AC-%EC%BD%94%EB%93%9C-%EB%AA%A9%EB%A1%9D)
```python
# example.py
from upbit import Upbit, TooManyRequests, UpbitClientError, UpbitServerError
upbit = Upbit()
try:
res = upbit.get_markets()
except TooManyRequests as e:
status_code = e.response.status_code # 429
# ...์์ธ ์ฒ๋ฆฌ ์ฝ๋
except UpbitClientError as e:
res = e.response
# ...์์ธ ์ฒ๋ฆฌ ์ฝ๋
except UpbitServerError as e:
res = e.response
# ...์์ธ ์ฒ๋ฆฌ ์ฝ๋
```
### ์์ฌ ์์ฒญ ์ ํ์ธ
์
๋นํธ API `Remaining-Req` ์๋ต ํค๋์ ๋ด๊ธด ์์ฌ ์์ฒญ ์๋ฅผ `upbit.models.RemainingReq` ๊ฐ์ฒด๋ก ์ ๊ณต๋์ด ์ ๋ณด์ ์ฝ๊ฒ ์ ๊ทผ ํ ์ ์์ต๋๋ค.
์์ฒญ ๊ทธ๋ฃน๋ณ ์ต์ ๊ฐ์ ํ๋กํผํฐ์ ์ ์ฅํ๋ฉฐ `get_remaining_reqs()` ํจ์๋ฅผ ํธ์ถํ๋ฉด ๊ทธ๋ฃน์ ์ต์ ์์ฌ ์์ฒญ ์ ๊ฐ์ฒด๋ฅผ ํ์ธ ํ ์ ์์ต๋๋ค.
์ฐธ๊ณ : [Exchange API ์์ฌ ์์ฒญ ์ ํ์ธ ๋ฐฉ๋ฒ](https://docs.upbit.com/docs/user-request-guide#exchange-api-%EC%9E%94%EC%97%AC-%EC%9A%94%EC%B2%AD-%EC%88%98-%ED%99%95%EC%9D%B8-%EB%B0%A9%EB%B2%95)
```python
# example.py
from upbit import Upbit
from upbit.models import RemainingReq
upbit = Upbit()
res = upbit.get_candles_day('KRW-BTC')
rr: RemainingReq = upbit.get_remaining_reqs('candles')
rr.minute
rr.updated
# ์๋ต ํค๋๋ ๊ทธ๋๋ก ์ฌ์ฉ ๊ฐ๋ฅ
rr_text = res.headers['Remaining-Req'] # 'group=candles; min=59; sec=4'
```
### ์์ฒญ ํ๋ผ๋ฏธํฐ ๋ชจ๋ธ ์ ๊ณต
Upbit API ์์ฒญ ํ๋ผ๋ฏธํฐ์ ์ฌ์ฉํ ๋ค์ํ ๋ฐ์ดํฐ ๋ชจ๋ธ์ ํ์
์ผ๋ก ์ ๊ณตํฉ๋๋ค.
```python
# example.py
from upbit.models import OrderSide
# ...
# Type Hint
order_side: OrderSide = 'bid'
res = upbit.create_order(market='KRW-BTC', side=order_side, ord_type='limit', price='100', volume='0.01')
```
### Docstring & Type Hint
์ ์์ฑํ Docstring ๋ด์ฉ๊ณผ ํ์
์ผ๋ก IDE์์ ์ฌ์ฉํ๊ธฐ ํธ๋ฆฌํฉ๋๋ค. ๐๐ก
## WebSocket
์์ง ์ง์ํ์ง ์์ต๋๋ค. [์ฐธ๊ณ ์ด์](https://github.com/designmeme/python-upbit-api/issues/7)
## Changelog
์ต์ ๋ณ๊ฒฝ ์ฌํญ์ ์๋์์ ํ์ธํ ์ ์์ต๋๋ค.
- [CHANGELOG.md][changelog]
- [PyPI Releases][releases]
[pypi]: https://pypi.org/project/python-upbit-api/
[releases]: https://pypi.org/project/python-upbit-api/#history
[changelog]: https://github.com/designmeme/python-upbit-api/blob/main/CHANGELOG.md
[license]: https://github.com/designmeme/python-upbit-api/blob/main/LICENSE
[downloads]: https://pypistats.org/packages/python-upbit-api
[requests]: https://requests.readthedocs.io/en/latest/
[upbit docs api]: https://docs.upbit.com/reference/