Skip to Content
PackagesHost librariesnetwork (web, native)

network is HTTP, WebSocket, and Server-Sent Events from scripts. Import it by bare name or declare it in the host field of packages.json. The native engine builds it in, see the native engine.

The surface is fetch, fetch_text, fetch_json, abort_request, plus WebSocket (ws_open, ws_send, ws_close, ws_state) and Server-Sent Events (sse_open, sse_close, sse_state). HTTP calls suspend until the response arrives. fetch returns the full response as a JSON string with id, ok, status, headers, and body, and abort_request(id) cancels an in-flight request. fetch_text returns the body as a string and fetch_json does the same for you to parse with json.loads. Both raise on a non-2xx status. All three take an optional second argument, a JSON options string (RequestInit in the browser). WebSocket and SSE connections open with a msg tag and stream events through receive(), with payload type values open, message, close, and error. Binary WebSocket frames surface as binary: true only. In the browser, CORS applies: a cross-origin target must return Access-Control-Allow-Origin or the call raises. CORS is a browser rule and does not apply on other hosts.

from network import fetch, fetch_text import json data = json.loads(fetch("https://api.github.com/zen")) print(data["ok"], data["status"]) print(len(fetch_text("https://api.github.com/zen")) > 0)
Output · expected
True 200 True
from network import fetch_json, fetch_text import json data = json.loads(fetch_json("https://api.github.com/")) print(isinstance(data, dict)) try: fetch_text("https://nope.invalid/x") except Exception as e: print(type(e).__name__)
Output · expected
True RuntimeError

Known limitations: on the native engine only fetch, fetch_text, and fetch_json are available, and fetch blocks instead of suspending. WebSocket and SSE are web-only.

Last updated on