Running the Nefertiti trading bot as a local web server

Stefan
3 min readMay 14, 2021

--

Nefertiti is a CLI (aka command-line) app that usually runs in your terminal/console and does not really have an UI other than the command-line commands and arguments.

Nefertiti does include a simple web server, allowing other apps (for example: an Electron or Tauri app) to talk to it.

Before you can send requests to the Nefertiti web server and receive responses, you’ll need to start the so-called hub.

The hub is the thing that starts new trading bots, configures them, and terminates them. You as a front-end entity will mostly talk to the hub.

Starting the hub

Starting the hub is easy. You start the Nefertiti CLI with a command named listen and an optional arg named --port=X

./nefertiti listen

If you do not include --port=X then the hub will listen to requests coming in on port 38700 (or the next available port)

Ping the hub

Now that the hub is running, you can ping it. The hub will respond with a list of running trading bots

GET http://127.0.0.1:38700/ping

Since we haven’t told the hub to start any trading bots, it will probably respond with this:

[]

Stop the hub

If you want to stop the hub, you can send it a DELETE request:

DELETE http://127.0.0.1:38700

Please note that you will not receive a response back. This is the designed behavior and not a bug.

Alternatively, you can terminate the hub in your terminal/console via Ctrl+C

Starting a new trading bot

Now that the hub is up-and-running, we can instruct it to start new a trading bot. Let’s start with a new sell bot.

POST http://127.0.0.1:38700/post

You must include the following x-www-form-urlencoded key/value pairs:

+----------------+------+
| command | sell |
| exchange | GDAX |
| api-key | X |
| api-secret | Y |
| api-passphrase | Z |
+----------------+------+

In the above example, command is a mandatory key that you MUST include every time you POST something to the hub. All the other keys are command-dependent. You can look up all of the available (mandatory and optional) keys in the Nefertiti CLI.

Now that we have started a new bot, we can ping it for information. First, let’s ping the hub:

GET http://127.0.0.1:38700/ping

Assuming a new sell bot got started and there weren’t any errors, the hub should respond with this:

[
{
"port":38701,
"command":"sell",
"args":
[
"listen",
"exchange=GDAX",
"strategy=0",
"mult=1.05",
"notify=2"
]
}
]

The above response tells us a trading bot is listening on port 38701. We can ping the trading bot listening on port on port 38701…

GET http://127.0.0.1:38701/ping

…and it should give us more or less the same response:

{
"port":38701,
"command":"sell",
"args":
[
"listen",
"exchange=GDAX",
"strategy=0",
"mult=1.05",
"notify=2"
]
}

Update a running trading bot

What if we’ll want to update a trading bot while the thing is running? We can do that via another POST request. But this time around, we won’t be sending a POST to the hub. We’re sending a POST request to the trading bot listing on the other port:

POST http://127.0.0.1:38701/post

In this example, we’ll include the following x-www-form-urlencoded key/value pair:

+------+------+
| mult | 1.06 |
+------+------+

Boom. You have updated a trading bot while it was running.

Terminate a running bot

Assuming you have a trading bot listening on port 38701, terminating it is easy:

DELETE http://127.0.0.1:38701

Please note that you will not receive a response back. This is the designed behavior and not a bug.

--

--

Stefan

Delphi/Rust/Go developer. Ethereum consultant. Embarcadero MVP. Ex-Adobe, Macromedia. Helped build 1Password.