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.


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.py

2. 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_KEY It 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.

EnvironmentRegionBase URL
sgSingaporehttps://sg-api.upbit.com/v1
idIndonesiahttps://id-api.upbit.com/v1
thThailandhttps://th-api.upbit.com/v1

You can check the Upbit API available through the SDK by clicking the link below.


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

StatusError Type
400BadRequestError
401AuthenticationError
403PermissionDeniedError
404NotFoundError
418RateLimitPenaltyError
422UnprocessableEntityError
429RateLimitError
> =500InternalServerError
N/AAPIConnectionError

Deactivate Virtual Environment

deactivate