Search code examples
pythonapirestauthenticationethereum

Issue with FTX US API POST Request


I have looked through the FTX API documentation found here: https://docs.ftx.us/#overview

And I've looked at the example code found in this repo: https://github.com/ftexchange/ftx/tree/master/rest

I am hitting POST Request on /wallet/withdrawals but I am getting

{"success":false,"error":"Not logged in: Invalid signature","errorCode":"not_logged_in"}

Here is my Code:

        resp = requests.get('https://otc.ftx.com/api/time')
        ftx_t = resp.json()
        time = ftx_t['result']
        date_format = datetime.datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%f%z")
        unix_time = datetime.datetime.timestamp(date_format)
        ts = unix_time * 1000
        # ts = int(time.time() * 1000)
        request = Request('**POST**', '**https://ftx.us/api/wallet/withdrawals**')  # To make withdrawas
        prepared1 = request.prepare()
        body = {
            "coin": "USDT",   # coin to withdraw
            "size": 0,  # amount to withdraw
            "address": "***************"  # address to send to
        }
        signature_payload1 = f'{ts}{prepared1.method}{prepared1.path_url}'.encode()
        signature1 = hmac.new('**SECRET KEY**'.encode(), signature_payload1, 'sha256').hexdigest()

        prepared1.headers[f'FTXUS-KEY'] = '**API KEY**'
        prepared1.headers[f'FTXUS-SIGN'] = signature1
        prepared1.headers[f'FTXUS-TS'] = str(ts)
        prepared1.headers['Content-Type'] = 'application/json'
        prepared1.headers['Accept'] = 'application/json'
        
        data1 = json.dumps(body).encode()


        res10 = requests.post('https://ftx.us/api/wallet/withdrawals', data=data1, headers=prepared1.headers)

Solution

  • You need to encode your data for sending post requests and add this encoded data in the body as well as when you are requesting a POST request.

    This might help:

    Before calling your signature1 variable you need to add the following:

    prepared1.body = json.dumps(body).encode()
    if prepared1.body:
       signature_payload1 += prepared1.body
    

    And in your POST request method, add the body in your Data Attribute:

    res10 = requests.post('https://ftx.us/api/wallet/withdrawals', data=prepared1.body, headers=prepared1.headers)
    

    Here I have edited the code for you:

     resp = requests.get('https://otc.ftx.com/api/time')
                ftx_t = resp.json()
                time = ftx_t['result']
                date_format = datetime.datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%f%z")
                unix_time = datetime.datetime.timestamp(date_format)
                ts = (unix_time * 1000)
                request = Request('POST', 'https://ftx.us/api/wallet/withdrawals')  # To make withdrawas
                prepared1 = request.prepare()
                signature_payload1 = f'{ts}{prepared1.method}{prepared1.path_url}'.encode()
                body = {
                    "coin": "USDT",  # coin to withdraw
                    "size": 0,  # amount to withdraw
                    "address": "***************"  # address to send to
                }
                prepared1.body = json.dumps(body).encode()
                if prepared1.body:
                    signature_payload1 += prepared1.body
                signature1 = hmac.new(SECRET KEY.encode(), signature_payload1,
                                      'sha256').hexdigest()
    
                prepared1.headers[f'FTXUS-KEY'] = API KEY
                prepared1.headers[f'FTXUS-SIGN'] = signature1
                prepared1.headers[f'FTXUS-TS'] = str(ts)
                prepared1.headers['Content-Type'] = 'application/json'
                prepared1.headers['Accept'] = 'application/json'
                res10 = requests.post('https://ftx.us/api/wallet/withdrawals', data=prepared1.body, headers=prepared1.headers)
    

    You can check these for reference:

    https://blog.ftx.com/blog/api-authentication/

    https://github.com/ftexchange/ftx/issues/10