Search code examples
binancebinance-api-client

Binance API Withdrawal Error: "Signature for this request is not valid."


I'm facing an issue with the Binance API

Whenever I try to make a request for withdrawal, it returns "Signature for this request is not valid." error message, even though if with the same function I make a request for the account info, Funding balance, getAll balances or whatever, it works.

I already tried several solutions, but nothing seemed to work. This are some of my tries reading similar issues

  • Works with endpoints like "api/v3/account" or "sapi/v1/capital/config/getall"
  • Sending the signature and the params in the body (it returns "You are not authorized to execute this request" error message, I think is because is not reading the body, only query params)
  • Activate all permissions for the API, and check the API
  • Send in the query the param "name"
  • Using Node packages like node-binance-api (It's not up to date with the request for withdrawals)
  • Changing scripts in librery node-binance-api to the actual api for withdrawals (same error as doing it myself)

I'm using NodeJs, axios to make the requests. This is my function:

    /**
     * Send Request to withdraw USDT funds from the selected wallet to a specified address
     * @param wallet 
     * @param amount 
     * @param address 
     * @returns 
     * @link https://binance-docs.github.io/apidocs/spot/en/#withdraw-user_data
     */
    makeWithdrawal(
        wallet: WalletType = 'SPOT',
        amount: number,
        address: string
    ) {
        const walletIndex = WalletEnum[wallet];
        return this.makeRequest<{ id: string }>(
            'sapi/v1/capital/withdraw/apply',
            'POST',
            {
                coin: 'USDT',
                amount,
                wallet: walletIndex,
                address,
                network: 'TRX',
                name: '_'
            }
        );
    }


    /**
     * Makes Request to Binance Pay API
     * @param endpoint 
     * @param method 
     * @param params 
     * @returns 
     * @link https://binance-docs.github.io/apidocs/spot/en
     */
    private makeRequest<T>(
        endpoint: string,
        method: Method = 'GET',
        params: {[key: string]: string | number} = {}
    ): Promise<T> {
        const timestamp = Number(new Date().getTime()).toFixed(0);

        let query = `timestamp=${timestamp}`;
        for (const key in params) {
            if (Object.prototype.hasOwnProperty.call(params, key)) {
                const value = params[key];
                query += `&${key}=${value}`
            }
        }
            
        const signature = this.sign(query);

        const headers: AxiosRequestHeaders = {
            'X-MBX-APIKEY': process.env.BINANCE_API_KEY,
        };

        const requestObservable = this.httpService.request<T>({
            method,
            url: `${process.env.BINANCE_API_URL}/${endpoint}`,
            params: {...params, timestamp, signature},
            headers
        }).pipe(
            map(res => res.data)
        );
        

        return lastValueFrom(requestObservable);
    }

    /**
     * Signs payload with private key
     * @param payload 
     * @returns 
     */
    private sign(query: string) {
        return createHmac('sha256', process.env.BINANCE_API_SECRET)
            .update(query)
            .digest('hex');
    }

I really can't understand what is going, if someone could please help me to solve this, as I'm reading this is a common issue, but I really tried solutions and no hope.

Thank you


Solution

  • For anyone having this problem, the issue was that I was sending the request inside the "params" instead of the url, weirdly enough, it didn't proccess it correctly, so make this small change:

    const requestObservable = this.httpService.request<T>({
                method,
                url: `${process.env.BINANCE_API_URL}/${endpoint}?${query}&signature=${signature}`,
                headers
            }).pipe(
                map(res => res.data)
            );
    

    And you are good to go