Skip to content
Written for developers. Just using FluxPlugs? Go to the user guide.

Network requests

Declare network and list your servers to send HTTPS requests to them. A plugin can also ask the user to approve servers of their own, such as one on their computer. The main process makes each request and returns the status and text of the reply. It needs FluxPlugs 0.5.0-beta.1 or later.

Add a network field next to the capability:

{
"capabilities": ["settings", "network"],
"network": {
"hosts": ["api.example.com", "cdn.example.com"]
}
}

The field is required exactly when capabilities lists network; otherwise the manifest is refused with invalid_plugin_network. List 1 to 16 unique hosts (none only with userServers), each a lowercase public DNS name with at least two labels: no IP addresses, wildcards, ports or trailing dot. Names under localhost, local, localdomain, internal, lan, home, test, invalid, example, onion and arpa are refused.

The hosts are part of what the user trusts. The enable prompt lists them under Connects to, and a plugin whose hosts change needs to be confirmed again, like one whose capabilities change.

Requests need no user action. fetch resolves with the reply, including 4xx and 5xx replies; it rejects only when the request is refused or doesn’t finish.

try {
const response = await api.network.fetch({
url: "https://api.example.com/v1/status?lang=" + encodeURIComponent("en"),
headers: { Accept: "application/json" },
});
if (response.status === 200) {
const data: unknown = JSON.parse(response.body);
api.logger.info("Status loaded", { object: typeof data === "object" });
} else {
api.logger.warn("Status request failed", { status: response.status });
}
} catch (error) {
api.logger.warn("Status request refused", {
code: error instanceof Error ? error.message : "unknown",
});
}

For a POST, pass the body as a string and set its Content-Type. Without one, FluxPlugs sends text/plain;charset=utf-8.

async function translate(text: string): Promise<string | undefined> {
const apiKey = await api.settings.get<string>("apiKey");
const response = await api.network.fetch({
url: "https://api.example.com/v1/translate",
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + (apiKey ?? ""),
},
body: JSON.stringify({ text, target: "en" }),
});
if (response.status === 429) return undefined;
if (response.status < 200 || response.status >= 300)
throw new Error("http_" + response.status);
const data = JSON.parse(response.body) as { translation?: unknown };
return typeof data.translation === "string" ? data.translation : undefined;
}
try {
const translated = await translate("Hola");
if (translated !== undefined) await api.ui.notify(translated);
} catch {
await api.ui.notify("The translation service didn't answer.");
}

Keep an API key in a secret setting or a stored secret, and never log it or show it in a notice.

A request may take 10 seconds unless it sets timeoutMs, from 1000 to 120000 milliseconds. Ask for more only when the service needs it, such as one that writes a long answer before replying.

To cancel a request, pass an AbortSignal in the second argument. Aborting it rejects the request with network_cancelled, and the main process stops it.

const controller = new AbortController();
const cancel = await api.ui.registerAttachmentAction(
{ id: "cancel-report", label: "Cancel report" },
() => controller.abort(),
);
try {
const response = await api.network.fetch(
{
url: "https://api.example.com/v1/report",
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ period: "week" }),
timeoutMs: 60_000,
},
{ signal: controller.signal },
);
api.logger.info("Report ready", { status: response.status });
} catch (error) {
const code = error instanceof Error ? error.message : "unknown";
if (code !== "network_cancelled")
await api.ui.notify("The report service didn't answer.");
} finally {
cancel();
}

A plugin that works with a server the user runs, such as a local AI model or a home server, can’t list it in the manifest: only the user knows its address. Set userServers instead, and ask the user to approve each server:

{
"capabilities": ["settings", "network"],
"network": {
"hosts": ["api.example.com"],
"userServers": true
}
}

With userServers, hosts may be empty. The enable prompt tells the user that the plugin can ask them to approve servers of their own, and a plugin that adds or drops userServers in an update needs to be confirmed again, like one whose hosts change.

network.requestServer(origin) opens a FluxPlugs dialog: “Let Plugin connect to origin?”, with where the server is (this computer, the local network or the internet). The promise resolves to true when the user allows it and false when they decline. It needs a recent user action, which it uses up, or your settings page on screen. network.servers() returns the approved origins.

cleanup.push(
await api.commands.register(
{
name: "connect",
description: "Connects to your server",
usage: "<address>",
},
async ({ input }) => {
const origin = input || "http://localhost:11434";
try {
const allowed =
(await api.network.servers()).includes(origin) ||
(await api.network.requestServer(origin));
if (!allowed) return;
const response = await api.network.fetch({ url: origin + "/api/tags" });
await api.ui.notify("The server answered with " + response.status);
} catch (error) {
const code = error instanceof Error ? error.message : "unknown";
await api.ui.notify(
code === "network_unreachable"
? "Nothing is answering at " + origin + ". Is the server running?"
: "Could not reach " + origin + " (" + code + ")",
);
}
},
),
);

