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

# Replace Document Content

> Replace document content and optionally re-index

> Replace document content and optionally re-index

<CodeGroup>
  ```bash cURL theme={null}
  curl --request PUT \
    --url https://api.wistx.ai/v1/indexing/documents/res_abc123/content \
    --header 'Authorization: Bearer YOUR_API_KEY' \
    --form 'file=@/path/to/new-document.pdf' \
    --form 're_index=true'
  ```

  ```python Python theme={null}
  import requests

  api_key = "YOUR_API_KEY"
  resource_id = "res_abc123"

  with open("new-document.pdf", "rb") as f:
      response = requests.put(
          f"https://api.wistx.ai/v1/indexing/documents/{resource_id}/content",
          headers={"Authorization": f"Bearer {api_key}"},
          files={"file": f},
          data={"re_index": "true"}
      )

  resource = response.json()
  print(f"Content replaced: {resource['status']}")
  ```

  ```javascript JavaScript theme={null}
  const apiKey = "YOUR_API_KEY";
  const resourceId = "res_abc123";

  const formData = new FormData();
  formData.append("file", fileInput.files[0]);
  formData.append("re_index", "true");

  const response = await fetch(
    `https://api.wistx.ai/v1/indexing/documents/${resourceId}/content`,
    {
      method: "PUT",
      headers: {
        Authorization: `Bearer ${apiKey}`
      },
      body: formData
    }
  );

  const resource = await response.json();
  console.log(`Content replaced: ${resource.status}`);
  ```

  ```go Go theme={null}
  package main

  import (
      "bytes"
      "fmt"
      "io"
      "mime/multipart"
      "net/http"
      "os"
  )

  func main() {
      apiKey := "YOUR_API_KEY"
      resourceID := "res_abc123"
      url := fmt.Sprintf("https://api.wistx.ai/v1/indexing/documents/%s/content", resourceID)

      file, _ := os.Open("new-document.pdf")
      defer file.Close()

      body := &bytes.Buffer{}
      writer := multipart.NewWriter(body)
      part, _ := writer.CreateFormFile("file", "new-document.pdf")
      io.Copy(part, file)
      writer.WriteField("re_index", "true")
      writer.Close()

      req, _ := http.NewRequest("PUT", url, body)
      req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
      req.Header.Set("Content-Type", writer.FormDataContentType())

      client := &http.Client{}
      resp, _ := client.Do(req)
      defer resp.Body.Close()

      var resource map[string]interface{}
      json.NewDecoder(resp.Body).Decode(&resource)
      fmt.Printf("Content replaced: %v\n", resource["status"])
  }
  ```

  ```ruby Ruby theme={null}
  require 'net/http'
  require 'json'
  require 'uri'

  api_key = "YOUR_API_KEY"
  resource_id = "res_abc123"
  url = URI("https://api.wistx.ai/v1/indexing/documents/#{resource_id}/content")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  request = Net::HTTP::Put.new(url)
  request["Authorization"] = "Bearer #{api_key}"

  form_data = [
    ['file', File.open('new-document.pdf')],
    ['re_index', 'true']
  ]
  request.set_form(form_data, 'multipart/form-data')

  response = http.request(request)
  resource = JSON.parse(response.body)
  puts "Content replaced: #{resource['status']}"
  ```

  ```php PHP theme={null}
  <?php

  $apiKey = "YOUR_API_KEY";
  $resourceId = "res_abc123";
  $url = "https://api.wistx.ai/v1/indexing/documents/" . $resourceId . "/content";

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  curl_setopt($ch, CURLOPT_HTTPHEADER, [
      "Authorization: Bearer " . $apiKey
  ]);

  $file = new CURLFile('new-document.pdf', 'application/pdf', 'new-document.pdf');
  curl_setopt($ch, CURLOPT_POSTFIELDS, [
      'file' => $file,
      're_index' => 'true'
  ]);

  $response = curl_exec($ch);
  curl_close($ch);

  $resource = json_decode($response, true);
  echo "Content replaced: " . $resource['status'];
  ```

  ```java Java theme={null}
  import java.io.*;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.nio.file.Files;
  import java.nio.file.Paths;

  public class ReplaceDocumentContent {
      public static void main(String[] args) throws Exception {
          String apiKey = "YOUR_API_KEY";
          String resourceId = "res_abc123";
          URL url = new URL("https://api.wistx.ai/v1/indexing/documents/" + resourceId + "/content");
          
          String boundary = "----WebKitFormBoundary" + System.currentTimeMillis();
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("PUT");
          conn.setRequestProperty("Authorization", "Bearer " + apiKey);
          conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
          conn.setDoOutput(true);
          
          try (OutputStream os = conn.getOutputStream();
               PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF-8"), true)) {
              
              writer.append("--" + boundary).append("\r\n");
              writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"new-document.pdf\"").append("\r\n");
              writer.append("Content-Type: application/pdf").append("\r\n");
              writer.append("\r\n").flush();
              
              Files.copy(Paths.get("new-document.pdf"), os);
              os.flush();
              
              writer.append("\r\n").flush();
              writer.append("--" + boundary).append("\r\n");
              writer.append("Content-Disposition: form-data; name=\"re_index\"").append("\r\n");
              writer.append("\r\n");
              writer.append("true").append("\r\n").flush();
              
              writer.append("--" + boundary + "--").append("\r\n").flush();
          }
          
          BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
          StringBuilder response = new StringBuilder();
          String responseLine;
          while ((responseLine = br.readLine()) != null) {
              response.append(responseLine.trim());
          }
          System.out.println(response.toString());
      }
  }
  ```
</CodeGroup>

## Path Parameters

<ParamField path="resource_id" type="string" required>
  Resource ID of the document to replace.
</ParamField>

## Request Parameters

<ParamField body="file" type="file">
  New file to upload. Either `file` or `document_url` is required.
</ParamField>

<ParamField body="document_url" type="string">
  New document URL (http/https). Either `file` or `document_url` is required.
</ParamField>

<ParamField body="re_index" type="boolean">
  Whether to re-index after replacement.

  **Default:** `true`
</ParamField>

## Response

<ResponseExample>
  ```json 200 Success theme={null}
  {
    "resource_id": "res_abc123",
    "resource_type": "document",
    "status": "indexing",
    "progress": {
      "percentage": 0,
      "stage": "queued",
      "message": "Document re-indexing queued"
    },
    "updated_at": "2024-01-15T10:30:00Z"
  }
  ```

  ```json 400 Bad Request theme={null}
  {
    "error": {
      "code": "VALIDATION_ERROR",
      "message": "Either 'file' or 'document_url' must be provided",
      "details": null
    },
    "metadata": {
      "request_id": "req_abc123",
      "timestamp": 1704067200.0
    }
  }
  ```

  ```json 400 Bad Request theme={null}
  {
    "error": {
      "code": "VALIDATION_ERROR",
      "message": "Cannot provide both 'file' and 'document_url'",
      "details": null
    },
    "metadata": {
      "request_id": "req_abc123",
      "timestamp": 1704067200.0
    }
  }
  ```

  ```json 401 Unauthorized theme={null}
  {
    "detail": "User ID not found"
  }
  ```

  ```json 404 Not Found theme={null}
  {
    "error": {
      "code": "FILE_NOT_FOUND",
      "message": "File not found: /path/to/document.pdf",
      "details": null
    },
    "metadata": {
      "request_id": "req_abc123",
      "timestamp": 1704067200.0
    }
  }
  ```

  ```json 404 Not Found theme={null}
  {
    "detail": "Resource not found or access denied"
  }
  ```

  ```json 500 Internal Server Error theme={null}
  {
    "error": {
      "code": "REPLACE_ERROR",
      "message": "Failed to replace document content",
      "details": null
    },
    "metadata": {
      "request_id": "req_abc123",
      "timestamp": 1704067200.0
    }
  }
  ```
</ResponseExample>

## Related Endpoints

* [Update Document Metadata](/api-reference/indexing/update-document-metadata) - Update metadata only
* [Re-index Document](/api-reference/indexing/reindex-document) - Re-index existing document
* [Get Resource Status](/api-reference/indexing/resource-status) - Check resource status


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.