This is a lightweight library that works as a connector to DeGate public SDK
- Supported APIs:
- Spot
- Spot Websocket Market Stream
- Spot User Data Stream
- Inclusion of examples
- Customizable base URL
- Response metadata can be displayed
pip install degate-connectorUsage examples:
fromdegate.spotimportSpotasClientETH= {
"id": 0,
"symbol": "ETH",
}
USDC= {
"id": 8,
"symbol": "USDC",
}
client=Client(tokens=[ETH,USDC])
# Get server timestampprint(client.time())
# Get klines of ETHUSDC at 1m intervalprint(client.klines("ETHUSDC", "1m"))
# Get last 10 klines of ETHUSDC at 1h intervalprint(client.klines("ETHUSDC", "1h", limit=10))
# accountAddress、assetPrivateKey、accountId are required for user data endpointsclient=Client(accountAddress='<account_address>',assetPrivateKey='<DeGate AssetPrivateKey>',accountId='<account_id>',tokens=[ETH,USDC])
# Get account and balance informationprint(client.account())
# Post a new orderparams= {
'symbol': 'ETHUSDC',
'side': 'SELL',
'type': 'LIMIT',
'quantity': 0.1,
'price': 9500
}
response=client.newOrder(**params)
print(response)Please find examples folder to check for more endpoints.
Spot Testnet is available, it can be used to test.
To use testnet:
fromdegate.spotimportSpotasClientclient=Client(baseUrl='https://testnet-backend.degate.com')
print(client.time())The DeGate API server provides weight usages in the headers of each response.
You can display them by initializing the client with show_header=True:
fromdegate.spotimportSpotasClientclient=Client(show_header=True)
print(client.time())returns:
{'data': {'serverTime': 1587990847650}, 'header': {'Context-Type': 'application/json;charset=utf-8', ...}}If ClientError is received, it'll display full response meta information.
Setting the log level to DEBUG will log the request URL, payload and response text.
There are 2 types of error returned from the library:
degate.error.ClientError- This is thrown when server returns
4XX, it's an issue from client side. - It has 4 properties:
status_code- HTTP status codeerror_code- Server's error code, e.g.-1102error_message- Server's error message, e.g.Unknown order sent.header- Full response header.
- This is thrown when server returns
degate.error.ServerError- This is thrown when server returns
5XX, it's an issue from server side.
- This is thrown when server returns
importtimefromdegate.websocket.spot.websocket_clientimportSpotWebsocketClientasWsClientdefmessage_handler(message):
print(message)
ETH= {
"id": 0,
"symbol": "ETH",
}
USDC= {
"id": 8,
"symbol": "USDC",
}
wsClient=WsClient(tokens=[ETH,USDC])
wsClient.start()
wsClient.subscribeTicker(
symbol="ETHUSDC",
id=1,
callback=message_handler,
)
time.sleep(300)
wsClient.stop()More websocket examples are available in the examples folder
fromdegate.websocket.spot.websocket_clientimportSpotWebsocketClientasWsClientwsClient=WsClient(websocketBaseUrl='wss://testnet-ws.degate.com')