Skip to content

CGateClient API Reference

Complete API reference for the CG SDK client.

Constructor

new CGateClient(options)

Creates a new CG SDK client instance.

Parameters:

NameTypeRequiredDescription
options.apiKeystringYesYour CognipeerAI Gateway API key
options.baseURLstringNoCustom API base URL (default: https://api.cognipeer.com/api/client/v1)
options.timeoutnumberNoRequest timeout in milliseconds (default: 60000)
options.maxRetriesnumberNoMaximum retry attempts (default: 3)
options.fetchtypeof fetchNoCustom fetch implementation

Returns: CGateClient

Example:

typescript
const client = new CGateClient({
  apiKey: 'your-api-key',
  baseURL: 'https://api.cognipeer.com',
  timeout: 30000,
  maxRetries: 5,
});

Properties

client.chat

Chat completions API resource.

Type: ChatResource

See Chat API for details.

client.embeddings

Embeddings API resource.

Type: EmbeddingsResource

See Embeddings API for details.

client.vectors

Vector operations API resource.

Type: VectorsResource

See Vectors API for details.

client.files

File management API resource.

Type: FilesResource

See Files API for details.

client.tracing

Agent tracing API resource.

Type: TracingResource

See Tracing API for details.

Methods

client.getBaseURL()

Get the configured base URL.

Returns: string

Example:

typescript
const baseURL = client.getBaseURL();
console.log(baseURL); // "https://api.cognipeer.com/api/client/v1"

Type Definitions

CGateClientOptions

typescript
interface CGateClientOptions {
  apiKey: string;
  baseURL?: string;
  timeout?: number;
  maxRetries?: number;
  fetch?: typeof fetch;
}

ChatMessage

typescript
interface ChatMessage {
  role: 'system' | 'user' | 'assistant' | 'tool';
  content: string;
  name?: string;
  tool_calls?: ToolCall[];
  tool_call_id?: string;
}

ChatCompletionRequest

typescript
interface ChatCompletionRequest {
  model: string;
  messages: ChatMessage[];
  temperature?: number;
  top_p?: number;
  max_tokens?: number;
  stream?: boolean;
  stop?: string | string[];
  presence_penalty?: number;
  frequency_penalty?: number;
  user?: string;
  request_id?: string;
  tools?: Tool[];
  tool_choice?: 'none' | 'auto' | object;
}

ChatCompletionResponse

typescript
interface ChatCompletionResponse {
  id: string;
  object: string;
  created: number;
  model: string;
  choices: ChatChoice[];
  usage: Usage;
  request_id?: string;
}

EmbeddingRequest

typescript
interface EmbeddingRequest {
  model: string;
  input: string | string[];
  encoding_format?: 'float' | 'base64';
  user?: string;
  request_id?: string;
}

EmbeddingResponse

typescript
interface EmbeddingResponse {
  object: string;
  data: Embedding[];
  model: string;
  usage: Usage;
  request_id?: string;
}

VectorProvider

typescript
interface VectorProvider {
  _id: string;
  key: string;
  driver: string;
  label: string;
  description?: string;
  status: 'active' | 'inactive' | 'error';
  credentials?: Record<string, unknown>;
  settings?: Record<string, unknown>;
  metadata?: Record<string, unknown>;
  capabilities?: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
}

VectorIndex

typescript
interface VectorIndex {
  _id: string;
  key: string;
  indexId: string;
  name: string;
  dimension: number;
  metric: 'cosine' | 'euclidean' | 'dotproduct';
  providerKey: string;
  metadata?: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
}

Error Types

CGateError

Base error class for all SDK errors.

typescript
class CGateError extends Error {
  constructor(
    message: string,
    statusCode?: number,
    response?: unknown
  );
}

Properties:

  • message: string - Error message
  • statusCode?: number - HTTP status code (if applicable)
  • response?: unknown - Raw response data

CGateAPIError

Error class for API-specific errors.

typescript
class CGateAPIError extends CGateError {
  constructor(
    message: string,
    statusCode: number,
    errorType?: string,
    response?: unknown
  );
}

Properties:

  • All properties from CGateError
  • errorType?: string - API error type (e.g., 'invalid_request_error')

Example:

typescript
import { CGateAPIError } from '@cognipeer/cgate-sdk';

try {
  const response = await client.chat.completions.create({
    model: 'gpt-4',
    messages: [{ role: 'user', content: 'Hello!' }],
  });
} catch (error) {
  if (error instanceof CGateAPIError) {
    console.error('Status:', error.statusCode);
    console.error('Type:', error.errorType);
    console.error('Message:', error.message);
  }
}

Constants

Default Values

typescript
const DEFAULT_BASE_URL = 'https://api.cognipeer.com/api/client/v1';
const DEFAULT_TIMEOUT = 60000; // 60 seconds
const DEFAULT_MAX_RETRIES = 3;

Usage Patterns

Singleton Pattern

typescript
let clientInstance: CGateClient | null = null;

export function getClient(): CGateClient {
  if (!clientInstance) {
    clientInstance = new CGateClient({
      apiKey: process.env.CGATE_API_KEY!,
    });
  }
  return clientInstance;
}

Factory Pattern

typescript
export function createClient(
  environment: 'dev' | 'prod'
): CGateClient {
  return new CGateClient({
    apiKey: process.env[`${environment.toUpperCase()}_API_KEY`]!,
  });
}

Request Wrapper

typescript
async function safeRequest<T>(
  fn: () => Promise<T>
): Promise<T | null> {
  try {
    return await fn();
  } catch (error) {
    if (error instanceof CGateAPIError) {
      console.error('API Error:', error.message);
    }
    return null;
  }
}

const response = await safeRequest(() =>
  client.chat.completions.create({
    model: 'gpt-4',
    messages: [{ role: 'user', content: 'Hello!' }],
  })
);

See Also

Released under the MIT License.