Bancor provides public API methods which take HTTP GET requests and return JSON.
Please note that making more than 6 calls per second to the public API, or repeatedly and needlessly fetching excessive amounts of data, can result in your IP being banned.
What is the “Price Discover” API?
The "Price Discover (Value)" API helps you easily calculate the expected tokens needed/received (based on the amount indication) when executing a convert on the Bancor Network.
URL format:
Requesting amount needed in the "To" token:
When using this format, the amount value you pass in the call indicates the amount of "ToCurrency" you wish to receive at the end of the transaction. As a result, the response will be the amount of "FromCurrency" needed.
https://api.bancor.network/0.1/currencies/{FromCurrencyID}/value?toCurrencyId={ToCurrencyID}&toAmount={Amount}
Requesting amount needed in the "From" token:
When using this format, the amount value you pass in the call indicates the amount of "FromTokens" you wish to convert and would like to know how many "ToCurrency" you will receive. As a result, the response will be the amount of "ToCurrency" expected to receive from the transaction.
https://api.bancor.network/0.1/currencies/{FromCurrencyID}/value?toCurrencyId={ToCurrencyID}&fromAmount={Amount}
UPDATE:
You can now use currency code in the URL for ease of use.
Format:
https://api.bancor.network/0.1/currencies/{FromCurrencyCode}/value?toCurrencyId={ToCurrencyCode}&toAmount={Amount}
Or
https://api.bancor.network/0.1/currencies/{FromCurrencyCode}/value?toCurrencyId={ToCurrencyCode}&fromAmount={Amount}
URL Values:
You will require to pass 3 values in order for the API to successfully work:
- {FromCurrencyID} - this value is a unique currency ID that will indicate the token you would like to convert from. This is the token you currently have and would like to convert.
- {ToCurrencyID} - this unique currency ID indicate the token you would like to convert to. This is the token you currently do not own and would like to receive at the end of the conversion process.
- {Amount} - this integer value represents the smallest unit (wei) of the amount you would like to convert and receive indication for from the API.
UPDATE - you can also use currency code in the API
- {FromCurrencyCode} - the currency code as it is presented on the bancor network. For example, the currency code for ethereum is ETH.
- {ToCurrencyCode} - the currency code as it is presented on the bancor network. For example, the currency code for ethereum is ETH.
Sample Call:
https://api.bancor.network/0.1/currencies/5937d635231e97001f744267/value?toCurrencyId=594bb7e468a95e00203b048d&toAmount=1000000000000000000
- {FromCurrencyID} - 5937d635231e97001f744267 (indicate ETH)
- {ToCurrencyID} - 594bb7e468a95e00203b048d (indicate BNT)
- {Amount} - 1000000000000000000 (indicate 1 ETH in Wei). This integer value represents the smallest unit (wei) of the token.
There are two options for the value indication:- toAmount={Amount} - when appending this to the API call, you will indicate the amount of tokens you wish to receive as a result of the transaction. The API will return the expected units to use in order to receive this return.
- fromAmount={Amount} - when appending this to the API call, you will indicate the amount of tokens you wish to convert. The API will return the expected units you would receive if executing the transaction.
Sample Response:
{"data":"5607837548991896"}
What is '5607837548991896'?
This value is the amount of BNT tokens you are expected to receive/use in the smallest possible unit of the token.
Note that if you use:
- FromAmount - the response will be in the smallest unit of the ToCurrency
- ToAmount - the response will be in the smallest unit of the FromCurrency
Note - execution amount may vary due to external changes. The calculation is dynamic and is relevant for the time of sampling. Since actual execution may be done with a delay, the result amount can change (up or down) given the data on the exact time of execution
Keep in mind:
Bancor API is currently in alpha and is likely to break or be slow to response.
Prices may vary during the process, and should be considered as estimation only.
We offer several APIs for our partners, for more information please visit our API Documentation