JS-SDK
Binance JS-SDK is a web development toolkit provided by Binance Mini-program Platform for web developers.
By using the Binance JS-SDK, web developers can directly use Binance-specific capabilities such as oauth, payment, etc. to provide a better web experience for Binance users.
Step1: Create a new app
We should create a new app in binance mini-program platform, the specific steps can be found in the section MPP Usage
Step2: Import JS-SDK file
Import the following JS-SDK file on the page where the JS API needs to be called: https://public.bnbstatic.com/static/js/binance-sdk/binance-sdk-v1.0.3.min.js
<script src="https://public.bnbstatic.com/static/js/binance-sdk/binance-sdk-v1.0.3.min.js"></script>
And it is recommended that developers put the js-sdk file on their own CDN so that they can provide users with a uniform and controlled speed experience.
Note: Support for using AMD/CMD
Step3: Permission Verification through bn.init
api
All pages that need to use JS-SDK must inject configuration information first, otherwise other APIs will not be called
Note: The same url only needs to be called once, for the SPA of changing url's web-app, we should call bn.init
api every time the url changes)
bn.init({
debug: true, // open debug mode, will log the input value/return value of all the api
clientId: '', // required,unique identifier for developers
ts: '', // required,timestamp of generating signature
nonce: '', // required, the random string of generating signature
signature: '',// required,signature
});
Parameters
Name | Type | Description |
---|---|---|
debug | boolean | open debug mode, will log the input value/return value of all the api |
clientId | string | unique identifier for developers |
ts | string | timestamp of generating signature |
nonce | string | the random string of generating signature |
signature | string | signature |
the specific signature algorithm can be found in the section Generate JS-SDK signature
Step4: Processing successful verification through bn.ready
api
bn.ready(function(){
// your code
});
Note: if bn.init
method executed successfully, bn.ready
will be called.
bn.init
is a client-side asynchronous operation, so if the relevant api needs to be called when the page is loaded, the api must be called in the ready function to ensure correct execution.
for apis that are called only when triggered by the user, they can be called directly and do not need to be placed in the ready function.
API call instructions
All apis are called via a bn
object with an object as argument, and all the api will return a Promise<{err: string; payload: object}>
, the payload is the return value of api, the err will be empty string if api call success and will be the specific failure reason if api call failed.
API List
getSystemInfo
Get the information of current Binance app, for exmaple language, theme, and versions. For more information, please refer to APIs of Mini Program Frameworks
bn.getSystemInfo().then((res) => {
res.payload
});
Response
Name | Type | Description |
---|---|---|
brand | string | Device brand |
model | string | Device model |
pixelRatio | number | Device's pixel ratio |
screenWidth | number | Screen width in px |
screenHeight | number | Screen height in px |
windowWidth | number | Available window width in px |
windowHeight | number | Available window height in px |
statusBarHeight | number | Status bar height in px |
language | string | Language set in Binance app |
version | string | Binance app version |
system | string | Operating system and version |
platform | string | Client platform |
safeArea | Object | Safe area when the screen is in vertical orientation |
theme | string | light or dark theme current Binance app used |
safeArea
is composed as follows
Name | Type | Description |
---|---|---|
left | number | The x-coordinate of the top-left corner of the safe area |
right | number | The x-coordinate of the bottom-right corner to of the safe area |
top | number | The y-coordinate of the top-left corner of the safe area |
bottom | number | The y-coordinate of the bottom-right corner to of the safe area |
width | number | Safe area width in logical pixels |
height | number | Safe area height in logical pixels |
getOauthCode
Use oauth way to get user's inforamtion. For more infomation, please refer to Binance Login (OAuth)
bn.getOuathCode({
state: 'xxxx', // The CSRF token to protect against CSRF (cross-site request forgery) attacks
}).then((res) => {
res.payload.code // oauth code
res.payload.state // is same to state of request payload
});
Parameters
Name | Type | Description |
---|---|---|
status | String | The CSRF token to protect against CSRF (cross-site request forgery) attacks, can be a random string |
Response
Name | Type | Description |
---|---|---|
code | String | The code can be used to exchange the access_token from the oauth service. |
state | String | The status in the request parameters, developers can validate it with the request one to avoid CSRF attacks |
requestPayment
Start a Binance Pay procedure. For more information, please refer to Binance Pay
bn.requestPayment({
prepayId: 'xxxx', // prepaid order id, the specific details can be found in payment platform
certSn: "",
merchantId: "",
timeStamp: "",
noncestr: "",
paySign: "", // signature of the parameters
}).then((res) => {
res.payload.status // '0': pay success, others: pay failed
});
Parameters
Name | Type | Description |
---|---|---|
certSn | String | API identity key issued by Binance payment system |
merchantId | String | The merchant account id, issued when merchant been created at Binance. |
timeStamp | String | timestamp of the sign |
noncestr | String | nonceStr for the sign |
prepayId | String | The value of the prepay_id parameter returned by the unified order placement API |
paySign | String | Signature. For specific signature schemes, see Binance Pay API Documentation |
Response
Name | Type | Description |
---|---|---|
status | String | '0' for paied successfully, and others for paied failed |
Note: The prepayId
and status
are both with type of string, please don't pass a number, otherwise this can cause accuracy problems and thus make the payment fail.