# API

> Use jscpd programmatically in your applications.

## Overview

jscpd provides several integration options depending on your version:

<table>
<thead>
  <tr>
    <th>
      
    </th>
    
    <th>
      v5 (Rust)
    </th>
    
    <th>
      v4 (TypeScript)
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        CLI
      </strong>
    </td>
    
    <td>
      Native binary — fastest option
    </td>
    
    <td>
      Node.js CLI
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Programmatic API
      </strong>
    </td>
    
    <td>
      Rust crates (<code>
        cpd-finder
      </code>
      
      )
    </td>
    
    <td>
      Node.js (<code>
        import { jscpd }
      </code>
      
      )
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        Server
      </strong>
    </td>
    
    <td>
      jscpd-server (Node.js)
    </td>
    
    <td>
      jscpd-server (Node.js)
    </td>
  </tr>
</tbody>
</table>

## v5: Rust Crate API

The core detection engine is available as Rust crates:

```toml [Cargo.toml]
[dependencies]
cpd-finder = "0.1"
```

```rust [src/main.rs]
use cpd_finder::orchestrate;

fn main() {
    let result = orchestrate(&["./src".into()], &Default::default());
    println!("Clones: {}", result.statistics.total_clones);
    println!("Duplication: {:.1}%", result.statistics.total_percentage);
}
```

### Available Crates

<table>
<thead>
  <tr>
    <th>
      Crate
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <strong>
        cpd
      </strong>
    </td>
    
    <td>
      CLI entry point (produces both <code>
        jscpd
      </code>
      
       and <code>
        cpd
      </code>
      
       binaries)
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        cpd-core
      </strong>
    </td>
    
    <td>
      Core detection algorithm (Rabin-Karp rolling hash)
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        cpd-tokenizer
      </strong>
    </td>
    
    <td>
      Source code tokenizer (OXC for JS/TS, generic for 223 formats)
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        cpd-finder
      </strong>
    </td>
    
    <td>
      File walking, orchestration, git blame
    </td>
  </tr>
  
  <tr>
    <td>
      <strong>
        cpd-reporter
      </strong>
    </td>
    
    <td>
      13 output reporters
    </td>
  </tr>
</tbody>
</table>

See [Rust Crates](/api/rust-crates) for full documentation.

### Rust Crate Features

- Pure Rust — no external runtime required
- Parallel detection across format groups via `rayon`
- OXC-based JS/TS/JSX/TSX tokenization
- `gitoxide`-based in-process git blame (`--blame`)
- Cross-format detection for Vue, Svelte, Astro, Markdown

## v4: Node.js API

If you need a JavaScript/TypeScript programmatic API, use `jscpd@4`:

```typescript
import { IClone } from '@jscpd/core';
import { jscpd } from 'jscpd';

// Promise API
const clones: Promise<IClone[]> = jscpd(process.argv);

// Async/Await API
(async () => {
  const clones: IClone[] = await jscpd(['', '', __dirname, '-r', 'json']);
  console.log(clones);
})();
```

### Options

Pass options programmatically:

```typescript
import { jscpd } from 'jscpd';

const options = [
  '', // placeholder for node
  '', // placeholder for script
  './src',
  '--min-lines', '5',
  '--min-tokens', '50',
  '--reporters', 'json',
  '--output', './reports'
];

const clones = await jscpd(options);
```

### Clone Interface

Each detected clone has the following structure:

```typescript
interface IClone {
  format: string;
  lines: number;
  tokens: number;
  firstFile: ISourceLocation;
  secondFile: ISourceLocation;
  duplicationA: ISourceLines;
  duplicationB: ISourceLines;
}

interface ISourceLocation {
  name: string;
  start: number;
  end: number;
  startLoc: ILocation;
  endLoc: ILocation;
}
```

> **Note:** The v4 Node.js API is not available in v5. If you're migrating, see the [Migration Guide](/getting-started/migration) for alternatives.

## CLI

The simplest integration is via the command line (same in both versions):

```bash
jscpd ./src --reporters json --output ./reports
```

See [Installation](/getting-started/installation) and [Configuration](/getting-started/configuration) for full CLI options.

## Server API

For REST API and MCP integration, see the [Server API](/api/server) and [MCP Server](/api/mcp-server) pages.
