Skip to content

REST API

With an [api] section, etemenanki-app serves a small JSON API over HTTP. It is meant for a program that manages a client, such as a tray application or a script: it switches which node a route group uses, and closes the connections still on the old node when you ask; it reloads on request, lists live connections and closes them, streams connection events and traffic rates, and tests a node’s delay through that node. It serves no web page.

A route switch edits the configuration file and then reloads it. The file stays the only place your picks are kept, so a restart, or a hand edit later, sees what the API wrote.

Without [api], nothing listens beyond the inbounds.

A SOCKS5 proxy with two outbounds and the API on a Unix socket:

config.toml
# A local SOCKS5 proxy with the REST API on a Unix socket, so a client
# program can switch the default route between two servers without editing
# this file by hand. The API rewrites [route] default and reloads.
[[inbound]]
tag = "socks-in"
protocol = "socks"
listen = "127.0.0.1"
port = 1080
[[outbound]]
tag = "home"
protocol = "trojan"
server = "proxy1.example.com"
port = 443
[outbound.stream]
network = "tls"
[outbound.settings]
password = "replace-with-a-long-random-password"
[[outbound]]
tag = "travel"
protocol = "trojan"
server = "proxy2.example.com"
port = 443
[outbound.stream]
network = "tls"
[outbound.settings]
password = "replace-with-another-long-random-password"
[route]
default = "home"
[api]
# A Unix socket needs no secret: only processes that can open the socket
# file reach it. The directory must exist.
listen = "/run/etemenanki/api.sock"

Read the routing, then send the default route through travel:

Terminal window
curl --unix-socket /run/etemenanki/api.sock http://localhost/v1/routes
Terminal window
curl --unix-socket /run/etemenanki/api.sock -X PUT \
-H 'Content-Type: application/json' -d '{"target": "travel"}' \
http://localhost/v1/routes/default

The switch rewrites default = "home" as default = "travel" in [route], reloads, and answers with what the reload did. Over TCP, drop --unix-socket, use the address from listen, and add -H 'Authorization: Bearer <secret>' when a secret is set.

KeyTypeRequiredDefaultDescription
listenstringno"127.0.0.1:9090"Where the API listens: ip:port (127.0.0.1:9090, [::1]:9090), or a path for a Unix socket. Any value that contains a / is a path, so write ./api.sock rather than api.sock; the directory must exist. Windows refuses a Unix socket when the API starts (unix socket <path>: not served on this platform). A host name is refused ([api] listen "…": expected ip:port, or a path (containing a /) for a unix socket).
secretstringdepends—The bearer token every request must carry, as Authorization: Bearer <secret>. Required unless listen is a loopback address or a Unix socket ([api] listen …: an API reachable from other hosts needs a secret; listen on a loopback address or a unix socket, or set one). An empty value is refused ([api] secret is empty). Use a long random value.

[api] is checked like the rest of the configuration: --test, a start and every reload refuse a bad one. --test binds nothing, so it does not find an address that is already in use.

  • With a secret, every request needs Authorization: Bearer <secret>, requests to unknown paths included. Anything else is answered 401 with WWW-Authenticate: Bearer and {"error": "a valid bearer token is required"}.
  • Without a secret, the API must listen on a loopback address or a Unix socket. A loopback API is open to every process on the host. A Unix socket is open to every process that can open the socket file, so put it in a directory only the right users can enter.
  • There is no TLS. The API speaks plain HTTP/1.1. To use it from another machine, keep it on loopback or a Unix socket and reach it through an SSH tunnel, rather than listening on a public address.
  • It starts with the proxy and logs API listening on <address>. An API that cannot bind at start stops the start: failed to start: API on <listen>: <error>.
  • A reload that adds, changes or removes [api] starts, rebinds or stops the API once the reload is applied, after the request that asked for the reload, if any, has been answered. A changed secret rebinds the same address.
  • A new [api] that cannot bind is logged as API on <listen>: <error>; it stays off until [api] changes. The proxy keeps running.
  • Removing the section logs API stopped: the config no longer has [api].
  • At shutdown the API stops first, before the proxy stops accepting connections.
Method and path Does Answers
GET /v1/routes Shows the route groups, their picks and every possible target 200 with the route view and an ETag header
PUT /v1/routes/{group} Picks a target for a group, or for default, and optionally closes the connections the switch moved 200 with the reload report and the new ETag
POST /v1/reload Reloads the files from disk 200 with the reload report
GET /v1/connections Lists live sessions and flows 200
DELETE /v1/connections/{flow} Kills one flow 204, or 404 when it is not live
DELETE /v1/connections?… Kills every flow that matches a selector 200 with {"closed": <n>}
GET /v1/connections/events Streams flow events 200, an event stream
GET /v1/traffic Streams traffic totals and rates 200, an event stream
GET /v1/outbounds/{tag}/delay Tests the delay of one outbound or balancer with HTTP requests through it 200 with the delay, 502 or 504 when the test fails
GET /v1/version The etemenanki-app version 200, {"version": "…"}
GET /v1/health Answers while the API serves 200, {"status": "ok"}

