Table of contents
Introduction
Prerequisites
Testnet Key
Download and install Postman
Create an environment
Import a collection
Make a request
Summary
Understanding and using API interfaces for digital currency transactions can create more opportunities for buying and selling. Lots of possibilities. With some simple coding knowledge, you can use your trading platform's backend program to automate the execution of your trading strategies. By bypassing the website, matching engines can be found faster for high-performance applications.
This series of articles aims to introduce the REST API launched by Binance and demonstrate specific interaction methods. After learning, you will be able to query information about markets and positions with ease and place various types of orders.
In this article, we will use Postman to communicate with the trading platform. Don’t worry, we don’t put any real money at risk.
The demonstration process will be in the test Completed online, you can receive virtual funds without any actual value in advance, and their functions are exactly the same as real currencies and tokens. Once you become proficient in using this API, you can trade real money through it.
First, log in to the spot test network.
To gain access, you should log in with your GitHub account. If you are a new user, please create a GitHub account first.
Click Verify and log in through GitHub.
Under the API key directory, the system will notify you that the key has not been registered. Click Generate HMAC_SHA256 key to create a key pair.
In the next screen, add a label for the key. You can name it whatever you want and click Generate.
You will now see two keys: API Key and Encryption Key. Please record these two important items or you will need to create a new set of keys. We recommend storing it in your computer's Notepad program so it can be easily copied and pasted later.
Note: In a real transaction, the tag key is Key to managing different keys. Your account can have multiple keys with different permissions at the same time. If you run multiple trading bots, using keys with different descriptive labels makes it easier to manage permissions or delete the corresponding keys without having to adjust all trading bots.
Postman is an API collaboration platform. For us, it was the perfect starting point - without having to write any code, we could access the collection of Binance requests we needed to test through Postman.
This program is available for Mac, Windows and Linux systems. Please go to the download page to download the ".zip" file.
After downloading, find the file in your file browser and install it. Then launch the app and you're done. Please note that you can log into the program by creating an account, but creating an account is not a required step. If you'd like to skip this step, just select the option at the bottom of the window.
At this stage, the screen should appear similar to the image below.
First, we need to create the environment. This is just a way of adding a variable to the function you are about to use. To do this, you first need to scrape some information from the Binance GitHub repository. Please go here to download the ".zip" file,
The download process should be quick. Find the file in a file browser and unzip it. Then, you can return to Postman.
p>
Click on the gear icon in the upper right corner (as shown above). You will see the Manage Environment pop-up.
Select Import and go to the folder you just extracted ("binance -postman-api").
Go into the folder and select the environment folder.
You will see two files (one for mainnet and one for testnet). At this time, we select binance_com_spot_testnet_api.postman_environment.json. The correct password must be entered as two passwords cannot be mixed.
The operation is almost complete. Click Binance Spot Testnet API and the following variables will be displayed on the screen. Paste the previously saved key into the field outlined by the red line and edit the two parameters. Click Update and exit the popup.
In this screen The "Timestamp" and "Signature" fields are empty and specific values will be automatically created on each request.
Only one last step remains - to the right of the gear icon where you previously set up your environment, you will see a drop-down menu, currently DisplayNo environment. Click the menu and select Binance Spot Testnet API.
Now we import the collection - which contains various requests that perform heavy and complex tasks when we call them. To load it into our environment:
Click Import in the upper left corner strong>.
In the pop-up window, under the File tab, select Upload File.
Find the binance-postman-api folder again and open it.
At this point, enter the collection in the subdirectory.
Two files appear again on the screen: one of them is for the futures API, and we need to use the spot file, so please select binance_spot_api_v1.postman_collection.jsonFile.
A confirmation screen should now appear, showing that the imported collection is identified as Postman collection format. Select "Import".
Under the "Collections" tab on the left side of the window, you can find a storage for more than 100 The requested folder. Congratulations, the import was successful! In the next section we will detail the various requests that can be made.
If you expand the folder under the Collections tab, you can see many different requests. According to color coding, we can generally use three methods:
GET: The GET method is used to retrieve content from the server. We use it to query information such as account balances and asset prices.
POST: We usually use the POST method to create information in the server for operations such as placing orders and requesting withdrawals. help.
DELETE: The DELETE method requests the server to delete information and can play an important role when canceling an order.
Now we make our first request! We will obtain the assets that can be traded on the trading platform and the trading rules:
GET/exchangeInfo
This request does not require any other parameters, copy and paste them into the address bar to get the response. If it is a request containing multiple parameters, it can be easily viewed and modified through Postman.
To load this request, select Markets > Trading Platform Information. The following window will pop up:
We don't need to do anything else, so go ahead and click Send. Then you can get the response:
In the highlighted part at the top, some important information will be displayed:
Response status (200 means the operation is successful, 400-499 means there is a problem)
Response received Time (less than 1 second)
Response text size (approximately 22 KB).
The second box is the capacity of the response. We have organized it carefully for ease of reading and learning - it contains information about the trading platform, the currency pairs that can be traded and their minimum/maximum amounts.
This may seem like a lot of information, but this format is easy to process programmatically. When scripting interactions, you will be able to easily select specific attributes of specific elements from the response.
Here you can view the assets held and the corresponding amount:
GET/account
at
The timestamp tells the server when the request was sent. Considering that the network status is unstable or facing downtime, the server may receive the request much later than expected. If it takes too long, it will deny the request. The waiting time can be set through the recvWindow parameter, and the default value is 5000 milliseconds.
The values of these two fields are generated by Postman. Click "Send" to get the response. Under “Balance” you will see six assets – BNB, BTC, BUSD, ETH, LTC, and TRX. The balance will be divided into two parts: Free and Locked. We haven't locked any assets yet, all your assets are free to use.
Congratulations on your new asset! (But it does not actually exist)
We can get the current price of an asset in different ways. The simplest is probably via the following request:
GET /api/v3/ticker/24hr
As you might expect, with the above request, we can obtain asset price information for the past 24 hours. This request can be found under Market > 24 Hour Spread Indicator Statistics. The default trading pair for this asset variant is BTCUSDT.
You can directly send this request to view the price information details. You can also change the asset (to BNBBUSD, LTCUSDT, etc.), or cancel the selected variable and return 40 pairs of data.
There is also a simpler calling method: (Market> Asset Price Indicator), which can Return the current trading price of the asset:
GET /api/v3/price
As before, you can change or completely remove asset variables to get the latest prices for all assets.
Order Book Depth (also known as Depth of Market or DOM) contains a lot of market information. We can call the following request to obtain this valid information:
GET api/v3/depth
pre>When we send this request with the default value (Market > Order Book), it will tell us about the bid and demand for BTCUSDT. The amount of data generated by the testnet server is lower than actual, so here is a screenshot of what would appear in a real environment:
In the highlighted section above, you can see the first bid. We are looking at the BTCUSDT order book. The number above is the price a trader is willing to pay for Bitcoin, and the number below is the amount he is willing to buy. Therefore, this data indicates that this order requests to buy 0.999 BTC at a unit price of 9704.65 USDT. Continuing to look down, you can see that the prices are arranged from high to low, which means that the buyer's bid gradually decreases.
If you want your assets to be worth the money, the highest offer is naturally the most attractive. That is to say, if 3 BTC are sold in the market, only 0.999 BTC can be sold at the highest price. The remaining part needs to be sold at a subsequent lower quotation until the entire order is completed.
Continue scrolling down to see other requirements. They are similar to bids, except that they represent an order to sell BTC for USDT.
Publish test order
Now we publish test order.
POST api/v3/order/test
Even if only using funds from the testnet, the request will not actually issue an order. This request works great for testing orders before actually committing them. This request can be found in Trading > Test New Order (TRADE).
You can see more parameters. Let's browse the selected parameters:
symbol – has been introduced above and represents the currency pair that needs to be traded.
side – Specify BUY or SELL here. For the BTCUSDT trading pair, BUY means buying BTC with USDT, and SELL means selling USDT in exchange for BTC.
type – The type of order that needs to be submitted. Possible values (click here for details):
LIMIT
MARKET
STOP_LOSS
STOP_LOSS_LIMIT p>
TAKE_PROFIT
TAKE_PROFIT_LIMIT
LIMIT_MAKER
timeInForce– This parameter represents the expected Order Execution Method:
GTC (Good till canceled) – This is the most popular setting. GTC will ensure that the order is always valid until it is filled or canceled.
FOK (Full or Cancel) – FOK instructs the trading platform to execute the entire order immediately. If the operation cannot be performed, the order is immediately canceled.
IOC (Immediate Fill or Canceled) – The order will be executed immediately in whole or in part, otherwise it will be cancelled. Unlike FOK, if the order can be partially executed, it will not be canceled.
Quantity – refers to the quantity of assets that need to be purchased or sold.
price – Target selling price. In the BTCUSDT trading pair, the price is expressed in USDT.
newClientOrderId – Order ID. This is not a required field, but it can be set to an ID for later queries. Otherwise, it will be randomly generated by the trading platform.
Okay! Let’s create a test order now. We use an automatically generated value: a limit order to sell 0.1 BTC in exchange for USDT at a unit price of $9,000. Click Send. If the operation is successful, the response we will receive is {}.
Now we issue real virtual orders.
POST /api/v3/order
Go to Trading>New Order. Now that you are familiar with test orders, the parameters here will not be unfamiliar to you. We leave all values as is, but change the sale price to $40,000 since we have been long. Adjust the price value so that it is reflected in the system. Then click Send.
If the transaction is successful, your response will return a series of details about the order.
In the previous chapter, we have received confirmation that the order was successfully released, then if we What should I do if I want to check the order later? There are several requests available to us.
GET /api/v3/openOrders
Relevant requests can be found in Transactions> Current Open Orders (USER_DATA). BTCUSDT is the default option. If you click Send, you will see all open BTCUSDT orders (currently only the orders we have set up previously). You can not specify an asset and the system will respond to all open orders.
GET /api/v3/allOrders
Trading> All Orders (USER_DATA) provides an overview of all orders, not just open orders. Here you must specify an asset. orderId, startTime, endTime, and limit are all optional parameters that can help optimize your search, but we do not select them at this time, so please It remains unchecked. Click Send and the same response as before will appear. Closed or canceled orders can also be viewed here.
Finally, a specific order can be queried using the following request:
GET /api/v3/order
The request can be obtained under Transaction> Query Order (USER_DATA). You need to provide orderId or origClientOrderId (optional "newClientOrderId" tag added in the order). Uncheck orderId. For origClientOrderId, the previously added default label "my_order_id_1" must be provided. Fill in the field with this label and click Send to get a response.
Over time, we may decide that the $40,000 target is too optimistic and wish to cancel the order. In this case, you need to use:
DELETE /api/v3/order
Find this request under Transaction> Cancel Order and select the order you want to cancel. Uncheck orderId and newClientOrderId and set "my_order_id_1" to the value of origClientOrderId.
After sending this request, the order will be returned. If you scroll down to "Status" you can see that the order has indeed been cancelled. To confirm, use the GET /api/v3/openOrders endpoint again (an empty list will be displayed) or GET /api/ with the origClientOrderId v3/order.
Our previous order failed to be executed because it was a limit order and would only be executed when the BTC price reached Triggered when $40,000 is required. For market orders, it is basically equivalent to "buy/sell at the current trading price of the asset." The order will be filled immediately.
To do this, let's go back to Trading > New Order. We will demonstrate the response type (newOrderRespType), which is a parameter that can be adjusted depending on what kind of response we want the server to give. There are three options: ACK, RESULT, or FULL – you can see examples of each response here. We use ACK, which allows us to directly confirm received orders.
Below, you can see that we are about to submit a market order to sell BNB in exchange for BUSD at the current market price.
Please note that this response only provides us with the most concise information:
You can verify whether the order has been completed through the /api/v3/allOrders endpoint.
Finally, Let’s take a look at the endpoint for checking transactions:
GET /api/v3/myTrades
This request is under Transactions>Account Transaction List (USER_DATA). Through this endpoint, each transaction for a specific asset can be checked. To see all transactions for the default asset (BTCUSDT), simply uncheck startTime, endTime, and fromId. This response can return up to 500 transactions - to see more, just adjust the limit.
Postman can display the original HTTP request and response in detail.
This menu allows you to open the Postman console and print out detailed information for each request.
p>
This guide is intended to provide a brief introduction to how to use the Binance API without writing code. If you have read this article in detail, you should now have an idea of how to request and submit information.
Subsequent articles in this series will introduce some basic coding concepts. By mastering these concepts, we can automatically buy and sell digital currencies and other digital assets.
Do you have any other questions? Visit our growing Binance Developer Community forum, or check out the documentation.