Setting the signed keys to Binance API requests in NESTJS
When you interact with the Binance application subscription with NestJS, it is necessary to obtain signed keys to verify requests and prevent unauthorized access. In this article, we will give instructions on setting the signed keys to /API/V3/ORDER'- and ORDER/TEST" points.
Why are the signed keys necessary?
The Binance application interface requires a valid API key to perform certain functions. However, by default, the key is not signed, so it is prone to unauthorized access. To solve this problem, you need to create an application secret (also known as the X signature and public key) and sign it for signing your request.
Creating signed keys
If you want to get a valid signature, follow these steps:
- Create a Binance Developer Account : If you are not yet, register for the Binance Developer Account.
- Create Application Secrets : After logging in to the developer lineup, go to the API key tab and click "Create a new application for Secret". You can choose between JSON Web Tokens (JWT) or Server's Public Key Encryption (SSEK).
- Select an algorithm : Select the appropriate algorithm to create signed keys.
- Use the app secret in Nestjs : In your NestJS application, bring the@Nestjs/JWT
package and create it to create a authentication service.
Example Implementation
Here is an example of setting a signed key to/API/V3/ORDERand ORDER/ORDER/TESTERE Terminal points using JWT:
Typedript
Bring {injectable} ‘@Nestjs/Common’;
Import * JWT ‘JSONwebtokkokon’;
@Injectable ()
Export class Authservice {
Private Appsecret: string;
Builder () {
This.Appsecret = process.env.binance_app_secret;
}
Async Generateeten (User: User): Promise
Const payload = {user};
Restore jwt.sign (payload, this.appsecret, {aging: ‘1h’});
}
Async Vardifytoken (Token: String): Promise
Try {
Const decoded = jwt.verify (token, this.appsecret);
Return decoded. User for user;
} catch (error) {
return zero;
}
}
Async Createorder (User, Order: Charter Puts): Permission
Const token = Wait Authservice.GenateTetken (user);
Const payload = {user, order};
Const signature = jwt.sign (payload, this.appsecret, {aging: ‘1h’});
Return {… order, signature};
}
}
`
In the example above:
- We create a signed key using the
JSONWEBTOKOKE 'and store it as an environmental variable.
- When creating a new order, we will check the user ID with the signed key to ensure authenticity.
Infanting everything
Here's an updated version of the NESTJS application containing signed keys:
Typedript
Bring {module} ‘@Nestjs/Common’;
Import {AppController} from ‘./app.Controller’;
Bring {appservice} from ‘./app.service’;
Bring {Authservice} from ‘./Auth.service’;
@Module({
Controllers: [AppController],
Service Providers: [Authservice, Appservice],
})
Export class AppMODULE {}
`
`Typedript
Bring Express ‘Express’;
Bring the Authcontroller from ‘./Auth.Controller’;
Bring the Authservice from ‘./Auth.service’;
Const App = Express ();
App.use (Express.json ());
App.post (‘/API/V3/Order’, Async (Req: Request, Res: Reply) => {
Const order = req.body;
Try {
// Create a new order with signed key
Const token = Wait Authservice.GenateTeten ({user: ‘your_user_name’});
Const payload = {user: ‘your_user_name’, order};
Const signature = jwt.sign (payload, ‘your_app_secret_key’);
Return Res.status (201) .json ({
Message: ‘Order created successfully!’,
Information: {
…order,
signature,
},
});
} catch (error) {
Console.error (error);
Return Res.status (500).