Errors that the API produces itself have a JSON body {"error": "<message>"}:

Status Meaning
400 The request is malformed: a body that is not {"target": "<string>"} with an optional boolean close, an If-Match the API does not take, an unknown selector or query field, or a delay test URL that is not an http:// or https:// URL with a host
401 The bearer token is missing or wrong
404 No route group, live flow, outbound or balancer has that name, id or tag
409 The configuration file changed since the ETag you sent; the response carries the current one
422 The change would not make a valid configuration, or the files on disk cannot be applied. The message is the one --test would print.
500 Anything else, such as a file that cannot be read or written
502, 504 A delay test that ran and failed (504 when it ran out of time); see Delay tests
503 A delay test while the proxy is stopping

GET /v1/routes describes the files as they are on disk. With the subscribe file example, it answers:

GET /v1/routes
{
"default": { "pick": "vless-ws", "target": "vless-ws" },
"groups": [
{ "name": "Ads", "pick": null, "inherit": "blackhole", "target": "blackhole" },
{ "name": "Streaming", "pick": "trojan-tls", "inherit": { "group": "Proxy" }, "target": "trojan-tls" },
{ "name": "Local", "pick": null, "inherit": "direct", "target": "direct" },
{ "name": "Proxy", "pick": null, "inherit": "default", "target": "vless-ws" }
],
"targets": [
{ "name": "vless-ws", "kind": "node" },
{ "name": "trojan-tls", "kind": "node" },
{ "name": "ss-2022", "kind": "node" },
{ "name": "hy2", "kind": "node" },
{ "name": "wg", "kind": "node" },
{ "name": "direct", "kind": "direct" },
{ "name": "blackhole", "kind": "blackhole" }
]
}
Field Meaning
pick What the configuration file says, or null when it picks nothing for this group
inherit What the group uses without a pick, written as in the subscribe file
target Where the traffic goes: the pick, or else what the group falls back to. null when the files leave the traffic nowhere to go, which a load would refuse.
targets Everything a pick may name, in order: the configuration’s own outbounds (outbound) and balancers (balancer), the subscribe file’s nodes (node), then direct and blackhole

Without [subscribe], groups is empty and default is the only thing to switch: a switch rewrites [route] default.

The ETag header is the SHA-256 of the configuration file’s bytes, in quotes. It changes whenever the file does.

PUT /v1/routes/{group} with a JSON body {"target": "<name>"}, or {"target": "<name>", "close": true}, and Content-Type: application/json. Percent-encode the group name in the path, as in /v1/routes/Video%20Streaming. The group default is the default route.

  1. Optional check. With If-Match set to the ETag of your last GET or PUT, a file that changed since is left alone and the answer is 409, with the current ETag. Send If-Match: *, or no header, to skip the check.

  2. Target. An unknown group is 404 (no route group named "<group>"). A target that the route view does not list is 422 (no node, outbound or balancer is named "<target>"; the route view lists them).

  3. Validation. The edited configuration is checked as --test would check it, with the subscribe file on disk. A configuration that would not start is 422, and the file is not touched.

  4. Write. The file is written to a temporary file in the same directory and renamed over the old one. Only the one value changes: comments, order and formatting are kept. With a subscribe file the value is in [subscribe.routes]; without one, it is [route] default. A pick that already names the target writes nothing.

  5. Reload. The proxy reloads, and the response is the reload report with the file’s new ETag. If the reload is refused, the response is its error: the file holds the new pick, and the running configuration is still the old one.

  6. Close, if asked. With "close": true, once the reload succeeded, the API closes every live connection that the new routing sends to another outbound, and the report gains "closed": <n>.

A switch between outbounds that exist already rebuilds only the route table. Without close, connections that are open keep the outbound they were opened on, and only new connections of the group use the new target. A browser keeps its connections open for a while, so pages that were already loading stay on the old node.

"close": true moves those too: their apps reconnect, and the new connections take the new target. Only the connections the switch moved are closed. A connection whose outbound is unchanged keeps running, and so does one whose outbound is a member of the balancer it would now go to.

Terminal window
curl --unix-socket /run/etemenanki/api.sock -X PUT \
-H 'Content-Type: application/json' -d '{"target": "travel", "close": true}' \
http://localhost/v1/routes/default

The close also runs when the reload finds nothing new, for example because the file watcher applied the written file first, so a switch to the target already picked still closes the connections that are not on it. It does not run when the switch or its reload is refused. With closed in it, the report lists its fields in alphabetical order; read them by name.

