# 6. Get results

<mark style="color:green;">`POST`</mark> `/documentClassification/getResults`

This endpoint retrieves the classification results for a specific batch of documents.

By providing the `classificationId` and `batchId`, you can obtain the predicted document types for each file in the batch,.

Optionally, including a `fileId` will return results for that specific file only. This is useful for retrieving targeted results without querying the entire batch.

## Server URL

```
https://api.extracta.ai/api/v1
```

## Headers

| Name          | Value              |
| ------------- | ------------------ |
| Content-Type  | `application/json` |
| Authorization | `Bearer <token>`   |

## Body

<table><thead><tr><th width="242">Name</th><th width="99">Type</th><th width="126">Required</th><th>Description</th></tr></thead><tbody><tr><td><code>classificationId</code></td><td>string</td><td><code>true</code></td><td>The ID of the classification.</td></tr><tr><td><code>batchId</code></td><td>string</td><td><code>true</code></td><td>The ID of the batch.</td></tr><tr><td><code>fileId</code></td><td>string</td><td><code>false</code></td><td>The ID of the file.</td></tr></tbody></table>

## Body Example

```json
{
    "classificationId": "classificationId",
    "batchId": "batchId",
    "fileId": "fileId" // optional
}
```

## ⚠️ Important

To avoid rate-limiting, please ensure a delay of 2 seconds between consecutive requests to this endpoint.

## Code Example

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const axios = require('axios');

async function getClassificationResults(token, classificationId, batchId, fileId) {
    const url = "https://api.extracta.ai/api/v1/documentClassification/getResults";

    try {
        const payload = {
            classificationId,
            batchId
        };

        if (fileId) {
            payload.fileId = fileId;
        }

        const response = await axios.post(url, payload, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${token}`
            }
        });

        return response.data;
    } catch (error) {
        throw error.response ? error.response.data : new Error('An unknown error occurred');
    }
}

async function main() {
    const token = 'apiKey';
    const classificationId = 'classificationId';
    const batchId = 'batchId';
    const fileId = null; // or provide a specific file ID

    try {
        const results = await getClassificationResults(token, classificationId, batchId, fileId);
        console.log("Classification Results:", results);
    } catch (error) {
        console.error("Failed to retrieve classification results:", error);
    }
}

main();
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

def get_classification_results(token, classification_id, batch_id, file_id=None):
    url = "https://api.extracta.ai/api/v1/documentClassification/getResults"
    
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {token}'
    }
    
    payload = {
        'classificationId': classification_id,
        'batchId': batch_id
    }

    if file_id:
        payload['fileId'] = file_id

    response = requests.post(url, json=payload, headers=headers)
    
    if response.status_code == 200:
        return response.json()
    else:
        response.raise_for_status()

# Example usage
if __name__ == "__main__":
    token = 'apiKey'
    classification_id = 'classificationId'
    batch_id = 'batchId'
    file_id = None  # Or use a specific file ID like 'file123'

    try:
        results = get_classification_results(token, classification_id, batch_id, file_id)
        print("Classification Results:", results)
    except Exception as e:
        print("Failed to retrieve classification results:", e)

```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

function getClassificationResults($token, $classificationId, $batchId, $fileId = null) {
    $url = 'https://api.extracta.ai/api/v1/documentClassification/getResults';

    // Initialize cURL session
    $ch = curl_init($url);
    
    // Prepare the payload
    $payload = [
        'classificationId' => $classificationId,
        'batchId' => $batchId
    ];

    if ($fileId !== null) {
        $payload['fileId'] = $fileId;
    }

    $payload = json_encode($payload);

    // Set cURL options
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $token,
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);

    try {
        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            throw new Exception('Curl error: ' . curl_error($ch));
        }

        return $response;
    } catch (Exception $e) {
        return 'Error: ' . $e->getMessage();
    } finally {
        curl_close($ch);
    }
}

// Example usage
$token = 'apiKey';
$classificationId = 'classificationId';
$batchId = 'batchId';
$fileId = null; // or 'yourFileId' if you want to filter by file

try {
    $results = getClassificationResults($token, $classificationId, $batchId, $fileId);
    echo $results;
} catch (Exception $e) {
    echo "Failed to retrieve classification results: " . $e->getMessage();
}

?>
```

{% endtab %}
{% endtabs %}

## Responses

In case a file is linked to an extraction template, the response will also include an **`extraction`** object with:

* `extractionId` → the ID of the created extraction job
* `batchId` → the batch in which the file was processed
* `fileId` → the ID of the file that was uploaded and processed in the **data extraction module**

{% tabs %}
{% tab title="200 for all files" %}

```json
{
    "status": "success",
    "classificationId": "classificationId",
    "batchId": "batchId",
    "files": [
        {
            "fileId": "fileId1",
            "fileName": "File 1.pdf",
            "status": "processed",
            "result": {
                "confidence": 0.9,
                "documentType": "invoice"
            },
            "url": "fileUrl"
        },
        {
            "fileId": "fileId1",
            "fileName": "File 2.pdf",
            "status": "processed",
            "result": {
                "confidence": 0.95,
                "documentType": "resume"
            },
            "extraction": {
                "extractionId": "extractionId",
                "batchId": "batchId",
                "fileId": "fileId"
            },
            "url": "fileUrl"
        },
        {
            "fileId": "fileId2",
            "fileName": "File 3.pdf",
            "status": "processed",
            "result": {
                "confidence": 0.95,
                "documentType": "other"
            },
            "url": "fileUrl"
        },
        ...
    ]
}
```

{% endtab %}

{% tab title="200 for a single file" %}

```json
{
    "status": "success",
    "classificationId": "classificationId",
    "batchId": "batchId",
    "fileId": "fileId",
    "files": [
        {
            "fileId": "fileId",
            "fileName": "File 1.pdf",
            "status": "processed",
            "result": {
                "confidence": 0.9,
                "documentType": "invoice"
            },
            "url": "fileUrl"
        }
    ]
}
```

{% endtab %}

{% tab title="200 status waiting" %}

```json
{
    "status": "waiting",
    "classificationId": "-OPXQuIrkHuA2QZkY-44",
    "batchId": "8pMpPs6SMPBpcnnznoaRz8bsj"
}
```

{% endtab %}

{% tab title="400" %}

```json
{
    "status": "error",
    "message": "Internal server error"
}
```

{% endtab %}
{% endtabs %}
