API

SDK methods, REST endpoints, and custom storage adapter.

SDK API

typescript
// Returns the effective model ID (string) to pass to your AI SDK. Uses override if set, else fallback.
const modelId = await modelKit.getModel("feature-id", "anthropic/claude-3.5-sonnet");
// → e.g. "anthropic/claude-3.5-sonnet" or "anthropic/claude-opus-4" if overridden

// Set or update the runtime override for a feature. modelId required; temperature, maxTokens, topP, topK optional.
await modelKit.setOverride("feature-id", {
  modelId: "anthropic/claude-opus-4",
  temperature: 0.9,
  maxTokens: 4096,
});

// Returns the full override for a feature (modelId, temperature, maxTokens, etc.) or null if no override.
const config = await modelKit.getConfig("feature-id");
// → { modelId, temperature?, maxTokens?, ... } | null — use when you need params, not just the ID

await modelKit.listOverrides();   // All overrides: [{ featureId, override }, ...]
await modelKit.clearOverride("feature-id");   // Remove override; getModel will use fallback

REST API

Hono and Express routers expose:

GET /overrides List all
GET /overrides/:featureId Get one
POST /overrides/:featureId Set override
DELETE /overrides/:featureId Clear override
GET /studio Studio UI (served automatically; configurable via router options)

Router options

Both createModelKitHonoRouter and createModelKitExpressRouter accept an optional options object:

typescript
interface ModelKitRouterOptions {
  /** Serve the Studio UI. Default: true */
  studio?: boolean;
  /** Path to serve Studio at, relative to the mount point. Default: "/studio" */
  studioPath?: string;
  /** Enable CORS (Hono only). Pass true for permissive defaults or a config object. */
  cors?: boolean | CorsOptions;
}

createModelKitHonoRouter(modelKit, { studio: false })         // disable Studio
createModelKitHonoRouter(modelKit, { studioPath: "/admin" })  // custom path → /api/modelkit/admin

Protecting routes (auth)

The ModelKit router exposes write endpoints — anyone who can reach it can set or delete overrides. Protect it with your own middleware registered on the mount path, before the router.

typescript
// Hono — add middleware on the same path before app.route()
app.use("/api/modelkit/*", async (c, next) => {
  if (c.req.header("Authorization") !== `Bearer ${process.env.MODELKIT_SECRET}`) {
    return c.json({ error: "Unauthorized" }, 401);
  }
  await next();
});

app.route("/api/modelkit", createModelKitHonoRouter(modelKit));
typescript
// Express — pass middleware as a second argument to app.use()
const authGuard = (req, res, next) => {
  if (req.headers.authorization !== `Bearer ${process.env.MODELKIT_SECRET}`) {
    return res.status(401).json({ error: "Unauthorized" });
  }
  next();
};

app.use("/api/modelkit", authGuard, createModelKitExpressRouter(modelKit));

Any auth strategy works here — JWT verification, session cookie checks, IP allowlisting, etc. The middleware runs before every route the ModelKit router serves, including the Studio UI.

Custom Storage Adapter

typescript
import type { StorageAdapter } from "@benrobo/modelkit";

function createMyAdapter(): StorageAdapter {
  return {
    async get(featureId) { /* ... */ },
    async set(featureId, override) { /* ... */ },
    async delete(featureId) { /* ... */ },
    async list() { /* ... */ },
  };
}
const modelKit = createModelKit(createMyAdapter());