import asyncio import websockets import json REQ_URL = 'wss://api.coinxbit.online/ws/api/v2' ACC_EMAIL = 'morellodemis@tiscali.it' ACC_PASS = 'Vargod@88' CL_ID = '7rUN8Kut' CL_SECRET = 'L5oCev5Pw9IjloNTgQzKvozCioAFsg' msg = { "jsonrpc": "2.0", "id": 1, "params": {} } async def auth_api(): global msg global acc_token msg["method"] = "public/auth" msg["params"] = { "grant_type": "client_credentials", "client_id": CL_ID, "client_secret": CL_SECRET, "account_email": ACC_EMAIL, "account_password": ACC_PASS, "scope": "session:test" } async with websockets.connect(REQ_URL) as websocket: await websocket.send(json.dumps(msg)) while websocket.open: response = await websocket.recv() response_json = json.loads(response) acc_token = response_json["result"]["access_token"] return async def call_api(instrument): msg = { "jsonrpc" : "2.0", "id" : 1, "method" : "private/buy", "params" : { "instrument_name" : "ETH-PERPETUAL", "amount" : 40, "type" : "market"} } async with websockets.connect('wss://api.coinxbit.online/ws/api/v2') as websocket: await websocket.send(msg) while websocket.open: response = await websocket.recv() print(response) return response async def main(): global msg await auth_api() async with websockets.connect(REQ_URL) as websocket: response = await call_api("BTC-PERPETUAL") print(response) asyncio.get_event_loop().run_until_complete(main()) #asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))