Skip to main content

Upload Document

You can use the REST API to upload documents directly to DocumentPro. Once uploaded, documents can be sent to Workflow. This guide covers both regular uploads (up to 6MB) and large file uploads.

Regular Upload (Up to 6MB)

API Endpoint

POST https://api.documentpro.ai/v1/documents

Request

Headers

  • x-api-key: Your API key for authentication.

Body

The request body should be multipart/form-data with the following field:

  • file: The document file you want to upload.

Example Implementation

Using cURL

curl --location 'https://api.documentpro.ai/v1/documents' \
--header 'x-api-key: YOUR_API_KEY' \
--form 'file=@"/path/to/your/document.pdf"'

Using Python

import requests

url = "https://api.documentpro.ai/v1/documents"

headers = {
'x-api-key': 'YOUR_API_KEY'
}

files = {
'file': ('document.pdf', open('/path/to/your/document.pdf', 'rb'), 'application/pdf')
}

response = requests.post(url, headers=headers, files=files)

if response.status_code == 200:
result = response.json()
print(f"File uploaded successfully. Document ID: {result['document_id']}")
print(result)
else:
print('Failed to upload file')
print(response.text)

Large File Upload (Over 6MB)

For documents larger than 6MB, DocumentPro uses a multi-step upload process.

Step 1: Get Upload URL

First, you need to get a pre-signed URL for uploading the file.

API Endpoint

GET https://api.documentpro.ai/v1/documents/upload_url

Query Parameters

  • file_name (required): The name of the file you want to upload.

Headers

  • x-api-key (required): Your API key for authentication.

Example Using cURL

curl --location 'https://api.documentpro.ai/v1/documents/upload_url?file_name=large_document.pdf' \
--header 'x-api-key: YOUR_API_KEY'

Example Using Python

import requests

url = "https://api.documentpro.ai/v1/documents/upload_url"
params = {"file_name": "large_document.pdf"}
headers = {"x-api-key": "YOUR_API_KEY"}

response = requests.get(url, headers=headers, params=params)

if response.status_code == 200:
result = response.json()
upload_url = result['upload_url']
document_id = result['document_id']
print(f"Upload URL obtained. Document ID: {document_id}")
else:
print('Failed to get upload URL')
print(response.text)

Step 2: Upload File to Pre-signed URL

Use the upload_url from Step 1 to upload your file directly to our storage.

Example Using cURL

curl --location --request PUT 'UPLOAD_URL_FROM_STEP_1' \
--header 'Content-Type: application/pdf' \
--data '@/path/to/your/large_document.pdf'

Example Using JavaScript (Browser)

const file = document.getElementById('fileInput').files[0];
const uploadUrl = 'UPLOAD_URL_FROM_STEP_1';

fetch(uploadUrl, {
method: 'PUT',
body: file,
headers: {
'Content-Type': file.type
},
mode: 'cors'
})
.then(response => {
if (response.ok) {
console.log('File uploaded successfully');
} else {
console.error('Upload failed');
}
})
.catch(error => console.error('Error:', error));

Step 3: Confirm Upload

After successful upload, confirm it with DocumentPro.

API Endpoint

POST https://api.documentpro.ai/v1/documents

Headers

  • x-api-key (required): Your API key for authentication.
  • Content-Type: application/json

Body

{
"document_id": "DOCUMENT_ID_FROM_STEP_1",
"file_name": "large_document.pdf"
}

Example Using cURL

curl --location 'https://api.documentpro.ai/v1/documents' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
"document_id": "7c5389c6-2ac0-48c5-831a-dd86dae3ec98",
"file_name": "large_document.pdf"
}'

Example Using Python

import requests
import json

url = "https://api.documentpro.ai/v1/documents"
headers = {
'x-api-key': 'YOUR_API_KEY',
'Content-Type': 'application/json'
}
payload = json.dumps({
"document_id": "7c5389c6-2ac0-48c5-831a-dd86dae3ec98",
"file_name": "large_document.pdf"
})

response = requests.post(url, headers=headers, data=payload)

if response.status_code == 200:
print('Upload confirmed successfully')
print(response.json())
else:
print('Failed to confirm upload')
print(response.text)

Response

Successful Response (Status Code: 200)

{
"document_id": "0b13c9f2-5148-4ffb-bb7b-de03bb071ca8",
"user_id": "4449b25a-9bba-4de4-be67-c06233d2f305",
"source_name": "api",
"file_name": "file_name.pdf",
"file_extension": "pdf",
"num_pages": 8,
"meta_tags": {},
"parser_runs": [],
"created_at": "2024-07-25T14:16:44.540197",
"updated_at": "2024-07-25T14:16:44.540223"
}

Error Response (Status Codes: 400, 403, 500)

{
"success": false,
"error": "error_code",
"message": "descriptive error message"
}

Important Notes

  1. Regular uploads are limited to 6MB in size.
  2. For files larger than 6MB, use the multi-step upload process.
  3. Supported file formats include PDF, JPEG, PNG, and TIFF.
  4. The document_id in the response is crucial for subsequent operations, such as sending the document to a Workflow.
  5. Uploading a document does not automatically parse it. You need to send the document to a Workflow in a separate API call.
  6. For large file uploads:
    • The pre-signed URL from Step 1 is temporary and will expire after a short period.
    • Ensure you complete all steps in order and handle any errors that may occur during the process.
    • The Content-Type header in Step 2 should match the actual file type you're uploading.
    • For browser uploads, ensure CORS is properly configured on your end.

Next Steps

After successfully uploading a document:

  1. Send document to a Workflow on the uploaded document using its document_id.
  2. Retrieve workflow results once the parsing is complete.
  3. List your documents to see all uploaded documents in your account.