weightsapi.INFERENCEConsole
Navigation
Stream to a browser

Stream to a browser

A same-origin Node server holds the API key and forwards SSE.

Requirements#

Node 22 or newer. Set WEIGHTSAPI_API_KEY to your own key. AI access requires sign-in, one confirmed top-up of at least USD 100, an authorized API key and enough available credit for the request. Each later top-up also has a USD 100 minimum; smaller remaining balances stay usable when they cover the request. Credit requires transaction verification. Check service status for model availability before sending traffic.

Run the recipe#

// Node 22+. Save as server.mjs, set WEIGHTSAPI_API_KEY, then node server.mjs.
import http from "node:http";
const base = process.env.WEIGHTSAPI_BASE_URL || "https://weightsapi.com/v1";
http.createServer(async (req, res) => {
  if (req.url !== "/stream") {
    res.setHeader("Content-Type", "text/html");
    return res.end('<button id="run">Run</button><pre id="out"></pre><script>run.onclick=async()=>{out.textContent="";const r=await fetch("/stream");const reader=r.body.getReader();const decoder=new TextDecoder();for(;;){const {value,done}=await reader.read();if(done)break;out.textContent+=decoder.decode(value,{stream:true});}}<\/script>');
  }
  try {
    const upstream = await fetch(base+"/chat/completions", {method:"POST",
      headers:{"Content-Type":"application/json",Authorization:"Bearer "+process.env.WEIGHTSAPI_API_KEY},
      body:JSON.stringify({model:"Qwen/Qwen3-32B",messages:[{role:"user",content:"Explain SSE."}],stream:true,max_tokens:256})});
    res.writeHead(upstream.status, {"Content-Type":upstream.headers.get("content-type")||"text/plain","Cache-Control":"no-store"});
    for await (const chunk of upstream.body) res.write(chunk);
    res.end();
  } catch {res.writeHead(502);res.end("Upstream unavailable");}
}).listen(8080,"127.0.0.1");

Before production#

Bound concurrency and retries, validate outputs, and handle errors before exposing the workflow to users.