npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@layer-ai/sdk

v0.5.0

Published

Configure multiple AI models at runtime without code changes or deployments

Downloads

1,270

Readme

@layer-ai/sdk

TypeScript/JavaScript SDK for Layer AI - Manage LLM requests with intelligent routing and fallbacks.

Installation

npm install @layer-ai/sdk
# or
pnpm add @layer-ai/sdk
# or
yarn add @layer-ai/sdk

Quick Start

import { Layer } from '@layer-ai/sdk';

const layer = new Layer({
  apiKey: process.env.LAYER_API_KEY,
  baseUrl: 'http://localhost:3001'
});

// Complete a request through a gate
const response = await layer.complete({
  gate: 'my-gate',
  prompt: 'Explain quantum computing in simple terms'
});

console.log(response.text);

Configuration

Constructor Options

const layer = new Layer({
  apiKey: string;        // Required: Your Layer API key
  baseUrl?: string;      // Optional: API base URL (default: http://localhost:3001)
  adminMode?: boolean;   // Optional: Enable mutation operations (default: false)
});

Admin Mode

By default, the SDK is read-only to prevent accidental mutations in production. To enable create/update/delete operations, set adminMode: true:

const layer = new Layer({
  apiKey: process.env.LAYER_API_KEY,
  adminMode: true  // Required for mutations
});

// Now you can create/update/delete gates and keys
await layer.gates.create({
  name: 'my-gate',
  model: 'gpt-4o',
  temperature: 0.7
});

Note: Admin mode is intended for setup scripts and infrastructure-as-code. For ongoing management, use the CLI or config files.

API Reference

Completions

layer.complete(params)

Send a completion request through a gate.

const response = await layer.complete({
  gate: 'my-gate',
  prompt: 'What is the capital of France?',

  // Optional overrides (if gate allows)
  model?: 'gpt-4o',
  temperature?: 0.8,
  maxTokens?: 500,
  topP?: 0.9
});

// Response shape
{
  text: string;
  model: string;
  usage: {
    promptTokens: number;
    completionTokens: number;
    totalTokens: number;
  };
  cost: number;
}

Gates

layer.gates.list()

List all gates.

const gates = await layer.gates.list();
// Returns: Gate[]

layer.gates.get(name)

Get a specific gate by name.

const gate = await layer.gates.get('my-gate');

layer.gates.create(data) ⚠️ Requires Admin Mode

Create a new gate. For detailed information about gate configuration fields, see the Configuration Guide.

await layer.gates.create({
  name: 'my-gate',
  model: 'gpt-4o',
  description: 'My custom gate',
  systemPrompt: 'You are a helpful assistant',
  temperature: 0.7,
  maxTokens: 1000,
  topP: 1.0,
  allowOverrides: true,  // or specific fields: ['temperature', 'maxTokens']
  routingStrategy: 'fallback',
  fallbackModels: ['claude-sonnet-4', 'gemini-2.0-flash-exp'],
  tags: ['production', 'general']
});

layer.gates.update(name, data) ⚠️ Requires Admin Mode

Update an existing gate.

await layer.gates.update('my-gate', {
  temperature: 0.8,
  maxTokens: 1500
});

layer.gates.delete(name) ⚠️ Requires Admin Mode

Delete a gate.

await layer.gates.delete('my-gate');

layer.gates.suggestions(gateName)

Get AI-powered model recommendations for a gate based on its task description.

const suggestions = await layer.gates.suggestions('my-gate');

console.log(suggestions.primary);       // "gpt-4o"
console.log(suggestions.alternatives);  // ["claude-sonnet-4", "gemini-2.5-flash"]
console.log(suggestions.reasoning);     // "Based on your task description..."

Smart routing analyzes your gate's description and user preferences (cost, latency, quality weights) to recommend the best models from the registry.

API Keys

layer.keys.list()

List all API keys.

const keys = await layer.keys.list();

layer.keys.create(name) ⚠️ Requires Admin Mode

Create a new API key.

const key = await layer.keys.create('my-app-key');
// Returns: { id: string, name: string, key: string, createdAt: string }
// Save the key - it's only shown once!

layer.keys.delete(id) ⚠️ Requires Admin Mode

Revoke an API key.

await layer.keys.delete('key-id');

Logs

layer.logs.list(options?)

List request logs.

const logs = await layer.logs.list({
  limit: 100,
  offset: 0,
  gateName: 'my-gate'  // Optional filter
});

TypeScript Support

The SDK is written in TypeScript and provides full type definitions:

import { Layer, Gate, CompletionRequest, CompletionResponse } from '@layer-ai/sdk';

const layer = new Layer({ apiKey: 'key' });

// All methods are fully typed
const response: CompletionResponse = await layer.complete({
  gate: 'my-gate',
  prompt: 'Hello'
});

Error Handling

try {
  const response = await layer.complete({
    gate: 'my-gate',
    prompt: 'Hello'
  });
} catch (error) {
  if (error instanceof Error) {
    console.error('Layer error:', error.message);
  }
}

Examples

For complete working examples, check out the layer-ai-examples repository:

  • Content Generator - Next.js app with smart routing for content generation

Basic Completion

import { Layer } from '@layer-ai/sdk';

const layer = new Layer({
  apiKey: process.env.LAYER_API_KEY
});

const response = await layer.complete({
  gate: 'default',
  prompt: 'Explain machine learning'
});

console.log(response.text);

With Parameter Overrides

const response = await layer.complete({
  gate: 'my-gate',
  prompt: 'Write a haiku about coding',
  temperature: 0.9,  // More creative
  maxTokens: 100
});

Infrastructure as Code

import { Layer } from '@layer-ai/sdk';

const layer = new Layer({
  apiKey: process.env.LAYER_API_KEY,
  adminMode: true
});

// Create gates programmatically
await layer.gates.create({
  name: 'production-gate',
  model: 'gpt-4o',
  temperature: 0.7,
  routingStrategy: 'fallback',
  fallbackModels: ['claude-sonnet-4']
});

await layer.gates.create({
  name: 'dev-gate',
  model: 'gpt-4o-mini',
  temperature: 0.5
});

License

MIT