Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add .env
  • Loading branch information
jason.hsu
jason.hsu committed Dec 19, 2025
commit 75a20fc4eb115843420c116b708a8c56c4cdde61
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# OKX API Credentials
# Copy this file to .env and fill in your actual credentials
# NEVER commit .env to version control!

OKX_API_KEY=your_api_key_here
OKX_API_SECRET=your_api_secret_here
OKX_PASSPHRASE=your_passphrase_here

# Optional: Set to '0' for live trading, '1' for demo trading
OKX_FLAG=1
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ build/

### VS Code ###
.vscode/
id_rsa*
id_rsa*

# Environment files
.env
.env.local
.env.*.local
35 changes: 35 additions & 0 deletions test/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Test configuration module - loads API credentials from environment variables.

Usage:
from test.config import get_api_credentials

api_key, api_secret, passphrase, flag = get_api_credentials()
"""
import os
from pathlib import Path

# Try to load from .env file if python-dotenv is available
try:
from dotenv import load_dotenv
# Load .env from project root
env_path = Path(__file__).parent.parent / '.env'
load_dotenv(env_path)
except ImportError:
pass # python-dotenv not installed, rely on system environment variables


def get_api_credentials():
"""
Get API credentials from environment variables.

Returns:
tuple: (api_key, api_secret, passphrase, flag)
"""
api_key = os.getenv('OKX_API_KEY', '')
api_secret = os.getenv('OKX_API_SECRET', '')
passphrase = os.getenv('OKX_PASSPHRASE', '')
flag = os.getenv('OKX_FLAG', '1') # Default to demo trading

return api_key, api_secret, passphrase, flag

7 changes: 3 additions & 4 deletions test/AccountTest.py → test/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from loguru import logger

from okx import Account
from test.config import get_api_credentials


class AccountTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.AccountAPI = Account.AccountAPI(api_key, api_secret_key, passphrase, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.AccountAPI = Account.AccountAPI(api_key, api_secret_key, passphrase, flag=flag)

# '''
# POSITIONS_HISTORY = '/api/v5/account/positions-history' #need add
Expand Down
9 changes: 5 additions & 4 deletions test/BlockTradingTest.py → test/test_block_trading.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

import unittest
from okx import BlockTrading
from test.config import get_api_credentials


class BlockTradingTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.BlockTradingAPI = BlockTrading.BlockTradingAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.BlockTradingAPI = BlockTrading.BlockTradingAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)

"""
def test_get_counter_parties(self):
Expand Down
11 changes: 6 additions & 5 deletions test/ConvertTest.py → test/test_convert.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import unittest
from ..okx import Convert
from okx import Convert
from test.config import get_api_credentials


class ConvertTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.ConvertAPI = Convert.ConvertAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.ConvertAPI = Convert.ConvertAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)
'''

def test_get_currencies(self):
Expand Down
8 changes: 4 additions & 4 deletions test/CopyTradingTest.py → test/test_copy_trading.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import unittest
from okx import CopyTrading
from test.config import get_api_credentials


class CopyTradingTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.StackingAPI = CopyTrading.CopyTradingAPI(api_key, api_secret_key, passphrase, use_server_time=False,
flag='0')
flag=flag)

# def test_get_existing_leading_positions(self):
# print(self.StackingAPI.get_existing_leading_positions(instId='DOGE-USDT-SWAP'))
Expand Down
8 changes: 4 additions & 4 deletions test/EthStakingTest.py → test/test_eth_staking.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
from okx.Finance import EthStaking
from test.config import get_api_credentials


class EthStakingTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.StackingAPI = EthStaking.EthStakingAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.StackingAPI = EthStaking.EthStakingAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)

def test_eth_product_info(self):
print(self.StackingAPI.eth_product_info())
Expand Down
8 changes: 4 additions & 4 deletions test/FlexibleLoanTest.py → test/test_flexible_loan.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
from okx.Finance import FlexibleLoan
from test.config import get_api_credentials


class FlexibleLoanTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.FlexibleLoanAPI = FlexibleLoan.FlexibleLoanAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.FlexibleLoanAPI = FlexibleLoan.FlexibleLoanAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)

def test_borrow_currencies(self):
print(self.FlexibleLoanAPI.borrow_currencies())
Expand Down
8 changes: 4 additions & 4 deletions test/FundingTest.py → test/test_funding.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

import unittest
from okx import Funding
from test.config import get_api_credentials


class FundingTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.FundingAPI = Funding.FundingAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='0')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.FundingAPI = Funding.FundingAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)
"""
CANCEL_WITHDRAWAL = '/api/v5/asset/cancel-withdrawal' #need add
CONVERT_DUST_ASSETS = '/api/v5/asset/convert-dust-assets' #need add
Expand Down
8 changes: 4 additions & 4 deletions test/GridTest.py → test/test_grid.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

import unittest
from okx import Grid
from test.config import get_api_credentials


class GridTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.GridAPI = Grid.GridAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1', debug=False)
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.GridAPI = Grid.GridAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag, debug=False)
"""
GRID_COMPUTE_MARIGIN_BALANCE = '/api/v5/tradingBot/grid/compute-margin-balance'
GRID_MARGIN_BALANCE = '/api/v5/tradingBot/grid/margin-balance'
Expand Down
9 changes: 5 additions & 4 deletions test/MarketTest.py → test/test_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
BLOCK_TRADES = '/api/v5/market/block-trades'#need to add
'''

