How to Construct an HTTP Request

Once the signature is calculated the user can make a request to the endpoint of interest by sending:

  1. the HTTP method (POST, GET, etc)
  2. the full end point (this is the base URL and the path)
  3. the message header
  4. the message body

REST V3
HEADERAPI Key
API Signature
BODYTonce Unix time

Example Request Method

Here is an example of how to send the request.

requests.request(method, base_url + "/" + path, headers = headers, data = body_str)

Full REST Examples

These full examples bring all the methods together to demonstrate how to generate the API signature and send it along with the other important data.

The responses associated to these (and all end point requests) are documented in the relevant sections later in the document.

import time
import json
import base64
import hashlib
import hmac
import requests
import pprint
base_url = "https://trade-uk.sandbox.zodiamarkets.com"
key = "<key>"
secret = "<secret>"

def gen_sig_helper(secret, data):
  secret_bytes = base64.b64decode(secret.encode("utf8"))
  return base64.b64encode(hmac.new(secret_bytes, data.encode("utf8"), digestmod = hashlib.sha512).digest()).decode("utf8")

def v3_gen_sig(secret, path, body_str = None):
  data = path
  if body_str != None:
    data = data + chr(0) + body_str
  return gen_sig_helper(secret, data)

def v3_mk_request(method, path, body = {}, realrun = True):
  print("=> " + method + ' ' + path)
  tonce = int(time.time() * 1000 * 1000)
  body["tonce"] = tonce
  body_str = json.dumps(body)
  headers = {
    "Rest-Key": key,
    "Rest-Sign": v3_gen_sig(secret, path, body_str),
    "Content-Type": "application/json"
  }
  print("=> " + v3_gen_sig(secret, path, body_str))
  print("=> " + str(tonce))
  print("=> " + body_str)
  
  if realrun:
    response = requests.request(method, base_url + "/" + path, headers = headers, data = body_str)
    response_json = response.json()
    pprint.pprint(response_json)
    response.raise_for_status()
    return response_json
  else:
    print("=> Not sending messages to API server...")
    return 0
  
def request_acc():
return v3_mk_request("POST", "api/3/account")

request_acc()
import time
import json
import base64
import hashlib
import hmac
import requests
import pprint
base_url = "https://trade-uk.sandbox.zodiamarkets.com"
key = "<key>"
secret = "<secret>"

def gen_sig_helper(secret, data):
  secret_bytes = base64.b64decode(secret.encode("utf8"))
  return base64.b64encode(hmac.new(secret_bytes, data.encode("utf8"), digestmod = hashlib.sha512).digest()).decode("utf8")

def v3_gen_sig(secret, path, body_str = None):
  data = path
  if body_str != None:
    data = data + chr(0) + body_str    
  return gen_sig_helper(secret, data)

def v3_mk_request(method, path, body = {}, realrun = True):
  print("=> " + method + ' ' + path)
  tonce = int(time.time() * 1000 * 1000)
  body["tonce"] = tonce
  body_str = json.dumps(body)
  headers = {
    "Rest-Key": key,
    "Rest-Sign": v3_gen_sig(secret, path, body_str),
    "Content-Type": "application/json"
  }
  print("=> " + v3_gen_sig(secret, path, body_str))
  print("=> " + str(tonce))
  print("=> " + body_str)
  
  if realrun:
    response = requests.request(method, base_url + "/" + path, headers = headers, data = body_str)
    response_json = response.json()
    pprint.pprint(response_json)
    response.raise_for_status()
    return response_json
  else:
    print("=> Not sending messages to API server...")
    return 0
 

def request_token():
      return v3_mk_request("POST", "api/3/zm/rest/auth/token")
  
request_token()