Skip to main content
The Layer SDK provides a unified interface for AI capabilities across multiple providers. Use one SDK to access chat, image generation, embeddings, text-to-speech, video generation, and OCR.

Installation

npm install @layer-ai/sdk

Quick Start

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

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

const response = await layer.chat({
  gateId: 'your-gate-id',
  data: {
    messages: [
      { role: 'user', content: 'Hello!' }
    ]
  }
});

console.log(response.content);

Configuration

Initialize the Client

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

const layer = new Layer({
  apiKey: 'your-api-key',        // Required
  baseUrl: 'https://custom.api'  // Optional, defaults to https://api.uselayer.ai
});
Never commit API keys to version control. Use environment variables.

Get Your API Key

  1. Sign up at uselayer.ai/signup
  2. Get your API key from the Dashboard
  3. Store it in an environment variable
.env
LAYER_API_KEY=your-api-key-here

Available Methods

The SDK provides methods for different AI capabilities:

Response Structure

All SDK methods return a LayerResponse object:
interface LayerResponse {
  // Common fields
  id: string;              // Request ID
  model: string;           // Model used
  cost: number;            // Request cost in USD
  latency: number;         // Response time in ms

  // Method-specific fields
  content?: string;        // Chat response text
  data?: any[];            // Image/video/embedding data
  url?: string;            // Audio file URL
  text?: string;           // OCR extracted text

  // Usage statistics
  usage?: {
    promptTokens: number;
    completionTokens: number;
    totalTokens: number;
  };
}

Error Handling

Wrap SDK calls in try-catch blocks:
try {
  const response = await layer.chat({
    gateId: 'your-gate-id',
    data: {
      messages: [{ role: 'user', content: 'Hello!' }]
    }
  });

  console.log(response.content);
} catch (error) {
  console.error('Error:', error.message);
}

Common Error Messages

ErrorCause
Invalid tokenCheck your API key
Gate not foundVerify gate ID is correct
Rate limit exceededToo many requests, slow down
Insufficient creditsAdd credits to your account

TypeScript Support

The SDK is fully typed:
import { Layer, ChatRequest, LayerResponse } from '@layer-ai/sdk';

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

// TypeScript knows the structure
const response: LayerResponse = await layer.chat({
  gateId: 'your-gate-id',
  data: {
    messages: [
      { role: 'user', content: 'Hello!' }
    ]
  }
});

// Autocomplete and type safety
console.log(response.content);  // TypeScript knows this exists
console.log(response.cost);     // TypeScript knows this is a number

Environment Variables

Best practice for managing credentials:
// Good ✅
const layer = new Layer({
  apiKey: process.env.LAYER_API_KEY
});

// Bad ❌
const layer = new Layer({
  apiKey: 'lyr_hardcoded_key'  // Never do this!
});

Next Steps

Explore specific capabilities: Or learn about core concepts: