Sample code for 30+ languages & platforms
Swift

Bitfinex v2 REST Submit Order

See more Bitfinex v2 REST Examples

Submit an order.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    let crypt = CkoCrypt2()!

    var apiPath: String? = "v2/auth/w/order/submit"
    var apiKey: String? = "MY_API_KEY"
    var apiSecret: String? = "MY_API_SECRET"

    let dt = CkoDateTime()!
    dt.setFromCurrentSystemTime()

    let sbNonce = CkoStringBuilder()!
    sbNonce.append(value: dt.get(asUnixTimeStr: false))
    sbNonce.append(value: "000")
    var nonce: String? = sbNonce.getAsString()

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "type", value: "LIMIT")
    json.updateString(jsonPath: "symbol", value: "tBTCUSD")
    json.updateString(jsonPath: "price", value: "15")
    json.updateString(jsonPath: "amount", value: "0.001")
    json.updateInt(jsonPath: "flags", value: 0)
    var body: String? = json.emit()

    let sbSignature = CkoStringBuilder()!
    sbSignature.append(value: "/api/")
    sbSignature.append(value: apiPath)
    sbSignature.append(value: nonce)
    sbSignature.append(value: body)

    crypt.encodingMode = "hex_lower"
    crypt.hashAlgorithm = "sha384"
    crypt.macAlgorithm = "hmac"
    crypt.setMacKeyString(key: apiSecret)

    var sig: String? = crypt.macStringENC(inText: sbSignature.getAsString())

    http.setRequestHeader(name: "bfx-apikey", value: apiKey)
    http.setRequestHeader(name: "bfx-signature", value: sig)
    http.setRequestHeader(name: "bfx-nonce", value: nonce)

    let resp = CkoHttpResponse()!
    success = http.httpStr(verb: "POST", url: "https://api.bitfinex.com/v2/auth/w/order/submit", bodyStr: body, charset: "utf-8", contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("Response body:")
    print("\(resp.bodyStr!)")

}