API guides
Streaming and WebSockets
Follow deploy logs and other long operations live with Server-Sent Events.
๐ก Server-Sent Events
Long operations โ deploys, certificates, Artisan commands, server preparation, fleet runs, logs โ stream their output as Server-Sent Events (text/event-stream). Each event is one data: line with a JSON object, followed by a blank line:
data: {"line":"[1/8] Cloning repository...\n"}
data: {"line":"[3/8] Installing Composer dependencies...\n"}
: ka
data: {"done":true,"status":"success"}
- Output events carry
line: a raw chunk of output. A chunk may hold several lines or part of a line โ append chunks as they arrive instead of treating each as a line. - The last event has
"done": true(usually with astatus), and then the server closes the connection. - Lines starting with
:are keep-alive comments (: ka, every 20 seconds); ignore them.
Authenticating a stream
Streams need the same Authorization: Bearer โฆ header as every other endpoint. The browser's EventSource cannot send headers, so read the stream with fetch (or any HTTP client that can read a response body as it arrives):
curl -N https://panel.example.com/api/sites/12/deploy/stream \
-H "Authorization: Bearer $ORCH_TOKEN"
const res = await fetch('https://panel.example.com/api/sites/12/deploy/stream', {
headers: { Authorization: `Bearer ${process.env.ORCH_TOKEN}` },
});
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
let buffer = '';
for (;;) {
const { value, done } = await reader.read();
if (done) break;
buffer += value;
const events = buffer.split('\n\n');
buffer = events.pop(); // keep an incomplete event for the next chunk
for (const event of events) {
if (!event.startsWith('data: ')) continue; // ": ka" keep-alives
const data = JSON.parse(event.slice(6));
if (data.line) process.stdout.write(data.line);
if (data.done) console.log(`\nFinished: ${data.status}`);
}
}
use Illuminate\Support\Facades\Http;
$body = Http::withToken(env('ORCH_TOKEN'))
->withOptions(['stream' => true])
->get('https://panel.example.com/api/sites/12/deploy/stream')
->toPsrResponse()
->getBody();
$buffer = '';
while (! $body->eof()) {
$buffer .= $body->read(1024);
while (($pos = strpos($buffer, "\n\n")) !== false) {
$event = substr($buffer, 0, $pos);
$buffer = substr($buffer, $pos + 2);
if (! str_starts_with($event, 'data: ')) {
continue;
}
$data = json_decode(substr($event, 6), true);
echo $data['line'] ?? '';
if (! empty($data['done'])) {
echo PHP_EOL."Finished: {$data['status']}".PHP_EOL;
}
}
}
import json
import os
import requests
with requests.get(
"https://panel.example.com/api/sites/12/deploy/stream",
headers={"Authorization": f"Bearer {os.environ['ORCH_TOKEN']}"},
stream=True,
timeout=(10, 120),
) as res:
res.raise_for_status()
for raw in res.iter_lines(decode_unicode=True):
if not raw or not raw.startswith("data: "):
continue # blank separators and ": ka" keep-alives
data = json.loads(raw[6:])
print(data.get("line", ""), end="")
if data.get("done"):
print(f"\nFinished: {data.get('status')}")
Note. Reverse proxies and CI runners sometimes buffer responses. The panel's nginx already disables buffering for /api/; if you put another proxy in front, turn buffering off for streams there too.
๐๏ธ The streams
| Stream | Output events | Final event |
|---|---|---|
GET /api/sites/{id}/deploy/stream โ deploy, redeploy or rollback |
{"line": "<chunk>"} |
{"done": true, "status": "success" | "failed"} |
GET /api/sites/{id}/provision/stream |
{"line": "<chunk>"} |
{"done": true, "status": "active" | "error"} |
GET /api/sites/{id}/ssl/stream โ issue, renew, add or remove hostnames |
{"line": "<chunk>"} |
{"done": true, "status": "success" | "failed"} |
GET /api/sites/{id}/artisan/stream |
{"line": "<one line>"} |
{"done": true, "status": "success" | "failed"} |
GET /api/servers/{id}/prepare/stream |
{"line": "<chunk>"} |
{"done": true, "status": โฆ} |
GET /api/fleet/runs/{id}/stream โ fleet actions, commands and scripts |
{"serverId": 3, "chunk": "โฆ"} and {"serverId": 3, "status": "running", "exitCode": null} |
{"done": true} |
GET /api/fleet/servers/{id}/logs/stream |
{"line": "<one line>"} |
{"done": true, "status": โฆ} (with follow=1 it keeps streaming for up to 2 hours) |
GET /api/monitor/logs/{siteId}/stream โ a site's Laravel log, live |
{"line": "<chunk>"}, {"error": "โฆ"} |
runs until you disconnect |
GET /api/monitor/services/{key}/logs โ a service's journal, live |
{"line": "<one line>"}, {"error": "โฆ"} |
runs until you disconnect |
GET /api/system/run/{key}/stream โ System page actions |
{"line": "<one line>"} |
{"done": true, "status": "success" | "failed"} |
The exact behaviour of each stream is in the reference.
Connecting late, or after the end
The panel keeps the output of a finished operation in memory for a while and replays it to a new connection:
- Deploys and provisioning keep their output for 30 minutes. If nothing is running, the deploy stream sends a final event with status
unknownright away โ read the real result from the deployment:GET /api/sites/{id}โdeployments. - Certificates keep the last run for 10 minutes, then send
"status": "unknown". - Artisan sends
"status": "idle"when no command is running. - Fleet runs replay finished runs from the database, so a run's stream always ends with
{"done": true}.
The reliable pattern is: start the operation, then follow its stream, and confirm the outcome with a normal request once the stream ends.
๐ WebSockets
Interactive features use WebSockets. They are meant for the panel's own web app: they authenticate with the browser session token in the URL and do not accept personal access tokens.
| WebSocket | Purpose |
|---|---|
wss://โฆ/api/terminal/{siteId}?token=<session> |
The site terminal (admins and developers with access to the site). |
wss://โฆ/api/terminal/fleet/term/{serverId}?token=<session> |
A server terminal (admin, the server's terminal switch, a recent 2FA step-up with the same session). |
wss://โฆ/api/terminal/fleet/live?token=<session> |
Live metrics for the fleet (admin). |
wss://โฆ/api/terminal/fleet/agent |
The monitoring agent's connection โ see Agent protocol. |
Terminal messages
{ "type": "input", "data": "ls -la\r" }
{ "type": "resize", "cols": 120, "rows": 30 }
The server sends {"type": "output", "data": "โฆ"} with terminal output and {"type": "exit", "exitCode": 0} when the shell ends, then closes the socket.
Errors on WebSockets
The connection upgrade itself succeeds; authentication happens afterwards. A refused connection receives one JSON message such as {"error": "Unauthorized"} and is then closed. The fleet sockets use close codes:
| Close code | Meaning |
|---|---|
4401 |
Not logged in, invalid session, or not an admin. |
4403 |
Not allowed right now: terminal switched off, no 2FA step-up, agent offline or unable to open a terminal. |
4404 |
Server not found. |
4409 |
The agent connected again; the older connection was replaced. |
1013 |
The fleet service is not running on this panel instance. |