Sample code for 30+ languages & platforms
SQL Server

Bitfinex v2 REST Submit Order

See more Bitfinex v2 REST Examples

Submit an order.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT

    DECLARE @apiPath nvarchar(4000)
    SELECT @apiPath = 'v2/auth/w/order/submit'
    DECLARE @apiKey nvarchar(4000)
    SELECT @apiKey = 'MY_API_KEY'
    DECLARE @apiSecret nvarchar(4000)
    SELECT @apiSecret = 'MY_API_SECRET'

    DECLARE @dt int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT

    EXEC sp_OAMethod @dt, 'SetFromCurrentSystemTime', @success OUT

    DECLARE @sbNonce int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbNonce OUT

    EXEC sp_OAMethod @dt, 'GetAsUnixTimeStr', @sTmp0 OUT, 0
    EXEC sp_OAMethod @sbNonce, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @sbNonce, 'Append', @success OUT, '000'
    DECLARE @nonce nvarchar(4000)
    EXEC sp_OAMethod @sbNonce, 'GetAsString', @nonce OUT

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'type', 'LIMIT'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'symbol', 'tBTCUSD'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'price', '15'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'amount', '0.001'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'flags', 0
    DECLARE @body nvarchar(4000)
    EXEC sp_OAMethod @json, 'Emit', @body OUT

    DECLARE @sbSignature int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbSignature OUT

    EXEC sp_OAMethod @sbSignature, 'Append', @success OUT, '/api/'
    EXEC sp_OAMethod @sbSignature, 'Append', @success OUT, @apiPath
    EXEC sp_OAMethod @sbSignature, 'Append', @success OUT, @nonce
    EXEC sp_OAMethod @sbSignature, 'Append', @success OUT, @body

    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex_lower'
    EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'sha384'
    EXEC sp_OASetProperty @crypt, 'MacAlgorithm', 'hmac'
    EXEC sp_OAMethod @crypt, 'SetMacKeyString', @success OUT, @apiSecret

    DECLARE @sig nvarchar(4000)
    EXEC sp_OAMethod @sbSignature, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @crypt, 'MacStringENC', @sig OUT, @sTmp0

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'bfx-apikey', @apiKey
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'bfx-signature', @sig
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'bfx-nonce', @nonce

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpStr', @success OUT, 'POST', 'https://api.bitfinex.com/v2/auth/w/order/submit', @body, 'utf-8', 'application/json', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @crypt
        EXEC @hr = sp_OADestroy @dt
        EXEC @hr = sp_OADestroy @sbNonce
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbSignature
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END


    PRINT 'Response body:'
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @dt
    EXEC @hr = sp_OADestroy @sbNonce
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbSignature
    EXEC @hr = sp_OADestroy @resp


END
GO