Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Bitfinex v2 REST Public Trades

See more Bitfinex v2 REST Examples

The trades endpoint allows the retrieval of past public trades and includes details such as price, size, and time. Optional parameters can be used to limit the number of results; you can specify a start and end timestamp, a limit, and a sorting method.

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();

    // Implements the following CURL command:

    // curl https://api-pub.bitfinex.com/v2/trades/tBTCUSD/hist?limit=5

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api-pub.bitfinex.com/v2/trades/tBTCUSD/hist?limit=5", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const jarr_resp = try chilkat.JsonArray.init();
    defer jarr_resp.deinit();
    jarr_resp.loadSb(sb_response_body) catch {};
    jarr_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try jarr_resp.emit(alloc)});

    const resp_status_code = http.getLastStatus();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // on trading pairs (ex. tBTCUSD)
    // [
    //   [
    //     ID,
    //     MTS,
    //     AMOUNT,
    //     PRICE
    //   ]
    // ]

    // [
    //   [
    //     472164271,
    //     1594077725121,
    //     -0.15101673,
    //     9370
    //   ],
    //   [
    //     472164270,
    //     1594077723860,
    //     -0.015,
    //     9370
    //   ],
    //   [
    //     472164269,
    //     1594077720208,
    //     -0.015,
    //     9370
    //   ],
    //   [
    //     472164267,
    //     1594077718125,
    //     -0.005,
    //     9370
    //   ],
    //   [
    //     472164251,
    //     1594077697587,
    //     -0.015,
    //     9370
    //   ]
    // ]

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    const jarr_trade = try chilkat.JsonArray.init();
    defer jarr_trade.deinit();

    var i: i32 = 0;
    const count_i = jarr_resp.getSize();
    while (i < count_i) {
        jarr_resp.arrayAt2(i, jarr_trade) catch {};

        _ = try jarr_trade.stringAt(alloc, 0);
        _ = try jarr_trade.stringAt(alloc, 1);
        _ = try jarr_trade.stringAt(alloc, 2);
        _ = try jarr_trade.stringAt(alloc, 3);
        i = i + 1;
    }
}