The origin is a scheme, host and port, with no path, query or credentials:

  • https: on a public DNS name, on any port.
  • http: or https: on localhost, 127.0.0.0/8, [::1] or a private IPv4 address (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). Plain http: needs an explicit port, so http://localhost and port 80 are refused.
  • Names such as printer.local, link-local addresses (169.254.x.x), 100.64.0.0/10 and other special addresses are refused.

When the user approves plain http on a private address, the dialog also warns that the connection isn’t encrypted, so others on their network could read what’s sent, including API keys.

A user can approve 8 servers per plugin, and remove them from the plugin’s row on the Plugins page in Fluxer Settings → FluxPlugs. Approvals are dropped when a plugin stops asking for user servers.

A request to an approved server must match its origin exactly, and redirects must stay on that origin. localhost must resolve only to loopback addresses, and a public name only to public addresses; otherwise the request fails with network_private_address. The other rules below apply as for listed hosts.

Error Meaning
invalid_server_origin The origin breaks the rules above
user_gesture_required No recent user action, and the plugin’s settings page isn’t on screen
server_request_busy The plugin’s previous dialog is still open
server_request_declined The user declined this origin less than 30 seconds ago
server_limit The user already approved 8 servers for this plugin
capability_denied:network The manifest doesn’t set userServers, or the user hasn’t confirmed it
  • URL: at most 8 KiB, with no credentials. For a listed host, HTTPS only and no port; for an approved server, its exact origin. The fragment is dropped.
  • Addresses: a listed host must resolve to public IPv4 addresses. FluxPlugs pins the address it checked and refuses private and reserved ranges, except for approved servers on this computer or the local network. IPv6-only hosts and system proxies aren’t supported.
  • Headers: at most 16, with printable ASCII values of up to 2048 characters. FluxPlugs sets User-Agent: FluxPlugs and Accept-Encoding: identity. You can’t set host, cookie, cookie2, set-cookie, origin, referer, connection, keep-alive, content-length, transfer-encoding, te, trailer, upgrade, expect, via, date, dnt, forwarded, accept-encoding, accept-charset, user-agent, x-http-method, x-http-method-override or x-method-override, or any name starting with proxy-, sec-, x-forwarded- or access-control-. Names are compared without case.
  • Cookies: none are sent or stored.
  • Body: only with POST, at most 256 KiB of UTF-8.
  • Reply: at most 256 KiB of UTF-8 text. Compressed and binary replies are refused. The result has status, contentType when the server sent one, and body.
  • Redirects: up to 3, each checked like the original URL; from an approved server, only to the same origin. A 303, or a 301 or 302 after a POST, continues as a GET without body. Headers you set are dropped when a redirect leads to another host, and a 307 or 308 with a body is followed only on the same host; to another host it fails with network_redirect_denied.
  • Time: 10 seconds for the whole request, including DNS, redirects and the reply, or the request’s timeoutMs (1 to 120 seconds).
  • Concurrency: 4 requests in flight per plugin instance, and 32 plugin instances with requests in flight. Stopping the plugin cancels its requests.
Error Meaning
invalid_network_request Malformed request, http URL on a public host, forbidden header, GET body, timeoutMs out of range, too big
network_host_denied The host, or a redirect’s host, isn’t confirmed or approved
network_redirect_denied A redirect to a non-HTTPS URL, one with credentials or a port, off an approved server’s origin, or a 307 or 308 that would carry a body to another host
network_redirect_limit More than 3 redirects
network_private_address The host resolves to a private or reserved address
network_timeout Took longer than 10 seconds, or the request’s timeoutMs
network_cancelled The signal was aborted, or the plugin stopped
network_unreachable Nothing is listening, such as a local server that isn’t running
network_dns_failed The host name couldn’t be found
network_connection_closed The server closed the connection before it finished answering
network_tls_failed The secure connection failed, for example because something on the network intercepts it
network_response_too_large The reply is over 256 KiB
network_response_not_text / network_encoding_unsupported The reply isn’t UTF-8 text, or it is compressed
network_limit Too many requests in flight; wait for earlier ones
network_failed The connection failed for another reason
invalid_network_response The host’s answer failed validation
capability_denied:network The manifest doesn’t declare network, or it isn’t confirmed
plugin_stopped / plugin_changed The instance stopped or its code changed; ignore the result

Everything a request carries reaches the listed servers, or the servers the user approved, without the user seeing it. A plugin that also has decorations or message_content could send the ids or text of messages on screen, and the enable prompt warns about that combination. Send only what the feature needs, and say in your description which service you use and what you send to it. If you send other people’s messages, ask the user before the first request to each server.

FluxPlugs is an independent project, not affiliated with or endorsed by Fluxer.