Placing orders with API key in different programming languages

I am fascinated by the API support that HollaEx provides in the apidocs. I managed to get it to work with my exchange URL and everything seems to be working fine.

That being said it’s a bit challenging to create the signature in different programming languages. Is there a script for different programming languages that I can use as a sample to create an order? Lets say I want place a limit buy order for btc-usdt at 30,000 price.

I am particularly interested in Java, NodeJs, Python, PHP and C#.

Thanks in advance.

1 Like

Hi flashtrade, Here’s a working example of order creation in Pyhton with signature creation flow. You should include your api key and secret for api_key and api_secret variables in the script. Also change the url variable if you are not testing on localhost

the script places a limit buy order for btc-usdt at 30,000 price with 0,001 amount

import json
import requests
import time
import hmac
import hashlib

# API
api_key = "" # use your api key here
api_secret = ""  # use your my api secret here
url = 'http://localhost/v2/order'

def get_api_expires():
        return str(int(time.time() + 60))  # Expires in 60 seconds
    
def generate_signature(PATH_URL, METHOD, api_expires, params=None):
    string_to_encode = METHOD + PATH_URL + api_expires
    if params != None:
        string_to_encode += json.dumps(params, separators=(',', ':'))
    signature = hmac.new(API_SECRET.encode(),string_to_encode.encode(),hashlib.sha256).hexdigest()
    return signature
    
def init_signature( PATH_URL, METHOD, is_ws):
    if is_ws:
        method = "CONNECT"
        path = '/stream'
        api_expires = get_api_expires()
        return method, path, api_expires
    else:   
        method = METHOD
        path = f"/v2{PATH_URL}"
        api_expires = get_api_expires()
        return method, path, api_expires

def auth_me( PATH_URL, METHOD, is_ws=False, params=None):
    method, path, api_expires = init_signature(PATH_URL, METHOD, is_ws)
    signature = generate_signature(path, method, api_expires, params=params)
    headers = {
        "api-key": API_KEY,
        "api-signature": signature,
        "api-expires": api_expires
    }
    return headers


def create_order(market, amount, price, side):
    params = {
        'symbol': market,
        'size': amount,
        'price': price,
        'side': side,
        'type': 'limit' # or 'market' for a market order
    }
    headers = auth_me("/order", "POST", params=params)
    response = requests.post(url, json=params, headers=headers)
    if response.status_code == 200:
        order_id = response.json().get('id')
        print(f'Order created, order id: {order_id}')
        return order_id
    else:
        print(f'Error creating order: {response.text}')
        return None


create_order("btc-usdt", 0.001, 30000, "buy") 
1 Like

Hi, I recommend you CCXT which use JS, PY, PHP and C#, so no Java for you :confused: . It is easier because you write with the CCXT API which translate to the exchange API.

A bit a my python as an exemple

PAIR = 'ETH/USDT'
TIMEFRAME = '4h'

#Date range needs to be converted to unix timestamp in milliseconds for ccxt !
before = datetime.now() - relativedelta(hours=4000)
before = round(before.timestamp()) * 1000

#Hollaex API object
hollaex = ccxt.hollaex({
    'apiKey': API_KEY,
    'secret': API_SECRET
})

chart_response = hollaex.fetch_ohlcv(symbol = PAIR, timeframe = TIMEFRAME, since = before)
1 Like

I had to change the url in your script to my exchange api URL followed by /v2/order and it worked. Thank you. Do you happen to know the script for other programming languages?

1 Like

I tried this but ccxt only supports hollaex. When I initialize the object I think it tries to connect to api.hollaex.com by default. How can I change that to my exchange api url? Or maybe I should add my own exchange to ccxt by copy pasting hollaex.js in ccxt?

@flashtrade Apparently, it’s possible to change the url and the api base URL. I haven’t tried but I think this is the way : Manual · ccxt/ccxt Wiki · GitHub

My guess

your_exchange = ccxt.hollaex({
    'apiKey': API_KEY,
    'secret': API_SECRET
    'urls['www']': api.yourdomain.tld
})

That’s even better on the long run

1 Like

Have you tried this method? It didn’t work for me.

1 Like

I haven’t, it was a guesstimate.

Maybe try urls[‘api’] instead ?

1 Like

the following code is how we can change the url

const exchange = new ccxtpro['hollaex'](
  {
    apiKey: 'API_KEY',
    secret: 'API_SECRET',
    'urls': {
      'api': {
        'ws': "wss://yourdomain/stream",
        'rest': "https://yourdomain"
      },
      'test': {
        'ws': "wss://yourdomain/stream",
        'rest': "https://yourdomain"
      }
    },
  }
);
3 Likes

I can confirm that this code snippet works. Thanks a lot @Ozzy and @kch

3 Likes

If you are using NodeJS, you can also utilize the hollaex-node-lib which is available on npm.

You can initialize it with your exchange API URL and websocket URL like the example below.

Note that if you are not using the websocket you don’t need to set anything for wsURL and it you dont set anything for apiURL it would automatically connect to https://api.hollaex.com so its important to set the apiURL to your exchange URL e.g. https://mydomain.com/api which is the normal format for HollaEx based exchanges.

const client = new hollaex({
	apiURL: '<EXCHANGE_API_URL>',
	wsURL: '<EXCHANGE_WS_URL>',
	apiKey: '<MY_API_KEY>',
	apiSecret: '<MY_API_SECRET>'
});

Then you can create an order like this:

client.createOrder('btc-usdt', 'buy', 'limit', 30000)

Voila!

All good mate ! ! :slight_smile:
Btw, what’s your project ?