Skip to main content

Overview

The /api/upload-pdf endpoint provides three methods for managing PDF documents in Amazon S3:
  • POST: Upload a PDF document
  • GET: List all PDF documents in a bucket
  • DELETE: Delete a specific PDF document

POST /api/upload-pdf

Upload a PDF document to an S3 bucket.

Request

The request must be sent as multipart/form-data with the following fields:
file
File
required
The PDF file to upload (must have application/pdf content type)
region
string
required
AWS region where your S3 bucket is located (e.g., us-east-1)
bucketName
string
required
The name of your S3 bucket
accessKeyId
string
required
AWS Access Key ID for authentication
secretAccessKey
string
required
AWS Secret Access Key for authentication
sessionToken
string
Optional AWS Session Token for temporary credentials
prefix
string
S3 key prefix for organizing documents (default: documentos)

Response

Success (200 OK)

ok
boolean
Always true on successful upload
bucketName
string
The S3 bucket where the file was uploaded
key
string
The full S3 key of the uploaded file (includes prefix and timestamp)
{
  "ok": true,
  "bucketName": "my-documents-bucket",
  "key": "documentos/1709683200000-my_document.pdf"
}

File Key Format

Uploaded files are stored with the following key format:
{prefix}/{timestamp}-{sanitized-filename}
The filename is sanitized to replace non-alphanumeric characters (except ., _, -) with underscores:
const sanitizeFileName = (name: string): string => 
  name.replace(/[^a-zA-Z0-9._-]/g, '_');

const key = `${prefix.replace(/\/$/, '')}/${Date.now()}-${sanitizeFileName(file.name)}`;

Error Responses

400 Bad Request - Invalid File Type

{
  "error": "El archivo debe ser PDF."
}

400 Bad Request - Missing File

{
  "error": "Debe seleccionar un archivo PDF."
}

400 Bad Request - Missing Parameters

{
  "error": "Faltan parámetros de S3."
}

500 Internal Server Error

{
  "error": "Error message from AWS or internal error"
}

cURL Example

curl -X POST https://your-domain.com/api/upload-pdf \
  -F "[email protected]" \
  -F "region=us-east-1" \
  -F "bucketName=my-documents-bucket" \
  -F "accessKeyId=AKIAIOSFODNN7EXAMPLE" \
  -F "secretAccessKey=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" \
  -F "prefix=documentos"

JavaScript Example

const formData = new FormData();
formData.append('file', pdfFile);
formData.append('region', 'us-east-1');
formData.append('bucketName', 'my-documents-bucket');
formData.append('accessKeyId', 'AKIAIOSFODNN7EXAMPLE');
formData.append('secretAccessKey', 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY');
formData.append('prefix', 'documentos');

const response = await fetch('/api/upload-pdf', {
  method: 'POST',
  body: formData
});

const data = await response.json();
console.log(data.key);

GET /api/upload-pdf

List all PDF documents in an S3 bucket with a specific prefix.

Request Parameters

Query string parameters:
region
string
required
AWS region where your S3 bucket is located
bucketName
string
required
The name of your S3 bucket
accessKeyId
string
required
AWS Access Key ID for authentication
secretAccessKey
string
required
AWS Secret Access Key for authentication
sessionToken
string
Optional AWS Session Token for temporary credentials
prefix
string
S3 key prefix to filter documents (default: documentos)

Response

Success (200 OK)

documents
array
Array of document objects
{
  "documents": [
    {
      "key": "documentos/1709683200000-report.pdf",
      "size": 245678,
      "lastModified": "2024-03-05T12:00:00.000Z"
    },
    {
      "key": "documentos/1709683400000-summary.pdf",
      "size": 89012,
      "lastModified": "2024-03-05T12:03:20.000Z"
    }
  ]
}

Error Responses

400 Bad Request

{
  "error": "Faltan parámetros de S3 para listar documentos."
}

500 Internal Server Error

{
  "error": "Error message from AWS or internal error"
}

cURL Example

curl -X GET "https://your-domain.com/api/upload-pdf?region=us-east-1&bucketName=my-documents-bucket&accessKeyId=AKIAIOSFODNN7EXAMPLE&secretAccessKey=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY&prefix=documentos"

JavaScript Example

const params = new URLSearchParams({
  region: 'us-east-1',
  bucketName: 'my-documents-bucket',
  accessKeyId: 'AKIAIOSFODNN7EXAMPLE',
  secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
  prefix: 'documentos'
});

const response = await fetch(`/api/upload-pdf?${params}`);
const data = await response.json();

console.log(`Found ${data.documents.length} documents`);
data.documents.forEach(doc => {
  console.log(`${doc.key} - ${doc.size} bytes`);
});

DELETE /api/upload-pdf

Delete a specific PDF document from an S3 bucket.

Request Body

The request must be sent as JSON:
region
string
required
AWS region where your S3 bucket is located
bucketName
string
required
The name of your S3 bucket
key
string
required
The full S3 key of the document to delete
accessKeyId
string
required
AWS Access Key ID for authentication
secretAccessKey
string
required
AWS Secret Access Key for authentication
sessionToken
string
Optional AWS Session Token for temporary credentials

Response

Success (200 OK)

ok
boolean
Always true on successful deletion
key
string
The S3 key of the deleted document
{
  "ok": true,
  "key": "documentos/1709683200000-report.pdf"
}

Error Responses

400 Bad Request

{
  "error": "Faltan parámetros de S3 para eliminar el documento."
}

500 Internal Server Error

{
  "error": "Error message from AWS or internal error"
}

cURL Example

curl -X DELETE https://your-domain.com/api/upload-pdf \
  -H "Content-Type: application/json" \
  -d '{
    "region": "us-east-1",
    "bucketName": "my-documents-bucket",
    "key": "documentos/1709683200000-report.pdf",
    "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
    "secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
  }'

JavaScript Example

const response = await fetch('/api/upload-pdf', {
  method: 'DELETE',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    region: 'us-east-1',
    bucketName: 'my-documents-bucket',
    key: 'documentos/1709683200000-report.pdf',
    accessKeyId: 'AKIAIOSFODNN7EXAMPLE',
    secretAccessKey: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
  })
});

const data = await response.json();
console.log(`Deleted: ${data.key}`);

Document Management

Learn about the document management interface

S3 Setup

Configure your S3 bucket settings

Build docs developers (and LLMs) love