| 1 | # REST API & FreqUI |
| 2 | |
| 3 | ## FreqUI |
| 4 | |
| 5 | Freqtrade provides a builtin webserver, which can serve [FreqUI](https://github.com/freqtrade/frequi), the freqtrade UI. |
| 6 | |
| 7 | By default, the UI is not included in the installation (except for docker images), and must be installed explicitly with `freqtrade install-ui`. |
| 8 | This same command can also be used to update freqUI, should there be a new release. |
| 9 | |
| 10 | Once the bot is started in trade / dry-run mode (with `freqtrade trade`) - the UI will be available under the configured port below (usually `http://127.0.0.1:8080`). |
| 11 | |
| 12 | !!! info "Alpha release" |
| 13 | FreqUI is still considered an alpha release - if you encounter bugs or inconsistencies please open a [FreqUI issue](https://github.com/freqtrade/frequi/issues/new/choose). |
| 14 | |
| 15 | !!! Note "developers" |
| 16 | Developers should not use this method, but instead use the method described in the [freqUI repository](https://github.com/freqtrade/frequi) to get the source-code of freqUI. |
| 17 | |
| 18 | ## Configuration |
| 19 | |
| 20 | Enable the rest API by adding the api_server section to your configuration and setting `api_server.enabled` to `true`. |
| 21 | |
| 22 | Sample configuration: |
| 23 | |
| 24 | ``` json |
| 25 | "api_server": { |
| 26 | "enabled": true, |
| 27 | "listen_ip_address": "127.0.0.1", |
| 28 | "listen_port": 8080, |
| 29 | "verbosity": "error", |
| 30 | "enable_openapi": false, |
| 31 | "jwt_secret_key": "somethingrandom", |
| 32 | "CORS_origins": [], |
| 33 | "username": "Freqtrader", |
| 34 | "password": "SuperSecret1!" |
| 35 | }, |
| 36 | ``` |
| 37 | |
| 38 | !!! Danger "Security warning" |
| 39 | By default, the configuration listens on localhost only (so it's not reachable from other systems). We strongly recommend to not expose this API to the internet and choose a strong, unique password, since others will potentially be able to control your bot. |
| 40 | |
| 41 | You can then access the API by going to `http://127.0.0.1:8080/api/v1/ping` in a browser to check if the API is running correctly. |
| 42 | This should return the response: |
| 43 | |
| 44 | ``` output |
| 45 | {"status":"pong"} |
| 46 | ``` |
| 47 | |
| 48 | All other endpoints return sensitive info and require authentication and are therefore not available through a web browser. |
| 49 | |
| 50 | ### Security |
| 51 | |
| 52 | To generate a secure password, best use a password manager, or use the below code. |
| 53 | |
| 54 | ``` python |
| 55 | import secrets |
| 56 | secrets.token_hex() |
| 57 | ``` |
| 58 | |
| 59 | !!! Hint "JWT token" |
| 60 | Use the same method to also generate a JWT secret key (`jwt_secret_key`). |
| 61 | |
| 62 | !!! Danger "Password selection" |
| 63 | Please make sure to select a very strong, unique password to protect your bot from unauthorized access. |
| 64 | Also change `jwt_secret_key` to something random (no need to remember this, but it'll be used to encrypt your session, so it better be something unique!). |
| 65 | |
| 66 | ### Configuration with docker |
| 67 | |
| 68 | If you run your bot using docker, you'll need to have the bot listen to incoming connections. The security is then handled by docker. |
| 69 | |
| 70 | ``` json |
| 71 | "api_server": { |
| 72 | "enabled": true, |
| 73 | "listen_ip_address": "0.0.0.0", |
| 74 | "listen_port": 8080 |
| 75 | }, |
| 76 | ``` |
| 77 | |
| 78 | Uncomment the following from your docker-compose file: |
| 79 | |
| 80 | ```yml |
| 81 | ports: |
| 82 | - "127.0.0.1:8080:8080" |
| 83 | ``` |
| 84 | |
| 85 | !!! Danger "Security warning" |
| 86 | By using `8080:8080` in the docker port mapping, the API will be available to everyone connecting to the server under the correct port, so others may be able to control your bot. |
| 87 | |
| 88 | |
| 89 | ## Rest API |
| 90 | |
| 91 | ### Consuming the API |
| 92 | |
| 93 | You can consume the API by using the script `scripts/rest_client.py`. |
| 94 | The client script only requires the `requests` module, so Freqtrade does not need to be installed on the system. |
| 95 | |
| 96 | ``` bash |
| 97 | python3 scripts/rest_client.py <command> [optional parameters] |
| 98 | ``` |
| 99 | |
| 100 | By default, the script assumes `127.0.0.1` (localhost) and port `8080` to be used, however you can specify a configuration file to override this behaviour. |
| 101 | |
| 102 | #### Minimalistic client config |
| 103 | |
| 104 | ``` json |
| 105 | { |
| 106 | "api_server": { |
| 107 | "enabled": true, |
| 108 | "listen_ip_address": "0.0.0.0", |
| 109 | "listen_port": 8080 |
| 110 | } |
| 111 | } |
| 112 | ``` |
| 113 | |
| 114 | ``` bash |
| 115 | python3 scripts/rest_client.py --config rest_config.json <command> [optional parameters] |
| 116 | ``` |
| 117 | |
| 118 | ### Available endpoints |
| 119 | |
| 120 | | Command | Description | |
| 121 | |----------|-------------| |
| 122 | | `ping` | Simple command testing the API Readiness - requires no authentication. |
| 123 | | `start` | Starts the trader. |
| 124 | | `stop` | Stops the trader. |
| 125 | | `stopbuy` | Stops the trader from opening new trades. Gracefully closes open trades according to their rules. |
| 126 | | `reload_config` | Reloads the configuration file. |
| 127 | | `trades` | List last trades. |
| 128 | | `trade/<tradeid>` | Get specific trade. |
| 129 | | `delete_trade <trade_id>` | Remove trade from the database. Tries to close open orders. Requires manual handling of this trade on the exchange. |
| 130 | | `show_config` | Shows part of the current configuration with relevant settings to operation. |
| 131 | | `logs` | Shows last log messages. |
| 132 | | `status` | Lists all open trades. |
| 133 | | `count` | Displays number of trades used and available. |
| 134 | | `locks` | Displays currently locked pairs. |
| 135 | | `delete_lock <lock_id>` | Deletes (disables) the lock by id. |
| 136 | | `profit` | Display a summary of your profit/loss from close trades and some stats about your performance. |
| 137 | | `forcesell <trade_id>` | Instantly sells the given trade (Ignoring `minimum_roi`). |
| 138 | | `forcesell all` | Instantly sells all open trades (Ignoring `minimum_roi`). |
| 139 | | `forcebuy <pair> [rate]` | Instantly buys the given pair. Rate is optional. (`forcebuy_enable` must be set to True) |
| 140 | | `performance` | Show performance of each finished trade grouped by pair. |
| 141 | | `balance` | Show account balance per currency. |
| 142 | | `daily <n>` | Shows profit or loss per day, over the last n days (n defaults to 7). |
| 143 | | `stats` | Display a summary of profit / loss reasons as well as average holding times. |
| 144 | | `whitelist` | Show the current whitelist. |
| 145 | | `blacklist [pair]` | Show the current blacklist, or adds a pair to the blacklist. |
| 146 | | `edge` | Show validated pairs by Edge if it is enabled. |
| 147 | | `pair_candles` | Returns dataframe for a pair / timeframe combination while the bot is running. **Alpha** |
| 148 | | `pair_history` | Returns an analyzed dataframe for a given timerange, analyzed by a given strategy. **Alpha** |
| 149 | | `plot_config` | Get plot config from the strategy (or nothing if not configured). **Alpha** |
| 150 | | `strategies` | List strategies in strategy directory. **Alpha** |
| 151 | | `strategy <strategy>` | Get specific Strategy content. **Alpha** |
| 152 | | `available_pairs` | List available backtest data. **Alpha** |
| 153 | | `version` | Show version. |
| 154 | |
| 155 | !!! Warning "Alpha status" |
| 156 | Endpoints labeled with *Alpha status* above may change at any time without notice. |
| 157 | |
| 158 | Possible commands can be listed from the rest-client script using the `help` command. |
| 159 | |
| 160 | ``` bash |
| 161 | python3 scripts/rest_client.py help |
| 162 | ``` |
| 163 | |
| 164 | ``` output |
| 165 | Possible commands: |
| 166 | |
| 167 | available_pairs |
| 168 | Return available pair (backtest data) based on timeframe / stake_currency selection |
| 169 | |
| 170 | :param timeframe: Only pairs with this timeframe available. |
| 171 | :param stake_currency: Only pairs that include this timeframe |
| 172 | |
| 173 | balance |
| 174 | Get the account balance. |
| 175 | |
| 176 | blacklist |
| 177 | Show the current blacklist. |
| 178 | |
| 179 | :param add: List of coins to add (example: "BNB/BTC") |
| 180 | |
| 181 | count |
| 182 | Return the amount of open trades. |
| 183 | |
| 184 | daily |
| 185 | Return the profits for each day, and amount of trades. |
| 186 | |
| 187 | delete_lock |
| 188 | Delete (disable) lock from the database. |
| 189 | |
| 190 | :param lock_id: ID for the lock to delete |
| 191 | |
| 192 | delete_trade |
| 193 | Delete trade from the database. |
| 194 | Tries to close open orders. Requires manual handling of this asset on the exchange. |
| 195 | |
| 196 | :param trade_id: Deletes the trade with this ID from the database. |
| 197 | |
| 198 | edge |
| 199 | Return information about edge. |
| 200 | |
| 201 | forcebuy |
| 202 | Buy an asset. |
| 203 | |
| 204 | :param pair: Pair to buy (ETH/BTC) |
| 205 | :param price: Optional - price to buy |
| 206 | |
| 207 | forcesell |
| 208 | Force-sell a trade. |
| 209 | |
| 210 | :param tradeid: Id of the trade (can be received via status command) |
| 211 | |
| 212 | locks |
| 213 | Return current locks |
| 214 | |
| 215 | logs |
| 216 | Show latest logs. |
| 217 | |
| 218 | :param limit: Limits log messages to the last <limit> logs. No limit to get the entire log. |
| 219 | |
| 220 | pair_candles |
| 221 | Return live dataframe for <pair><timeframe>. |
| 222 | |
| 223 | :param pair: Pair to get data for |
| 224 | :param timeframe: Only pairs with this timeframe available. |
| 225 | :param limit: Limit result to the last n candles. |
| 226 | |
| 227 | pair_history |
| 228 | Return historic, analyzed dataframe |
| 229 | |
| 230 | :param pair: Pair to get data for |
| 231 | :param timeframe: Only pairs with this timeframe available. |
| 232 | :param strategy: Strategy to analyze and get values for |
| 233 | :param timerange: Timerange to get data for (same format than --timerange endpoints) |
| 234 | |
| 235 | performance |
| 236 | Return the performance of the different coins. |
| 237 | |
| 238 | ping |
| 239 | simple ping |
| 240 | |
| 241 | plot_config |
| 242 | Return plot configuration if the strategy defines one. |
| 243 | |
| 244 | profit |
| 245 | Return the profit summary. |
| 246 | |
| 247 | reload_config |
| 248 | Reload configuration. |
| 249 | |
| 250 | show_config |
| 251 | |
| 252 | Returns part of the configuration, relevant for trading operations. |
| 253 | |
| 254 | start |
| 255 | Start the bot if it's in the stopped state. |
| 256 | |
| 257 | stats |
| 258 | Return the stats report (durations, sell-reasons). |
| 259 | |
| 260 | status |
| 261 | Get the status of open trades. |
| 262 | |
| 263 | stop |
| 264 | Stop the bot. Use `start` to restart. |
| 265 | |
| 266 | stopbuy |
| 267 | Stop buying (but handle sells gracefully). Use `reload_config` to reset. |
| 268 | |
| 269 | strategies |
| 270 | Lists available strategies |
| 271 | |
| 272 | strategy |
| 273 | Get strategy details |
| 274 | |
| 275 | :param strategy: Strategy class name |
| 276 | |
| 277 | trade |
| 278 | Return specific trade |
| 279 | |
| 280 | :param trade_id: Specify which trade to get. |
| 281 | |
| 282 | trades |
| 283 | Return trades history. |
| 284 | |
| 285 | :param limit: Limits trades to the X last trades. No limit to get all the trades. |
| 286 | |
| 287 | version |
| 288 | Return the version of the bot. |
| 289 | |
| 290 | whitelist |
| 291 | Show the current whitelist. |
| 292 | |
| 293 | ``` |
| 294 | |
| 295 | ### OpenAPI interface |
| 296 | |
| 297 | To enable the builtin openAPI interface (Swagger UI), specify `"enable_openapi": true` in the api_server configuration. |
| 298 | This will enable the Swagger UI at the `/docs` endpoint. By default, that's running at http://localhost:8080/docs/ - but it'll depend on your settings. |
| 299 | |
| 300 | ### Advanced API usage using JWT tokens |
| 301 | |
| 302 | !!! Note |
| 303 | The below should be done in an application (a Freqtrade REST API client, which fetches info via API), and is not intended to be used on a regular basis. |
| 304 | |
| 305 | Freqtrade's REST API also offers JWT (JSON Web Tokens). |
| 306 | You can login using the following command, and subsequently use the resulting access_token. |
| 307 | |
| 308 | ``` bash |
| 309 | > curl -X POST --user Freqtrader http://localhost:8080/api/v1/token/login |
| 310 | {"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1ODkxMTk2ODEsIm5iZiI6MTU4OTExOTY4MSwianRpIjoiMmEwYmY0NWUtMjhmOS00YTUzLTlmNzItMmM5ZWVlYThkNzc2IiwiZXhwIjoxNTg5MTIwNTgxLCJpZGVudGl0eSI6eyJ1IjoiRnJlcXRyYWRlciJ9LCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MifQ.qt6MAXYIa-l556OM7arBvYJ0SDI9J8bIk3_glDujF5g","refresh_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1ODkxMTk2ODEsIm5iZiI6MTU4OTExOTY4MSwianRpIjoiZWQ1ZWI3YjAtYjMwMy00YzAyLTg2N2MtNWViMjIxNWQ2YTMxIiwiZXhwIjoxNTkxNzExNjgxLCJpZGVudGl0eSI6eyJ1IjoiRnJlcXRyYWRlciJ9LCJ0eXBlIjoicmVmcmVzaCJ9.d1AT_jYICyTAjD0fiQAr52rkRqtxCjUGEMwlNuuzgNQ"} |
| 311 | |
| 312 | > access_token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1ODkxMTk2ODEsIm5iZiI6MTU4OTExOTY4MSwianRpIjoiMmEwYmY0NWUtMjhmOS00YTUzLTlmNzItMmM5ZWVlYThkNzc2IiwiZXhwIjoxNTg5MTIwNTgxLCJpZGVudGl0eSI6eyJ1IjoiRnJlcXRyYWRlciJ9LCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MifQ.qt6MAXYIa-l556OM7arBvYJ0SDI9J8bIk3_glDujF5g" |
| 313 | # Use access_token for authentication |
| 314 | > curl -X GET --header "Authorization: Bearer ${access_token}" http://localhost:8080/api/v1/count |
| 315 | |
| 316 | ``` |
| 317 | |
| 318 | Since the access token has a short timeout (15 min) - the `token/refresh` request should be used periodically to get a fresh access token: |
| 319 | |
| 320 | ``` bash |
| 321 | > curl -X POST --header "Authorization: Bearer ${refresh_token}"http://localhost:8080/api/v1/token/refresh |
| 322 | {"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1ODkxMTk5NzQsIm5iZiI6MTU4OTExOTk3NCwianRpIjoiMDBjNTlhMWUtMjBmYS00ZTk0LTliZjAtNWQwNTg2MTdiZDIyIiwiZXhwIjoxNTg5MTIwODc0LCJpZGVudGl0eSI6eyJ1IjoiRnJlcXRyYWRlciJ9LCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MifQ.1seHlII3WprjjclY6DpRhen0rqdF4j6jbvxIhUFaSbs"} |
| 323 | ``` |
| 324 | |
| 325 | ### CORS |
| 326 | |
| 327 | All web-based front-ends are subject to [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) - Cross-Origin Resource Sharing. |
| 328 | Since most of the requests to the Freqtrade API must be authenticated, a proper CORS policy is key to avoid security problems. |
| 329 | Also, the standard disallows `*` CORS policies for requests with credentials, so this setting must be set appropriately. |
| 330 | |
| 331 | Users can configure this themselves via the `CORS_origins` configuration setting. |
| 332 | It consists of a list of allowed sites that are allowed to consume resources from the bot's API. |
| 333 | |
| 334 | Assuming your application is deployed as `https://frequi.freqtrade.io/home/` - this would mean that the following configuration becomes necessary: |
| 335 | |
| 336 | ```jsonc |
| 337 | { |
| 338 | //... |
| 339 | "jwt_secret_key": "somethingrandom", |
| 340 | "CORS_origins": ["https://frequi.freqtrade.io"], |
| 341 | //... |
| 342 | } |
| 343 | ``` |
| 344 | |
| 345 | !!! Note |
| 346 | We strongly recommend to also set `jwt_secret_key` to something random and known only to yourself to avoid unauthorized access to your bot. |