# Server API

> Use jscpd-server for remote duplication detection.

## Installation

```bash [Terminal]
npm install -g jscpd-server
```

> **Note:** The jscpd-server is still Node.js based. The CLI detection engine (`jscpd` command) is Rust-based in v5, but the server uses the v4 engine.

## Starting the Server

```bash [Terminal]
# Start server in current directory
jscpd-server

# Start server in specific directory
jscpd-server /path/to/project

# Start server on specific port
jscpd-server . --port 8080
```

## Server Options

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Description
    </th>
    
    <th>
      Default
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        -p
      </code>
      
      , <code>
        --port
      </code>
    </td>
    
    <td>
      Port to run the server on
    </td>
    
    <td>
      3000
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        -H
      </code>
      
      , <code>
        --host
      </code>
    </td>
    
    <td>
      Host to bind the server to
    </td>
    
    <td>
      0.0.0.0
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        -c
      </code>
      
      , <code>
        --config
      </code>
    </td>
    
    <td>
      Path to config file
    </td>
    
    <td>
      <code>
        .jscpd.json
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        -f
      </code>
      
      , <code>
        --format
      </code>
    </td>
    
    <td>
      Format or formats (comma-separated)
    </td>
    
    <td>
      -
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        -i
      </code>
      
      , <code>
        --ignore
      </code>
    </td>
    
    <td>
      Glob pattern for files to exclude
    </td>
    
    <td>
      -
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        -l
      </code>
      
      , <code>
        --min-lines
      </code>
    </td>
    
    <td>
      Minimum lines in a duplication
    </td>
    
    <td>
      5
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        -k
      </code>
      
      , <code>
        --min-tokens
      </code>
    </td>
    
    <td>
      Minimum tokens in a duplication
    </td>
    
    <td>
      50
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        -m
      </code>
      
      , <code>
        --mode
      </code>
    </td>
    
    <td>
      Quality mode (strict, mild, weak)
    </td>
    
    <td>
      strict
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        -a
      </code>
      
      , <code>
        --absolute
      </code>
    </td>
    
    <td>
      Use absolute paths in reports
    </td>
    
    <td>
      false
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        -n
      </code>
      
      , <code>
        --noSymlinks
      </code>
    </td>
    
    <td>
      Don't follow symlinks
    </td>
    
    <td>
      false
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        --skipLocal
      </code>
    </td>
    
    <td>
      Skip duplicates in local folders
    </td>
    
    <td>
      false
    </td>
  </tr>
</tbody>
</table>

## API Endpoints

### Check Code Snippet

**POST** `/api/check`

Check a code snippet for duplications against the scanned codebase:

```bash [Terminal]
curl -X POST http://localhost:3000/api/check \
  -H "Content-Type: application/json" \
  -d '{
    "code": "console.log(\"hello\");\nconsole.log(\"world\");",
    "format": "javascript"
  }'
```

**Parameters:**

- `code` (required, string): The code snippet to check
- `format` (required, string): Programming language/format (e.g. "javascript", "typescript")
- `recheck` (optional, boolean): If `true`, triggers a re-scan before checking

**Response:**

```json
{
  "duplications": [
    {
      "snippetLocation": {
        "startLine": 1,
        "endLine": 5
      },
      "codebaseLocation": {
        "file": "src/utils/helper.js",
        "startLine": 10,
        "endLine": 14,
        "fragment": "function hello() {\n  console.log(\"Hello, World!\");\n}"
      },
      "linesCount": 4
    }
  ],
  "statistics": {
    "totalDuplications": 1,
    "duplicatedLines": 4,
    "totalLines": 5,
    "percentageDuplicated": 80.0
  }
}
```

### Get Project Statistics

**GET** `/api/stats`

Get overall duplication statistics for the scanned codebase:

```bash [Terminal]
curl http://localhost:3000/api/stats
```

### Health Check

**GET** `/api/health`

Check server health and initialization status:

```bash [Terminal]
curl http://localhost:3000/api/health
```

**Response:**

```json
{
  "status": "ready",
  "workingDirectory": "/path/to/project",
  "lastScanTime": "2025-11-17T10:30:00.000Z"
}
```

### API Information

**GET** `/`

Get information about available endpoints.

## CI/CD Integration

```yaml [.github/workflows/jscpd.yml]
name: Check Code Duplication
on: [push, pull_request]

jobs:
  check-duplication:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install jscpd
        run: npm install -g jscpd
      - name: Check for duplications
        run: jscpd --threshold 5 ./src
```