etemenanki-app needs write access to the configuration file and to its directory, where the temporary file is created. A service manager that makes the file system read-only must allow that directory, for example with systemd’s ReadWritePaths=.

POST /v1/reload reloads from disk, exactly as saving the file does, and answers with a report. A PUT answers with the same report. This is the one for the switch in the minimal example:

Reload report
{
"status": "applied",
"built": ["route"],
"swapped": [],
"drained": [],
"rebound": [],
"restarted": [],
"removed": [],
"reused": ["dns", "outbound home@v1", "outbound travel@v1", "inbound socks-in"]
}

{"status": "unchanged"} means there was nothing new to apply. The lists name what the reload built, what it kept, and what it changed in place; Hot reload explains them. A reload that is refused answers 422 or 500 with the reason, and the running configuration stays.

GET /v1/connections lists live sessions (client connections) and flows (the connections and UDP streams opened through them):

Field In Meaning
id both The session or flow id
inbound both The inbound tag
source both The client’s IP address, or null
user both The name the user is known by (its email, or its user name), or null for anonymous traffic
up, down both Bytes from and to the client. On a session this counts the protocol’s own bytes too; on a flow, only the payload.
started_at both Unix time in milliseconds
session flows The session that carries the flow, or null for a TUN flow
network flows "tcp" or "udp"
host, port flows The destination, as a domain or an IP address
sniffed flows The domain read from the traffic by sniffing, or null
outbound, outbound_version flows The outbound the flow was opened on, and its version. A balancer’s flow names the member.
rule flows The index of the route rule that matched, or null for the default route

To kill flows:

  • DELETE /v1/connections/{flow} kills one flow: 204, or 404 with no live flow <id>. Only that flow is killed; other flows of the same client connection keep running.
  • DELETE /v1/connections?outbound=…&inbound=…&session=…&user=… kills every flow that matches all the fields given, and answers {"closed": <n>}. With no field it kills every flow. An unknown field is 400.

Both streams are server-sent events. Read them with a client that does not buffer, such as curl -N. An idle stream carries a : comment line every 15 seconds.

  • GET /v1/connections/events sends opened and closed events, each carrying one flow as GET /v1/connections shows it. A client that falls more than 1024 events behind gets one lagged event, {"missed": <n>}, and continues from there.
  • GET /v1/traffic sends one traffic event a second: up and down (bytes since start), up_rate and down_rate (bytes per second over the last second) and flows (live flows), in total and per inbound and outbound tag.
GET /v1/traffic
event: traffic
data: {"tick":42,"interval_ms":1000,"up":1000,"down":1000,"up_rate":0,"down_rate":0,"flows":1,"inbounds":{"socks-in":{…}},"outbounds":{"home":{…}}}

GET /v1/outbounds/{tag}/delay tests one outbound or balancer by making HTTP requests through it, whatever the routing says, so you can show a delay for every node without switching to it. {tag} is the tag of an outbound or balancer of the running configuration; each node of a subscribe file is an outbound named after the node.

Query Default Meaning
url https://www.gstatic.com/generate_204 The page to request, http:// or https://. Percent-encode it when it has a query string of its own
timeout 5000 Milliseconds for the whole test, the connection to the node included. At most 30000; a larger value is cut to it

The test opens one connection through the node and sends two HEAD requests on it. The first pays for everything a new connection costs: the connection to the node, the proxy protocol’s handshake, and TLS. The second costs one round trip through the node to the site and back, which is what a page that is already connected waits for each request. That second time is the delay, as mihomo’s “unified delay” measures it.

Terminal window
curl --unix-socket /run/etemenanki/api.sock \
'http://localhost/v1/outbounds/travel/delay?timeout=3000'
GET /v1/outbounds/travel/delay (example values)
{"status": 204, "handshake_ms": 412, "delay_ms": 87, "unified": true}
Field Meaning
status The HTTP status the site answered. Any status counts: the request went through the node and back
handshake_ms The first request, from the start of the connection: what a new connection costs
delay_ms The second request’s round trip: the delay to show
unified false when the site closed the connection after the first answer; delay_ms is then the first request’s time

A test that fails answers 502, or 504 when it runs out of time, with {"error": "<message>", "kind": "<kind>"}:

kind Meaning
connect The outbound could not open the connection, for example because the node cannot be reached
tls TLS with the test site, not with the node, failed, for example because the site’s certificate does not verify
http No HTTP answer came back through the node, for example because the node closed the connection
timeout The test took longer than timeout

An unknown {tag} is 404, and a malformed url or an unknown query field is 400. Tests run in parallel and do not affect routing; their connections are not listed in /v1/connections and not counted in /v1/traffic.