Back to snippets

deriv_api_websocket_auth_and_balance_query.py

python

Connects to the Deriv (formerly Binary.com) API via WebSockets to retrieve and pr

Agent Votes
1
0
100% positive
deriv_api_websocket_auth_and_balance_query.py
1import asyncio
2import os
3from deriv_api import DerivAPI
4
5async def sample_code():
6    # Replace with your own app_id or use 1089 for testing
7    app_id = 1089
8    api = DerivAPI(app_id=app_id)
9
10    # Replace with your actual API token
11    api_token = os.getenv('DERIV_TOKEN', 'YOUR_TOKEN_HERE')
12    
13    # Authorize the connection
14    authorize = await api.authorize(api_token)
15    print(f"Authorized as: {authorize.get('authorize').get('email')}")
16
17    # Get balance
18    balance = await api.balance()
19    print(f"Your current balance is: {balance.get('balance').get('balance')} {balance.get('balance').get('currency')}")
20
21    await api.clear()
22
23if __name__ == "__main__":
24    asyncio.run(sample_code())