Sample code for 30+ languages & platforms
Zig

Bitfinex v2 REST Submit Order

See more Bitfinex v2 REST Examples

Submit an order.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const http = try chilkat.Http.init();
    defer http.deinit();

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();

    const api_path = "v2/auth/w/order/submit";
    const api_key = "MY_API_KEY";
    const api_secret = "MY_API_SECRET";

    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    dt.setFromCurrentSystemTime() catch {};

    const sb_nonce = try chilkat.StringBuilder.init();
    defer sb_nonce.deinit();
    sb_nonce.append(try dt.getAsUnixTimeStr(alloc, false)) catch {};
    sb_nonce.append("000") catch {};
    const nonce = try sb_nonce.getAsString(alloc);

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("type", "LIMIT") catch {};
    json.updateString("symbol", "tBTCUSD") catch {};
    json.updateString("price", "15") catch {};
    json.updateString("amount", "0.001") catch {};
    json.updateInt("flags", 0) catch {};
    const body = try json.emit(alloc);

    const sb_signature = try chilkat.StringBuilder.init();
    defer sb_signature.deinit();
    sb_signature.append("/api/") catch {};
    sb_signature.append(api_path) catch {};
    sb_signature.append(nonce) catch {};
    sb_signature.append(body) catch {};

    crypt.setEncodingMode("hex_lower");
    crypt.setHashAlgorithm("sha384");
    crypt.setMacAlgorithm("hmac");
    crypt.setMacKeyString(api_secret) catch {};

    const sig = try crypt.macStringENC(alloc, try sb_signature.getAsString(alloc));

    http.setRequestHeader("bfx-apikey", api_key);
    http.setRequestHeader("bfx-signature", sig);
    http.setRequestHeader("bfx-nonce", nonce);

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpStr("POST", "https://api.bitfinex.com/v2/auth/w/order/submit", body, "utf-8", "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response body:\n", .{});
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
}