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

# Extract Attachment

> Extracts attachments from a PDF file.

## `POST /v1/pdf/attachments/extract`

## Attributes

<Note>Attributes are case-sensitive and should be inside JSON for POST request. for example: `{ "url": "https://example.com/file1.pdf" }`</Note>

| Attribute                     | Type    | Required | Default | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| ----------------------------- | ------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `url`                         | string  | *Yes*    | -       | URL to the source file [`url` attribute](/api-reference/url-input-and-request-limits#supported-file-sources)                                                                                                                                                                                                                                                                                                                                                                            |
| `callback`                    | string  | *No*     | -       | The callback URL (or Webhook) used to receive the POST data. see [Webhooks & Callbacks](/api-reference/webhooks). This is only applicable when `async` is set to `true`.                                                                                                                                                                                                                                                                                                                |
| `httpusername`                | string  | *No*     | -       | HTTP auth user name if required to access source URL.                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `httppassword`                | string  | *No*     | -       | HTTP auth password if required to access source URL.                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `inline`                      | boolean | *No*     | `false` | Set to true to return results inside the response. Otherwise, the endpoint will return a URL to the output file generated.                                                                                                                                                                                                                                                                                                                                                              |
| `expiration`                  | integer | *No*     | `60`    | Set the expiration time for the output link in minutes. After this specified duration, any generated output file(s) will be automatically deleted from [PDF.co Temporary Files Storage](/api-reference/file-upload/overview). The maximum duration for link expiration varies based on your current subscription plan. To store permanent input files (e.g. re-usable images, pdf templates, documents) consider using [PDF.co Built-In Files Storage](https://app.pdf.co/tools/files). |
| `password`                    | string  | *No*     | -       | Password for the PDF file.                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `async`                       | boolean | *No*     | `false` | Set `async` to `true` for long processes to run in the background, API will then return a `jobId` which you can use with the [Background Job Check endpoint](/api-reference/job-check). Also see [Webhooks & Callbacks](/api-reference/webhooks)                                                                                                                                                                                                                                        |
| `profiles`                    | object  | *No*     | -       | See [Profiles](/api-reference/profiles) for more information.                                                                                                                                                                                                                                                                                                                                                                                                                           |
|     `outputDataFormat`        | string  | *No*     | -       | If you require your output as `base64` format, set this to `base64`                                                                                                                                                                                                                                                                                                                                                                                                                     |
|     `DataEncryptionAlgorithm` | string  | *No*     | -       | Controls the encryption algorithm used for data encryption. See [User-Controlled Encryption](/knowledgebase/user-controlled-encryption) for more information. The available algorithms are: `AES128`, `AES192`, `AES256`.                                                                                                                                                                                                                                                               |
|     `DataEncryptionKey`       | string  | *No*     | -       | Controls the encryption key used for data encryption. See [User-Controlled Encryption](/knowledgebase/user-controlled-encryption) for more information.                                                                                                                                                                                                                                                                                                                                 |
|     `DataEncryptionIV`        | string  | *No*     | -       | Controls the encryption IV used for data encryption. See [User-Controlled Encryption](/knowledgebase/user-controlled-encryption) for more information.                                                                                                                                                                                                                                                                                                                                  |
|     `DataDecryptionAlgorithm` | string  | *No*     | -       | Controls the decryption algorithm used for data decryption. See [User-Controlled Encryption](/knowledgebase/user-controlled-encryption) for more information. The available algorithms are: `AES128`, `AES192`, `AES256`.                                                                                                                                                                                                                                                               |
|     `DataDecryptionKey`       | string  | *No*     | -       | Controls the decryption key used for data decryption. See [User-Controlled Encryption](/knowledgebase/user-controlled-encryption) for more information.                                                                                                                                                                                                                                                                                                                                 |
|     `DataDecryptionIV`        | string  | *No*     | -       | Controls the decryption IV used for data decryption. See [User-Controlled Encryption](/knowledgebase/user-controlled-encryption) for more information.                                                                                                                                                                                                                                                                                                                                  |

## Query parameters

*No query parameters accepted.*

## Responses

| Parameter          | Type           | Description                                                                                                                  |
| ------------------ | -------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `urls`             | array\[string] | List of URLs to the final PDF file stored in S3.                                                                             |
| `error`            | boolean        | Indicates whether an error occurred (`false` means success)                                                                  |
| `pageCount`        | integer        | Number of pages in the PDF document.                                                                                         |
| `status`           | string         | Status code of the request (200, 404, 500, etc.). For more information, see [Response Codes](/api-reference/response-codes). |
| `name`             | string         | Name of the output file                                                                                                      |
| `remainingCredits` | integer        | Number of credits remaining in the account                                                                                   |
| `duration`         | integer        | Time taken for the operation in milliseconds                                                                                 |

## `Example` Payload

<Note>To see the request size limits, please refer to the [Request Size Limits](/api-reference/url-input-and-request-limits#pdf-co-request-size).</Note>

```json theme={null}
{
  "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-attachments/attachments.pdf",
  "inline": false,
  "async": false
}
```

## `Example` Response

<Note>To see the main response codes, please refer to the [Response Codes](/api-reference/response-codes) page.</Note>

```json theme={null}
{
  "urls": [
    "https://pdf-temp-files.s3.amazonaws.com/DO1TAIHEZR5P9QLI7ICYM9DI0AAH57HY/sample.png",
    "https://pdf-temp-files.s3.amazonaws.com/EOINIMD7X48JSOB1G8ETLVPOFZLM1NJ2/SampleMetafile.emf",
    "https://pdf-temp-files.s3.amazonaws.com/3LW4BXNSPAE0WQTG5DPMXX498OCPNU4Q/ab.tif"
  ],
  "pageCount": 3,
  "error": false,
  "status": 200,
  "name": "attachments.json",
  "credits": 24,
  "duration": 1211,
  "remainingCredits": 98003902
}
```

<Note>
  **Inconsistent URL Encoding in cURL Output:** When using cURL to make API requests, the output JSON may show URL characters encoded as Unicode escape sequences. For example, the ampersand character (`&`) may appear as `\u0026` in the cURL output. This is normal JSON encoding behavior and does not affect the validity of the URL. The URL will function correctly when used, as JSON parsers automatically decode these escape sequences. If you're parsing the response programmatically, your JSON parser will handle this conversion automatically.
</Note>

## Code Samples

<Tabs>
  <Tab title="CURL">
    ```bash theme={null}
    curl --location --request POST 'https://api.pdf.co/v1/pdf/attachments/extract' \
    --header 'Content-Type: application/json' \
    --header 'x-api-key: *******************' \
    --data-raw '{
    "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-attachments/attachments.pdf",
    "inline": false,
    "async": false
    }'
    ```
  </Tab>

  <Tab title="JavaScript/Node.js">
    ```javascript theme={null}
    var https = require("https");
    var fs = require("fs");
    var path = require("path");

    // The authentication key (API Key).
    // Get your own by registering at https://app.pdf.co
    const API_KEY = "***********************************";


    // Source PDF file
    // You can also upload your own file into PDF.co and use it as url. Check "Upload File" samples for code snippets: https://github.com/bytescout/pdf-co-api-samples/tree/master/File%20Upload/    
    const SourceFileUrl = "https://bytescout-com.s3.us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-attachments/attachments.pdf";


    // Prepare request for API endpoint
    var queryPath = `/v1/pdf/attachments/extract`;

    // JSON payload for api request
    var jsonPayload = JSON.stringify({
        url: SourceFileUrl
    });

    var reqOptions = {
        host: "api.pdf.co",
        method: "POST",
        path: queryPath,
        headers: {
            "x-api-key": API_KEY,
            "Content-Type": "application/json",
            "Content-Length": Buffer.byteLength(jsonPayload, 'utf8')
        }
    };
    // Send request
    var postRequest = https.request(reqOptions, (response) => {

      let responseData = '';

      response.setEncoding("utf8");

      response.on("data", (chunk) => {
          responseData += chunk;
      });

      response.on("end", () => {
          // Parse JSON response
          var data = JSON.parse(responseData);
          if (data.error == false) {
              // Download extracted files
              data.urls.forEach((url) => {
                  var localFileName = path.basename(url);
                  var file = fs.createWriteStream(localFileName);
                  https.get(url, (response2) => {
                      response2.pipe(file)
                          .on("close", () => {
                              console.log(`Generated file saved as "${localFileName}" file.`);
                          });
                  });
              }, this);
          }
          else {
              // Service reported error
              console.log(data.message);
          }
      });
    }).on("error", (e) => {
      // Request error
      console.error(e);
    });

    // Write request data
    postRequest.write(jsonPayload);
    postRequest.end();
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    import requests
    import json

    url = "https://api.pdf.co/v1/pdf/attachments/extract"

    payload = json.dumps({
      "url": "https://bytescout-com.s3.us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-attachments/attachments.pdf",
      "inline": True,
      "async": False
    })
    headers = {
        'Content-Type': 'application/json',
        'x-api-key': '__Replace_With_Your_PDFco_API_Key__'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)
    ```
  </Tab>

  <Tab title="C#">
    ```csharp theme={null}
    using System;
    using RestSharp;
    namespace HelloWorldApplication {
        class HelloWorld {
            static void Main(string[] args) {
                var client = new RestClient("https://api.pdf.co/v1/pdf/attachments/extract");
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                request.AddHeader("x-api-key", "__Replace_With_Your_PDFco_API_Key__");
                var body = @"{" + "\n" +
                @"    ""url"": ""https://bytescout-com.s3.us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-attachments/attachments.pdf""," + "\n" +
                @"    ""inline"": true," + "\n" +
                @"    ""async"": false" + "\n" +
                @"}";
                request.AddParameter("application/json", body,  ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                Console.WriteLine(response.Content);
            }
        }
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    import java.io.*;
    import okhttp3.*;
    public class main {
        public static void main(String []args) throws IOException{
            OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, "{\n    \"url\": \"https://bytescout-com.s3.us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-attachments/attachments.pdf\",\n    \"inline\": true,\n    \"async\": false\n}");
            Request request = new Request.Builder()
                .url("https://api.pdf.co/v1/pdf/attachments/extract")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("x-api-key", "__Replace_With_Your_PDFco_API_Key__")
                .build();
            Response response = client.newCall(request).execute();
            System.out.println(response.body().string());
        }
    }
    ```
  </Tab>

  <Tab title="PHP">
    ```php theme={null}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Cloud API asynchronous "Extract PDF Attachment" job example (allows to avoid timeout errors).</title>
    </head>
    <body>

    <?php 

    // Cloud API asynchronous "Extract PDF Attachment" job example.
    // Allows to avoid timeout errors when processing huge or scanned PDF documents.


    // The authentication key (API Key).
    // Get your own by registering at https://app.pdf.co
    $apiKey = "********************************";

    // Direct URL of source PDF file. Check another example if you need to upload a local file to the cloud.
    // You can also upload your own file into PDF.co and use it as url. Check "Upload File" samples for code snippets: https://github.com/bytescout/pdf-co-api-samples/tree/master/File%20Upload/    
    $sourceFileUrl = "https://bytescout-com.s3.us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-attachments/attachments.pdf";

    // Prepare URL for `Extract PDF Attachment` API call
    $url = "https://api.pdf.co/v1/pdf/attachments/extract";

    // Prepare requests params
    $parameters = array();
    $parameters["url"] = $sourceFileUrl;
    $parameters["async"] = true; // (!) Make asynchronous job

    // Create Json payload
    $data = json_encode($parameters);

    // Create request
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("x-api-key: " . $apiKey, "Content-type: application/json"));
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

    // Execute request
    $result = curl_exec($curl);

    if (curl_errno($curl) == 0)
    {
        $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        
        if ($status_code == 200)
        {
            $json = json_decode($result, true);
            
            if (!isset($json["error"]) || $json["error"] == false)
            {
                // URL of generated JSON file available after the job completion; it will contain URLs of result PDF files.
                $resultFileUrl = $json["url"];
                // Asynchronous job ID
                $jobId = $json["jobId"];
                
                // Check the job status in a loop
                do
                {
                    $status = CheckJobStatus($jobId, $apiKey); // Possible statuses: "working", "failed", "aborted", "success".
                    
                    // Display timestamp and status (for demo purposes)
                    echo "<p>" . date(DATE_RFC2822) . ": " . $status . "</p>";
                    
                    if ($status == "success")
                    {
                        // Display link to the file with conversion results
                        echo "<div><h2>Results:</h2><a href='" . $resultFileUrl . "' target='_blank'>" . $resultFileUrl . "</a></div>";
                        break;
                    }
                    else if ($status == "working")
                    {
                        // Pause for a few seconds
                        sleep(3);
                    }
                    else 
                    {
                        echo $status . "<br/>";
                        break;
                    }
                }
                while (true);
            }
            else
            {
                // Display service reported error
                echo "<p>Error: " . $json["message"] . "</p>"; 
            }
        }
        else
        {
            // Display request error
            echo "<p>Status code: " . $status_code . "</p>"; 
            echo "<p>" . $result . "</p>"; 
        }
    }
    else
    {
        // Display CURL error
        echo "Error: " . curl_error($curl);
    }

    // Cleanup
    curl_close($curl);


    function CheckJobStatus($jobId, $apiKey)
    {
        $status = null;
        
      // Create URL
        $url = "https://api.pdf.co/v1/job/check";
        
        // Prepare requests params
        $parameters = array();
        $parameters["jobid"] = $jobId;

        // Create Json payload
        $data = json_encode($parameters);

        // Create request
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("x-api-key: " . $apiKey, "Content-type: application/json"));
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        
        // Execute request
        $result = curl_exec($curl);
        
        if (curl_errno($curl) == 0)
        {
            $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            
            if ($status_code == 200)
            {
                $json = json_decode($result, true);
            
                if (!isset($json["error"]) || $json["error"] == false)
                {
                    $status = $json["status"];
                }
                else
                {
                    // Display service reported error
                    echo "<p>Error: " . $json["message"] . "</p>"; 
                }
            }
            else
            {
                // Display request error
                echo "<p>Status code: " . $status_code . "</p>"; 
                echo "<p>" . $result . "</p>"; 
            }
        }
        else
        {
            // Display CURL error
            echo "Error: " . curl_error($curl);
        }
        
        // Cleanup
        curl_close($curl);
        
        return $status;
    }

    ?>

    </body>
    </html>
    ```
  </Tab>
</Tabs>
