Python
This guide explains how to call APIs using the Upbit SDK in a Python environment.
Get Started
The Upbit SDK is an official SDK library that allows you to use Upbit APIs (REST API, WebSocket) in various programming languages. It includes type definitions for all request parameters and responses, enabling auto-completion and type checking during development. You can conveniently integrate the API with minimal code without having to implement authentication, HTTP requests, or error handling yourself.
This guide explains how to call APIs using the Upbit SDK in a Python environment.
Upbit SDK Documentation
You can find documentation related to the Upbit Python SDK below.
- Getting Started Guide — Installation and basic usage of the Upbit Python SDK
- Upbit SDK API Reference — Full list of APIs supported by the SDK
- SDK Example Code — Sample Code and Detailed Usage
Python Integration Guide
Minimum Version: Python 3.9.0+
Set Up a Virtual Environment and Install the SDK
In Python, it is recommended to use a virtual environment for each project. This helps prevent package conflicts and version issues.
1. Create a Project Directory
mkdir upbit_sdk_project
cd upbit_sdk_project
touch upbit_sdk.py2. Create a Virtual Environment
python3 -m venv .venv
3. Activate the Virtual Environment
- Linux / macOS:
source .venv/bin/activate - Windows:
.venv\Scripts\activate
If activated successfully, it will be displayed as follows.
(.venv) user@computer:~/project$
4. Install the SDK
pip install upbit-sdk
Client Instance Setup
The Upbit SDK operates based on a client that includes authentication information and environment configuration.
Recommended Approach:
UPBIT_ACCESS_KEY,UPBIT_SECRET_KEYIt is not recommended to directly include your API key in the source code for security reasons.
# upbit_sdk.py
import os
from upbit import Upbit
client = Upbit(
access_key=os.environ.get("UPBIT_ACCESS_KEY"),
secret_key=os.environ.get("UPBIT_SECRET_KEY"),
environment: "sg",# environment: "sg"ㅣ"id"ㅣ"th"
)You can change the target region using the environment parameter.
| Environment | Region | Base URL |
|---|---|---|
| sg | Singapore | https://sg-api.upbit.com/v1 |
| id | Indonesia | https://id-api.upbit.com/v1 |
| th | Thailand | https://th-api.upbit.com/v1 |
You can check the Upbit API available through the SDK by clicking the link below.
- Upbit SDK API Reference: https://github.com/upbit-official/upbit-sdk-python/blob/main/api.md
Confirm Instance Configuration
To verify that the client is properly configured, write and run the code as follows.
API Calls Requiring Authentication
import os
from upbit import Upbit
def list_accounts():
client = Upbit(
access_key=os.environ.get("UPBIT_ACCESS_KEY"),
secret_key=os.environ.get("UPBIT_SECRET_KEY"),
environment="sg",
)
accounts = client.accounts.list()
print(accounts)
if __name__ == "__main__":
list_accounts()API Calls Without Authentication
from upbit import Upbit
def list_markets():
client = Upbit(
environment="sg",
)
markets = client.trading_pairs.list()
print(markets)
if __name__ == "__main__":
list_markets()Response Example
Authentication API Response
[
{
"currency": "BTC",
"balance": "0.00050000",
"locked": "0.00000000",
"avg_buy_price": "153559",
"avg_buy_price_modified": false,
"unit_currency": "SGD"
},
{
"currency": "SGD",
"balance": "1000000.00000000",
"locked": "0.00000000",
"avg_buy_price": "0",
"avg_buy_price_modified": true,
"unit_currency": "SGD"
}
]Unauthenticated API Response
[
{
"market": "SGD-BTC",
"english_name": "Bitcoin"
},
{
"market": "SGD-ETH",
"english_name": "Ethereum"
}
]Error Handling
API calls may result in errors such as authentication failures or request limit exceedance. You can handle exceptions as follows.
import os
import upbit
from upbit import Upbit
client = Upbit(
access_key=os.environ.get("UPBIT_ACCESS_KEY"),
secret_key=os.environ.get("UPBIT_SECRET_KEY"),
environment="sg",
)
try:
accounts = client.accounts.list()
print(accounts)
except upbit.APIConnectionError as e:
print("Unable to connect to the server")
print(e.__cause__)
except upbit.AuthenticationError:
print("Authentication failed. Check your Access Key and Secret Key.")
except upbit.RateLimitError:
print("Rate limited. Please wait and try again.")
except upbit.APIStatusError as e:
print(f"API error ({e.status_code}): {e.message}")Error Types
| Status | Error Type |
|---|---|
| 400 | BadRequestError |
| 401 | AuthenticationError |
| 403 | PermissionDeniedError |
| 404 | NotFoundError |
| 418 | RateLimitPenaltyError |
| 422 | UnprocessableEntityError |
| 429 | RateLimitError |
| > =500 | InternalServerError |
| N/A | APIConnectionError |
Deactivate Virtual Environment
deactivateUpdated 4 days ago
