# 3. Update extraction

<mark style="color:green;">`PATCH`</mark> `/updateExtraction`

Updates an existing document extraction process by modifying **specified parameters** within the extraction details.&#x20;

Only the parameters **included** in the `extractionDetails` will be **updated**; any parameters **not included** will remain **unchanged**. Use this to efficiently adjust specific aspects of an extraction process without altering its overall configuration.

## Postman Collection

For a complete and interactive set of API requests, please refer to our [postman-integration](https://docs.extracta.ai/data-extraction-api/postman-integration "mention")collection.

## Server URL

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

## Headers

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

## Body

<table><thead><tr><th width="183">Name</th><th width="101">Type</th><th width="104">Required</th><th width="199">Description</th><th>Dedicated page</th></tr></thead><tbody><tr><td><code>extractionId</code></td><td>string</td><td><code>true</code></td><td>The extraction Id</td><td></td></tr><tr><td><code>name</code></td><td>string</td><td><code>false</code></td><td>A descriptive name for the extraction.</td><td></td></tr><tr><td><code>description</code></td><td>string</td><td><code>false</code></td><td>A description for the extraction.</td><td></td></tr><tr><td><code>language</code></td><td>string</td><td><code>false</code></td><td>Document's language for accurate extraction.</td><td><a data-mention href="../extraction-details/supported-languages">supported-languages</a></td></tr><tr><td><code>options</code></td><td>object</td><td><code>false</code></td><td>Additional processing options.</td><td><a data-mention href="../extraction-details/options">options</a></td></tr><tr><td><code>fields</code></td><td>object</td><td><code>false</code></td><td>An array of objects, each specifying a field to extract.</td><td><a data-mention href="../extraction-details/fields">fields</a></td></tr></tbody></table>

To fully customize your data extraction request, understanding the `fields` parameter is crucial. This parameter allows you to specify exactly what information you want to extract, with options for `string`, `object`, and `array` types to match your data structure needs.

{% content-ref url="../extraction-details/fields" %}
[fields](https://docs.extracta.ai/data-extraction-api/extraction-details/fields)
{% endcontent-ref %}

Customize your extraction process with additional options such as table analysis and handwritten text recognition.

{% content-ref url="../extraction-details/options" %}
[options](https://docs.extracta.ai/data-extraction-api/extraction-details/options)
{% endcontent-ref %}

## Body Example

```json
{
    "extractionId": "extractionId",
    "extractionDetails": {
        "name": "CV - English",
        "description": "test",
        "language": "English",
        "options": {
            "hasTable": false,
            "handwrittenTextRecognition": true
        },
        "fields": [
            {
                "key": "name",
                "description": "the name of the person",
                "example": "John"
            },
            {
                "key": "email",
                "description": "the email of the person",
                "example": "john@email.com"
            }
        ]
    }
}
```

## Code Example

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

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

/**
 * Updates an existing document extraction with the provided details.
 * 
 * @param {string} token - The authorization token for API access.
 * @param {string} extractionId - The ID of the extraction to update.
 * @param {Object} extractionDetails - The new details of the extraction to update.
 * @returns {Promise<Object>} The promise that resolves to the API response with the updated extraction details.
 */
async function updateExtraction(token, extractionId, extractionDetails) {
    const url = "https://api.extracta.ai/api/v1/updateExtraction";

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

        // Handling response
        return response.data; // Directly return the parsed JSON response
    } catch (error) {
        // Handling errors
        throw error.response ? error.response.data : new Error('An unknown error occurred');
    }
}

async function main() {
    const token = 'apiKey';
    const extractionId = 'extractiondId'; // Placeholder for actual extraction ID
    const extractionDetails = {
        "name": "CV - English",
        "description": "...",
        "language": "English",
        "options": {
            "hasTable": false,
            "handwrittenTextRecognition": true
        },
        "fields": [
            {
                "key": "name",
                "description": "the name of the person",
                "example": "John"
            },
            {
                "key": "email",
                "description": "the email of the person",
                "example": "john@email.com"
            }
        ]
    };

    try {
        const response = await updateExtraction(token, extractionId, extractionDetails);
        console.log("Extraction Updated:", response);
    } catch (error) {
        console.error("Failed to update extraction:", error);
    }
}

main();
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

def update_extraction(token, extraction_id, extraction_details):
    url = "https://api.extracta.ai/api/v1/updateExtraction"
    headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
    payload = {
        "extractionId": extraction_id,
        "extractionDetails": extraction_details
    }

    try:
        response = requests.patch(url, json=payload, headers=headers)
        response.raise_for_status()  # Raises an HTTPError if the response status code indicates an error
        return response.json()  # Returns the parsed JSON response
    except requests.RequestException as e:
        # Handles any requests-related errors
        print(e)
        return None

# Example usage
if __name__ == "__main__":
    token = "apiKey"
    extraction_id = "extractionId"  # Placeholder for the actual extraction ID to be updated
    extraction_details = {
        "name": "CV - English",
        "description": "...",
        "language": "English",
        "options": {
            "hasTable": False,
            "handwrittenTextRecognition": True
        },
        "fields": [
            {
                "key": "name",
                "description": "the name of the person",
                "example": "John"
            },
            {
                "key": "email",
                "description": "the email of the person",
                "example": "john@email.com"
            }
        ]
    }

    response = update_extraction(token, extraction_id, extraction_details)
    print("Extraction Updated:", response)
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

/**
 * Updates an existing document extraction with the provided details.
 * 
 * @param string $token The authorization token for API access.
 * @param string $extractionId The ID of the extraction to update.
 * @param array $extractionDetails The new details of the extraction to update.
 * @return mixed The API response with the updated extraction details or an error message.
 */
function updateExtraction($token, $extractionId, $extractionDetails) {
    $url = 'https://api.extracta.ai/api/v1/updateExtraction';

    // Initialize cURL session
    $ch = curl_init($url);

    // Prepare the payload with both extractionId and extractionDetails
    $payload = json_encode([
        'extractionId' => $extractionId,
        'extractionDetails' => $extractionDetails
    ]);

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

    try {
        // Execute cURL session
        $response = curl_exec($ch);

        // Check for cURL errors
        if (curl_errno($ch)) {
            throw new Exception('Curl error: ' . curl_error($ch));
        }

        // For simplicity, returning the decoded response for now
        return $response;
    } catch (Exception $e) {
        // Handle exceptions or errors here
        return 'Error: ' . $e->getMessage();
    } finally {
        // Always close the cURL session
        curl_close($ch);
    }
}

// Example usage
$token = 'apiKey';
$extractionId = 'extractionId'; // Placeholder for the actual extraction ID
$extractionDetails = [
    "name" => "CV - English",
    "description" => "...",
    "language" => "English",
    "options" => [
        "hasTable" => false,
        "handwrittenTextRecognition" => true
    ],
    "fields" => [
        ["key" => "name", "description" => "the name of the person", "example" => "John"],
        ["key" => "email", "description" => "the email of the person", "example" => "john@email.com"]
    ]
];

try {
    $response = updateExtraction($token, $extractionId, $extractionDetails);
    echo $response;
} catch (Exception $e) {
    echo "Failed to update extraction: " . $e->getMessage();
}

?>
```

{% endtab %}
{% endtabs %}

## Responses

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

```json
{
    "status": "updated",
    "updatedAt": 1712547789609,
    "extractionId": "extractionId"
}
```

{% endtab %}

{% tab title="400" %}

```json
{
    "status": "error",
    "message": "Extraction does not exist",
    "extractionId": "extractionId"
}
```

{% endtab %}

{% tab title="500" %}

```json
{
    "status": "error",
    "message": "Could not update extraction",
    "extractionId": "extractionId"
}
```

{% endtab %}
{% endtabs %}
