Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Bitfinex v2 REST Submit Order

See more Bitfinex v2 REST Examples

Submit an order.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.CkDateTime,
  Chilkat.HttpResponse,
  Chilkat.StringBuilder,
  Chilkat.JsonObject,
  Chilkat.Crypt2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  crypt: TCrypt2;
  apiPath: string;
  apiKey: string;
  apiSecret: string;
  dt: TCkDateTime;
  sbNonce: TStringBuilder;
  nonce: string;
  json: TJsonObject;
  body: string;
  sbSignature: TStringBuilder;
  sig: string;
  resp: THttpResponse;

begin
  success := False;

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

  http := THttp.Create;

  crypt := TCrypt2.Create;

  apiPath := 'v2/auth/w/order/submit';
  apiKey := 'MY_API_KEY';
  apiSecret := 'MY_API_SECRET';

  dt := TCkDateTime.Create;
  dt.SetFromCurrentSystemTime();

  sbNonce := TStringBuilder.Create;
  sbNonce.Append(dt.GetAsUnixTimeStr(False));
  sbNonce.Append('000');
  nonce := sbNonce.GetAsString();

  json := TJsonObject.Create;
  json.UpdateString('type','LIMIT');
  json.UpdateString('symbol','tBTCUSD');
  json.UpdateString('price','15');
  json.UpdateString('amount','0.001');
  json.UpdateInt('flags',0);
  body := json.Emit();

  sbSignature := TStringBuilder.Create;
  sbSignature.Append('/api/');
  sbSignature.Append(apiPath);
  sbSignature.Append(nonce);
  sbSignature.Append(body);

  crypt.EncodingMode := 'hex_lower';
  crypt.HashAlgorithm := 'sha384';
  crypt.MacAlgorithm := 'hmac';
  crypt.SetMacKeyString(apiSecret);

  sig := crypt.MacStringENC(sbSignature.GetAsString());

  http.SetRequestHeader('bfx-apikey',apiKey);
  http.SetRequestHeader('bfx-signature',sig);
  http.SetRequestHeader('bfx-nonce',nonce);

  resp := THttpResponse.Create;
  success := http.HttpStr('POST','https://api.bitfinex.com/v2/auth/w/order/submit',body,'utf-8','application/json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  WriteLn('Response body:');
  WriteLn(resp.BodyStr);


  http.Free;
  crypt.Free;
  dt.Free;
  sbNonce.Free;
  json.Free;
  sbSignature.Free;
  resp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.