from test.config import get_api_credentials


class MarketAPITest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.MarketApi = MarketData.MarketAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.MarketApi = MarketData.MarketAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)
'''

def test_index_component(self):
Expand Down
9 changes: 5 additions & 4 deletions test/PublicDataTest.py → test/test_public_data.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import unittest
from okx import PublicData
from test.config import get_api_credentials


class publicDataTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.publicDataApi = PublicData.PublicAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.publicDataApi = PublicData.PublicAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)
'''
TestCase For:
INTEREST_LOAN = '/api/v5/public/interest-rate-loan-quota' #need to add
Expand Down
8 changes: 4 additions & 4 deletions test/SavingsTest.py → test/test_savings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
from okx.Finance import Savings
from test.config import get_api_credentials


class SavingsTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.StackingAPI = Savings.SavingsAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.StackingAPI = Savings.SavingsAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)


def test_get_saving_balance(self):
Expand Down
8 changes: 4 additions & 4 deletions test/SolStakingTest.py → test/test_sol_staking.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
from okx.Finance import SolStaking
from test.config import get_api_credentials


class SolStakingTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.StackingAPI = SolStaking.SolStakingAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.StackingAPI = SolStaking.SolStakingAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)

def test_sol_purchase(self):
print(self.StackingAPI.sol_purchase(amt="1"))
Expand Down
9 changes: 5 additions & 4 deletions test/SpreadTest.py → test/test_spread.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import unittest
from okx import SpreadTrading
from test.config import get_api_credentials


class TradeTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.tradeApi = SpreadTrading.SpreadTradingAPI(api_key, api_secret_key, passphrase, False, '1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.tradeApi = SpreadTrading.SpreadTradingAPI(api_key, api_secret_key, passphrase, False, flag)

# def test_place_order(self):
# print(self.tradeApi.place_order(sprdId='BTC-USDT_BTC-USDT-SWAP',clOrdId='b15',side='buy',ordType='limit',
Expand Down
7 changes: 3 additions & 4 deletions test/StakingDefiTest.py → test/test_staking_defi.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import unittest
from okx.Finance import StakingDefi
from test.config import get_api_credentials


class StakingDefiTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.StackingAPI = StakingDefi.StakingDefiAPI(api_key, api_secret_key, passphrase, use_server_time=False,
flag='1')
flag=flag)

def test_get_offers(self):
print(self.StackingAPI.get_offers(ccy="USDT"))
Expand Down
8 changes: 4 additions & 4 deletions test/SubAccountTest.py → test/test_sub_account.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
from okx import SubAccount
from test.config import get_api_credentials


class SubAccountTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.SubAccountApi = SubAccount.SubAccountAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag='1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.SubAccountApi = SubAccount.SubAccountAPI(api_key, api_secret_key, passphrase, use_server_time=False, flag=flag)
'''
ENTRUST_SUBACCOUNT_LIST = '/api/v5/users/entrust-subaccount-list' #need to add
SET_TRSNSFER_OUT = '/api/v5/users/subaccount/set-transfer-out' #need to add
Expand Down
7 changes: 3 additions & 4 deletions test/TradeTest.py → test/test_trade.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import unittest

from okx import Trade
from test.config import get_api_credentials


class TradeTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
self.tradeApi = Trade.TradeAPI(api_key, api_secret_key, passphrase, False, '1')
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.tradeApi = Trade.TradeAPI(api_key, api_secret_key, passphrase, False, flag)

# # """
# def test_place_order(self):
Expand Down
9 changes: 4 additions & 5 deletions test/TradingDataTest.py → test/test_trading_data.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import unittest
from ..okx import TradingData
from okx import TradingData
from test.config import get_api_credentials


class TradingDataTest(unittest.TestCase):
def setUp(self):
api_key = 'your_apiKey'
api_secret_key = 'your_secretKey'
passphrase = 'your_secretKey'
api_key, api_secret_key, passphrase, flag = get_api_credentials()
self.TradingDataAPI = TradingData.TradingDataAPI(api_key, api_secret_key, passphrase, use_server_time=False,
flag='1')
flag=flag)
"""
def test_get_support_coins(self):
print(self.TradingDataAPI.get_support_coin())
Expand Down
8 changes: 5 additions & 3 deletions test/WsPrivateAsyncTest.py → test/test_ws_private_async.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import asyncio

from okx.websocket.WsPrivateAsync import WsPrivateAsync
from test.config import get_api_credentials


def privateCallback(message):
print("privateCallback", message)


async def main():
api_key, api_secret_key, passphrase, _ = get_api_credentials()
url = "wss://wspap.okx.com:8443/ws/v5/private?brokerId=9999"
ws = WsPrivateAsync(
apiKey="your apiKey",
passphrase="your passphrase",
secretKey="your secretKey",
apiKey=api_key,
passphrase=passphrase,
secretKey=api_secret_key,
url=url,
useServerTime=False
)
Expand Down
File renamed without changes.
Loading