Skip to main content

TypeScript SDK

Official TypeScript SDK for Shim. Zero dependencies, works in Node.js 18+ and browsers.

Installation

npm
npm install shim-sdk
yarn
yarn add shim-sdk
pnpm
pnpm add shim-sdk

Quick Start

import { ShimClient } from 'shim-sdk';

const shim = new ShimClient({
  apiKey: process.env.SHIM_API_KEY
});

// Batch repair
const result = await shim.repair({
  raw_output: '{"name": "John", "age": 30'
});

console.log(result.repaired); // { name: "John", age: 30 }

Client Configuration

new ShimClient(config)

const shim = new ShimClient({
  apiKey: string,      // Required
  baseUrl?: string     // Optional, default: 'https://api.shim.so'
});
Parameters:
  • apiKey (string, required): Your Shim API key
  • baseUrl (string, optional): Custom API base URL (for testing)
Example:
const shim = new ShimClient({
  apiKey: process.env.SHIM_API_KEY,
  baseUrl: 'https://api.shim.so' // Default
});

Batch Repair

client.repair(request)

Fix malformed JSON in one request.
const result = await shim.repair({
  raw_output: string,
  schema?: JSONSchema,
  mode?: 'strict' | 'lenient'
});
Parameters:
  • raw_output (string, required): Malformed JSON string
  • schema (JSONSchema, optional): JSON Schema for validation
  • mode (‘strict’ | ‘lenient’, optional): Repair mode
Returns: Promise<RepairResponse> Example:
const result = await shim.repair({
  raw_output: '{"name": "John", "age": "30"',
  schema: {
    type: 'object',
    properties: {
      name: { type: 'string' },
      age: { type: 'number' }
    }
  }
});

if (result.success) {
  console.log(result.repaired); // { name: "John", age: 30 }
  console.log(result.metadata.confidence); // "medium"
}

Streaming Repair

client.stream.start(options)

Start a new streaming session.
const session = await shim.stream.start({
  schema?: JSONSchema,
  mode?: 'strict' | 'lenient'
});
Parameters:
  • schema (JSONSchema, optional): JSON Schema for validation
  • mode (‘strict’ | ‘lenient’, optional): Repair mode
Returns: Promise<{ session_id: string; expires_at: number }> Example:
const session = await shim.stream.start({
  schema: {
    type: 'object',
    properties: {
      name: { type: 'string' }
    }
  }
});

console.log(session.session_id); // "sess_abc123"

client.stream.push(request)

Push a chunk to an active streaming session.
const result = await shim.stream.push({
  session_id: string,
  chunk: string
});
Parameters:
  • session_id (string, required): Session ID from stream.start()
  • chunk (string, required): Chunk of JSON to process
Returns: Promise<{ state: StreamingState }> Example:
const result = await shim.stream.push({
  session_id: session.session_id,
  chunk: '{"name": "Jo'
});

console.log(result.state.structurally_complete); // false
console.log(result.state.buffered); // '{"name": "Jo'

client.stream.finalize(request)

Finalize a streaming session and get the repaired result.
const result = await shim.stream.finalize({
  session_id: string
});
Parameters:
  • session_id (string, required): Session ID from stream.start()
Returns: Promise<RepairResponse> Example:
const result = await shim.stream.finalize({
  session_id: session.session_id
});

console.log(result.repaired); // { name: "John" }

Complete Streaming Example

import { ShimClient } from 'shim-sdk';

const shim = new ShimClient({
  apiKey: process.env.SHIM_API_KEY
});

// Start session
const session = await shim.stream.start();

// Push chunks as they arrive from LLM
await shim.stream.push({
  session_id: session.session_id,
  chunk: '{"name": "Jo'
});

await shim.stream.push({
  session_id: session.session_id,
  chunk: 'hn", "age": 30'
});

// Finalize when stream is complete
const result = await shim.stream.finalize({
  session_id: session.session_id
});

console.log(result.repaired); // { name: "John", age: 30 }

TypeScript Support

The SDK includes full TypeScript definitions.
import { ShimClient, RepairResponse, StreamingState } from 'shim-sdk';

const shim = new ShimClient({ apiKey: 'sk_live_...' });

const result: RepairResponse = await shim.repair({
  raw_output: '{}'
});

// Full IntelliSense support
console.log(result.metadata.confidence); // "high" | "medium" | "low" | "n/a"

Error Handling

The SDK throws errors for network failures. Shim always returns HTTP 200 with structured error responses.
try {
  const result = await shim.repair({
    raw_output: 'invalid json'
  });

  if (!result.success) {
    console.error('Repair failed:', result.metadata.errors);
  }
} catch (error) {
  console.error('Network error:', error);
}

Next Steps

Batch Endpoint

API reference for batch repair

Streaming Endpoints

API reference for streaming repair

Response Object

Full response schema

GitHub

View source code