SDKS · TYPESCRIPT

TypeScript SDK

Use the official openai npm package. Set baseURL to https://api.hoonify.dev/v1 and pass your Hoonify key. Same client, same generated types, same streaming primitives.

No separate package

Hoonify doesn't publish a separate npm SDK. The OpenAI SDK already covers chat, embeddings, and streaming. Hoonify-only fields are passed inline (with a single @ts-expect-error) or via headers.

Install

shell
npm install openai

Setup

typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.hoonify.dev/v1",
  apiKey: process.env.HOONIFY_API_KEY!,
});

Streaming

typescript
const stream = await client.chat.completions.create({
  model: "llama-3.3-70b",
  messages: [{ role: "user", content: "Tell me a fun fact about octopi." }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

Hoonify extensions

Pin a pool per-request

typescript
const resp = await client.chat.completions.create(
  {
    model: "llama-3.3-70b",
    messages: [...],
  },
  { headers: { "X-Hoonify-Pool": "eu" } },
);

Quantization / top_k

These aren't in the OpenAI types yet — pass them inline with a single @ts-expect-error until we ship a typed shim.

typescript
const resp = await client.chat.completions.create({
  model: "llama-3.3-70b",
  messages: [...],
  // @ts-expect-error — Hoonify-only fields
  quantization: "fp8",
  top_k: 40,
});

Errors

typescript
import OpenAI from "openai";

try {
  await client.chat.completions.create({...});
} catch (e) {
  if (e instanceof OpenAI.APIError) {
    if (e.status === 429) {
      // rate-limited — SDK already retried; surface to caller
    } else if (e.status === 409 && (e.error as any)?.type === "no_capacity") {
      // retry with a different X-Hoonify-Pool
    }
  }
  throw e;
}

Edge runtimes

The OpenAI SDK supports Cloudflare Workers, Deno, Bun, and Vercel Edge. Hoonify works the same way — global anycast on the API, fetch-based transport, no Node APIs in the request path.

typescript
// Vercel Edge / Cloudflare Workers
export const runtime = "edge";

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.hoonify.dev/v1",
  apiKey: env.HOONIFY_API_KEY,    // platform-provided env binding
});

Compute / instances

The instance API isn't in the OpenAI shape — call it directly with fetch:

typescript
const inst = await fetch("https://api.hoonify.dev/v1/instances", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.HOONIFY_API_KEY!}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    sku: "h200-141",
    gpus: 8,
    duration_hours: 4,
  }),
}).then((r) => {
  if (!r.ok) throw new Error(`hoonify ${r.status}`);
  return r.json();
});

Related: Python SDK · OpenAI compatibility