Sample code for 30+ languages & platforms
Dart

Bitfinex v2 REST Submit Order

See more Bitfinex v2 REST Examples

Submit an order.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final http = CkHttp();

  final crypt = CkCrypt2();

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

  final dt = CkDateTime();
  dt.setFromCurrentSystemTime();

  final sbNonce = CkStringBuilder();
  sbNonce.append(dt.getAsUnixTimeStr(false));
  sbNonce.append('000');
  final nonce = sbNonce.getAsString();

  final json = CkJsonObject();
  json.updateString('type', 'LIMIT');
  json.updateString('symbol', 'tBTCUSD');
  json.updateString('price', '15');
  json.updateString('amount', '0.001');
  json.updateInt('flags', 0);
  final body = json.emit();

  final sbSignature = CkStringBuilder();
  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);

  final sig = crypt.macStringENC(sbSignature.getAsString());

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

  final resp = CkHttpResponse();
  try {
    http.httpStr('POST', 'https://api.bitfinex.com/v2/auth/w/order/submit', body, 'utf-8', 'application/json', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Response body:');
  print(resp.bodyStr);
}