> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reducto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript SDK Overview

> Get started with the Reducto JavaScript SDK

The Reducto JavaScript SDK provides a type-safe interface to the Reducto API. It handles authentication, request formatting, and response parsing automatically.

***

## Quick Start

```javascript theme={null}
import Reducto from 'reductoai';
import fs from 'fs';

// Initialize the client (reads REDUCTO_API_KEY from environment)
const client = new Reducto();

// Upload a document
const upload = await client.upload({ 
  file: fs.createReadStream("invoice.pdf") 
});

// Parse the document
const result = await client.parse.run({ input: upload.file_id });

// Access the extracted content
for (const chunk of result.result.chunks) {
  console.log(chunk.content);
}
```

***

## Key Features

<CardGroup cols={2}>
  <Card title="TypeScript Support" icon="shield-check">
    Full TypeScript definitions with IDE autocomplete for all methods and responses.
  </Card>

  <Card title="Simple API" icon="code">
    Intuitive method names that match the REST API endpoints.
  </Card>

  <Card title="Error Handling" icon="triangle-exclamation">
    Clear error classes with helpful error messages and status codes.
  </Card>

  <Card title="Automatic Retries" icon="repeat">
    Built-in retry logic with exponential backoff for transient errors.
  </Card>
</CardGroup>

***

## Installation

```bash theme={null}
npm install reductoai
```

Or with yarn:

```bash theme={null}
yarn add reductoai
```

Requires Node.js 14+. The SDK includes full TypeScript definitions.

***

## Authentication

Set your API key as an environment variable:

```bash theme={null}
export REDUCTO_API_KEY="your_api_key_here"
```

The SDK reads it automatically:

```javascript theme={null}
import Reducto from 'reductoai';

const client = new Reducto();  // Reads REDUCTO_API_KEY from environment
```

Or pass it explicitly:

```javascript theme={null}
const client = new Reducto({ apiKey: "your_api_key_here" });
```

Get your API key from [Reducto Studio](https://studio.reducto.ai/) → API Keys.

***

## Core Methods

The SDK provides methods for all Reducto endpoints:

### Upload

Upload documents to Reducto's servers before processing:

```javascript theme={null}
const upload = await client.upload({ 
  file: fs.createReadStream("document.pdf") 
});
// Returns: { file_id: "reducto://..." }
```

<Card title="Upload Documentation" icon="upload" href="/sdk/javascript/upload">
  File upload options, size limits, and presigned URLs.
</Card>

### Parse

Convert documents into structured JSON with text, tables, and figures:

```javascript theme={null}
const result = await client.parse.run({ input: upload.file_id });
// Returns: ParseResponse with chunks, blocks, and metadata
```

<Card title="Parse Documentation" icon="file-lines" href="/sdk/javascript/parse">
  Parse configuration, chunking options, and response structure.
</Card>

### Extract

Pull specific fields from documents using JSON schemas:

```javascript theme={null}
const result = await client.extract.run({
  input: upload.file_id,
  instructions: {
    schema: {
      type: "object",
      properties: {
        invoice_number: { type: "string" },
        total: { type: "number" }
      }
    }
  }
});
```

<Card title="Extract Documentation" icon="brackets-curly" href="/sdk/javascript/extract">
  Schema design, array extraction, and citations.
</Card>

### Split

Divide documents into sections based on content type:

```javascript theme={null}
const result = await client.split.run({
  input: upload.file_id,
  split_description: [
    { name: "Introduction", description: "Introduction section" },
    { name: "Results", description: "Results section" }
  ]
});
// Returns: SplitResponse with splits array (name, pages, conf)
```

<Card title="Split Documentation" icon="scissors" href="/sdk/javascript/split">
  Split configuration and section types.
</Card>

### Edit

Fill PDF forms and modify DOCX documents:

```javascript theme={null}
const result = await client.edit.run({
  document_url: upload.file_id,
  edit_instructions: "Fill name with 'John Doe' and date with '12/25/2024'"
});
// Returns: EditResponse with document_url
```

<Card title="Edit Documentation" icon="pen-to-square" href="/sdk/javascript/edit">
  Form schemas and document modification.
</Card>

### Pipeline

Run pre-configured workflows from Reducto Studio:

```javascript theme={null}
const result = await client.pipeline.run({
  input: upload.file_id,
  pipeline_id: "your_pipeline_id"  // From Reducto Studio
});
```

<Card title="Pipeline Documentation" icon="diagram-project" href="/sdk/javascript/pipeline">
  Running studio-configured pipelines.
</Card>

***

## Async Jobs

For long-running operations, use `runJob` methods to get a job ID:

```javascript theme={null}
// Start async job
const job = await client.parse.runJob({ input: upload.file_id });
console.log(job.job_id);

// Check status
const status = await client.job.retrieve(job.job_id);
if (status.status === "Completed") {
  console.log(status.result);
}
```

<Card title="Async Processing" icon="clock" href="/sdk/javascript/async">
  Concurrent processing, rate limiting, and batch operations.
</Card>

***

## Response Types

All SDK methods return strongly-typed response objects:

```typescript theme={null}
import Reducto, { ParseResponse } from 'reductoai';

const client = new Reducto();
const result: ParseResponse = await client.parse.run({ input: upload.file_id });

// Access typed fields
console.log(result.job_id);  // string
console.log(result.usage.num_pages);  // number
console.log(result.result.chunks);  // Chunk[]
```

***

## Error Handling

The SDK throws specific error classes for different error conditions:

```javascript theme={null}
import Reducto, { 
  APIError, 
  AuthenticationError, 
  BadRequestError,
  RateLimitError 
} from 'reductoai';

try {
  const result = await client.parse.run({ input: "invalid-file-id" });
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error(`Auth failed: ${error.message}`);
  } else if (error instanceof BadRequestError) {
    console.error(`Invalid request: ${error.status} - ${error.message}`);
  } else if (error instanceof RateLimitError) {
    console.error(`Rate limited: ${error.message}`);
  } else if (error instanceof APIError) {
    console.error(`API error: ${error.status} - ${error.message}`);
  }
}
```

<Card title="Error Handling Guide" icon="triangle-exclamation" href="/sdk/javascript/error-handling">
  Complete error handling reference with all error types.
</Card>

***

## Advanced Features

### Per-Request Options

Override client settings for individual requests:

```javascript theme={null}
// Custom timeout
await client.parse.run({ input: upload.file_id }, { timeout: 60000 });

// Custom retry settings
const customClient = new Reducto({ maxRetries: 5 });
```

### CommonJS Support

```javascript theme={null}
const Reducto = require('reductoai').default;
const client = new Reducto();
```

***

## Next Steps

<Steps>
  <Step title="Get your API key">
    Create an API key in [Reducto Studio](https://studio.reducto.ai/) and set it as `REDUCTO_API_KEY`.
  </Step>

  <Step title="Try the quickstart">
    Run your first parse operation with the [quickstart example](/quickstart).
  </Step>

  <Step title="Explore the methods">
    Check out the [core methods documentation](/sdk/javascript/parse) for detailed usage.
  </Step>
</Steps>
