0
votes

I'm using NestJs as backend to send and get requests from Binance api, but I'm getting a { code: -1022, msg: 'Signature for this request is not valid.' } when I want to post a new order (buy/sell).

This is how the body of postman looks like:

{ 
    "symbol": "BTCUSDT",
    "side": "BUY", 
    "type": "LIMIT", 
    "timeInForce": "GTC", 
    "quantity": "1", 
    "price": "59806.23000000"
}

This is my method for creating the signed key:

async testTrading(trade: ITradeReqOrderDto)
    {
        const serverTime = Date.now();
        const extraQuery = "symbol="+trade.symbol+"&side="+trade.side+
                            "&type="+trade.type+"&timeInForce="+trade.timeInForce+"&quantity="+
                            trade.quantity+"&price="+trade.price+"&recvWindow=5000";

        const signature = await this.createSignature(serverTime,extraQuery);
        const response = await this.wsService.orderTest(trade, serverTime, this.apiKey, signature);
    }
async createSignature(serverTime: number, extraQuery: string): Promise<string>
    {
        if (extraQuery != null)
        {
            return await this.cypherService.createSignature(this.secretKey, extraQuery+"&timestamp="+serverTime);
        }
        return await this.cypherService.createSignature(this.secretKey, "timestamp="+serverTime);
    }

Here is where the signed key is created:

async createSignature(secret: string, queryString: string)
    {
        return await crypto.createHmac('sha256', secret).update(queryString).digest('hex');
    }

This is how I'm sending the data to test it:

async orderTest(trade: ITradeReqOrderDto, timestamp: number, apiKey: string, signature: string): Promise<ITradeOrderResDto>
    {
        try
        {
            trade.timestamp = timestamp;
            trade.signature = signature;
            const response = await this.httpService.post("https://api.binance.com/api/v3/order/test", null,
            { 
                params: trade,
                headers: { "X-MBX-APIKEY": apiKey, "Content-Type": "application/json"}
            }).toPromise();

        } catch(error) {
            console.log(error);
            return { errors: [error]};
        }
    }

This is the path that is sended to the Binance API /api/v3/order/test:

path: '/api/v3/order/test?symbol=BTCUSDT&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=59806.23000000&timestamp=1618610661033&signature=6f739d53c9cc24b9c489e1d8b4b2577b05b1530806ded7ba8932339fd87243a
1

1 Answers

0
votes

NEVERMIND. I just realize that I had an extra parameter which was recvWindow=5000";. Stuck with this for 2 hours but finally got it.