要求#
Node 22 或更高版本。将 WEIGHTSAPI_API_KEY 设置为您的密钥。AI 访问需要登录、一次至少 100 美元的确认充值、一个已授权的 API 密钥以及足够的可用余额。每次后续充值的最低金额也为 100 美元;较小的剩余余额在足以支付请求时仍可使用。余额需要交易验证。发送流量前请查看服务状态 了解模型可用性。
运行配方#
// 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");
上线之前#
在向用户公开工作流之前,限制并发和重试、验证输出并处理错误。