Docs

Quickstart

TypeScript quickstart

The @siliconnetwork/sdk package: no runtime dependencies, typed requests and answers, streaming. Node 20 or newer, Bun, Deno and browsers.

1. Install and set the key

setup - zsh
npm install @siliconnetwork/sdk # not on npm yet? clone the repository and install it from therenpm install ./siliconnetwork/sdks/typescript export SILICON_API_KEY=sn_live_...

Create the key on the API Keys page. The client reads SILICON_API_KEY, or takes apiKey. The files below use top level await, so run them as ES modules: for example with npx tsx chat.ts in a project whose package.json has "type": "module".

In a browser, anyone can read a key shipped in the page. Call the API from your server, or use a scoped key with a spend cap (see Key protection).

2. One chat completion

chat.ts
import { SiliconNetwork } from "@siliconnetwork/sdk"; const client = new SiliconNetwork(); // reads SILICON_API_KEY const out = await client.chat.completions.create({  model: "llama-3.2-3b",  messages: [{ role: "user", content: "Say hello in one line." }],});console.log(out.choices[0].message.content);console.log(out.meta.requestId, out.meta.model, out.meta.node);

meta carries the gateway's headers: the request id (quote it to support), the model that served and the node. Silicon Network body fields such as models: ["fast"] (fallbacks) pass through as extra fields.

3. Stream the answer

stream.ts
import { SiliconNetwork } from "@siliconnetwork/sdk"; const client = new SiliconNetwork(); const stream = await client.chat.completions.stream({  model: "llama-3.2-3b",  messages: [{ role: "user", content: "Write a haiku about silicon." }],});for await (const chunk of stream) {  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");}process.stdout.write("\n");

create({ ..., stream: true }) returns the same stream. The promise settles once the gateway accepted the request, so a 402 or 429 rejects before any output.

4. List the models

models.ts
import { SiliconNetwork } from "@siliconnetwork/sdk"; const client = new SiliconNetwork(); const models = await client.models.list();for (const model of models.data) console.log(model.id);

The list is what the network is serving now. Any id or alias in it works as model.

5. Handle an error

errors.ts
import { SiliconError, SiliconNetwork } from "@siliconnetwork/sdk"; const client = new SiliconNetwork(); try {  const out = await client.chat.completions.create({    model: "llama-3.2-3b",    messages: [{ role: "user", content: "Say hello in one line." }],  });  console.log(out.choices[0].message.content);} catch (err) {  if (!(err instanceof SiliconError)) throw err;  if (err.isOutOfCredits) console.error("Add balance on the Billing page:", err.message);  else if (err.isRateLimited) console.error(`Rate limited; retry after ${err.retryAfter} s`);  else console.error(err.status, err.code, err.message, err.requestId);}
SiliconError
FieldMeaning
statusThe HTTP status: 401 bad key, 402 out of credits or over a spend cap, 403 not allowed, 429 rate limited, 5xx the gateway or node failed.
codeThe machine code, when the body carried one.
requestIdThe gateway's request id; quote it to support.
retryAfterSeconds to wait, when the gateway said so.
isOutOfCredits, isRateLimited402 and 429.

Non-streaming calls retry 429 and 5xx twice by default, honoring Retry-After; set maxRetries on the client to change that. Next: Streaming, Structured outputs and tools, Errors.

Silicon Network›Docs›Guides›Quickstarts by language›TypeScript54 items