Prerequisites
-
Contact your CSM to enable this feature and get access to:
- Get Exchange Accounts Credentials Public Key endpoint
- Add an Exchange Account endpoint
-
Make sure to prepare your workspace and perform all the steps necessary here.
-
Install all the prerequisites for our TypeScript and Python SDK.
Add an Exchange Account via SDK
You can use the following code to add an exchange account
This endpoint currently only supports the following exchanges INDEPENDENT_RESERVE,BIT, BITHUMB, BITSO, CRYPTOCOM, BYBIT_V2, WHITEBIT , HITBTC, GEMINI, HUOBI, GATEIO, COINHAKO, BULLISH, BITGET, and LUNO
If you’re connecting a sub-account (for example, on exchanges that support account hierarchies), include subAccountId in the credentials object. This field is required for sub-account connections and should be set to your exchange sub-account ID.
import base64
import json
from fireblocks.client import Fireblocks
from fireblocks.client_configuration import ClientConfiguration
from fireblocks.base_path import BasePath
from fireblocks.models.add_exchange_account_request import AddExchangeAccountRequest
from cryptography.hazmat.primitives.serialization import load_pem_public_key
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from pprint import pprint
my_api_key="your_api_key"
with open('your_secret_key_file_path', 'r') as file:
secret_key_value = file.read()
configuration = ClientConfiguration(
api_key=my_api_key,
secret_key=secret_key_value,
base_path=BasePath.US #Make sure to use the correct environment. Please see the note
)
with Fireblocks(configuration) as fireblocks:
tenant_id = None
public_key = None
try:
# Get exchange accounts credentials public key
api_response = fireblocks.exchange_accounts.get_exchange_accounts_credentials_public_key().result()
tenant_id = api_response.data.tenant_id
public_key = api_response.data.public_key
except Exception as e:
print("Exception when calling ExchangeAccountAPI->get_exchange_accounts_credentials_public_key: %s\n" % e)
exit(1)
# Credentials encryption
exchange_api_key = "your_exchange_account_api_key"
exchange_api_secret = 'your_exchange_account_secret_key'
credentials = {
"apiKey": exchange_api_key,
"secret": exchange_api_secret,
"tenantId": tenant_id,
# Optional for sub-accounts:
# "subAccountId": sub_account_id,
}
pem_public_key = load_pem_public_key(bytearray(public_key, 'utf-8'), default_backend())
credentials_str = bytes(json.dumps(credentials, separators=(',', ':')), 'utf-8')
ciphertext = pem_public_key.encrypt(
credentials_str,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
encrypted_creds = bytes.decode(base64.b64encode(ciphertext))
# Prepare add exchange account request
add_exchange_account_request: AddExchangeAccountRequest = AddExchangeAccountRequest(
exchange_type='BIT',
name='My BIT account',
creds=encrypted_creds,
key=exchange_api_key
)
try:
# Add an exchange account
future = fireblocks.exchange_accounts.add_exchange_account(add_exchange_account_request=add_exchange_account_request)
api_response = future.result() # Wait for the response
print("The response of ExchangeAccountAPI->add_exchange_account:\n")
pprint(api_response.data.to_json())
except Exception as e:
print("Exception when calling ExchangeAccountAPI->add_exchange_account: %s\n" % e)
Make sure you’re using the correct value for the API base URL for your environment:
BasePath.US - for production workspaces
BasePath.Sandbox - for sandbox workspaces