How to Generate 1,000 Documents in Minutes with DocForge Batch API
Learn how to automate high-volume document generation using the DocForge API in Python — invoices, reports, and contracts at scale without manual effort.

Generating one document manually takes minutes. Generating 1,000 documents manually takes days — and introduces inconsistency, formatting errors, and compliance risk. Whether you're a SaaS platform sending monthly invoices, an HR system generating offer letters, or a compliance team producing audit certificates, batch document generation is a critical capability that most teams build badly.
This guide shows you how to automate high-volume document generation using the DocForge API in Python — producing PDFs, DOCX files, and structured reports at scale, with consistent branding and zero manual effort.
The Problem with Manual Document Generation
Most teams start with a Word template, export to PDF, and repeat. This works for five documents. It breaks at fifty — and it's completely unworkable at five thousand.
Common failure modes:
- Format inconsistency: Different team members applying templates differently
- Data entry errors: Copy-paste mistakes in names, amounts, dates
- Version drift: Old templates still in circulation after updates
- Compliance gaps: Missing required clauses or disclosure language
- Time cost: Hours of work for what should be a seconds-long process
The solution is to decouple document structure (the template) from document data (the payload), and generate both programmatically.
DocForge API Overview
The DocForge API accepts a JSON payload containing a template identifier and a data object, and returns a completed document — PDF, DOCX, or HTML — in the response body or via a signed URL.
Key capabilities:
- Template management: Upload and version your templates once, reference by ID
- Data injection: Merge any structured JSON data into template variables
- Batch mode: Submit arrays of document requests in a single API call
- Format flexibility: Generate PDF, DOCX, or HTML output per document
- Conditional content: Show/hide sections based on data values
- Brand compliance: Fonts, colors, logos applied consistently from templates
Prerequisites
pip install requests python-dotenvSet your API key:
export RAPIDAPI_KEY="YOUR_API_KEY"Step 1: Single Document Generation
Start with a single document to validate your setup.
import requests
import json
import os
RAPIDAPI_KEY = os.environ["RAPIDAPI_KEY"]
DOCFORGE_HOST = "docforge-api.p.rapidapi.com"
def generate_document(template_id: str, data: dict, output_format: str = "pdf") -> bytes:
"""Generate a single document from a template and data payload."""
url = f"https://{DOCFORGE_HOST}/generate"
headers = {
"X-RapidAPI-Key": RAPIDAPI_KEY,
"X-RapidAPI-Host": DOCFORGE_HOST,
"Content-Type": "application/json"
}
payload = {
"template_id": template_id,
"data": data,
"output_format": output_format
}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return response.content
# Example: generate a single invoice
invoice_data = {
"invoice_number": "INV-2026-0042",
"issue_date": "2026-04-03",
"due_date": "2026-05-03",
"client_name": "Acme Corporation",
"client_address": "100 Main St, San Francisco, CA 94105",
"line_items": [
{"description": "API Access - Professional Plan", "qty": 1, "unit_price": 499.00},
{"description": "Overage Calls (2,400 calls)", "qty": 2400, "unit_price": 0.002}
],
"subtotal": 503.80,
"tax_rate": 0.085,
"tax_amount": 42.82,
"total": 546.62,
"currency": "USD"
}
pdf_bytes = generate_document("invoice-standard-v3", invoice_data)
with open("invoice_acme.pdf", "wb") as f:
f.write(pdf_bytes)
print(f"Generated invoice: {len(pdf_bytes) / 1024:.1f} KB")Step 2: Batch Document Generation
The DocForge batch endpoint accepts an array of requests and processes them in parallel server-side — dramatically faster than sequential API calls.
import time
from typing import List, Dict, Any
from pathlib import Path
def generate_batch(
template_id: str,
records: List[Dict[str, Any]],
output_format: str = "pdf",
output_dir: str = "./output"
) -> Dict[str, Any]:
"""Generate multiple documents in a single batch API call."""
url = f"https://{DOCFORGE_HOST}/generate/batch"
headers = {
"X-RapidAPI-Key": RAPIDAPI_KEY,
"X-RapidAPI-Host": DOCFORGE_HOST,
"Content-Type": "application/json"
}
# Build batch request — each record becomes one document
batch_requests = [
{
"request_id": f"doc_{i:04d}",
"template_id": template_id,
"data": record,
"output_format": output_format
}
for i, record in enumerate(records)
]
payload = {
"batch": batch_requests,
"delivery": "urls" # Return signed download URLs instead of raw bytes
}
start = time.time()
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
elapsed = time.time() - start
result = response.json()
print(f"Batch of {len(records)} documents completed in {elapsed:.1f}s")
print(f"Success: {result['summary']['succeeded']} | Failed: {result['summary']['failed']}")
return result
def download_batch_results(batch_result: dict, output_dir: str = "./output"):
"""Download all documents from signed URLs returned by the batch endpoint."""
Path(output_dir).mkdir(parents=True, exist_ok=True)
downloaded = 0
for item in batch_result["documents"]:
if item["status"] == "success":
request_id = item["request_id"]
download_url = item["download_url"]
extension = item["format"]
file_response = requests.get(download_url)
file_response.raise_for_status()
output_path = Path(output_dir) / f"{request_id}.{extension}"
output_path.write_bytes(file_response.content)
downloaded += 1
print(f"Downloaded {downloaded} documents to {output_dir}/")Step 3: Loading Data from a Source
In production, your document data will come from a database, CSV export, or API. Here's how to load from CSV:
import csv
from datetime import date, timedelta
def load_clients_from_csv(filepath: str) -> List[Dict[str, Any]]:
"""Load client billing data from a CSV file."""
clients = []
with open(filepath, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
# Transform CSV columns into document data format
amount = float(row["monthly_amount"])
tax = amount * 0.085
clients.append({
"invoice_number": f"INV-{date.today().strftime('%Y%m')}-{row['client_id']}",
"issue_date": date.today().isoformat(),
"due_date": (date.today() + timedelta(days=30)).isoformat(),
"client_name": row["company_name"],
"client_address": row["billing_address"],
"plan_name": row["plan"],
"subtotal": amount,
"tax_amount": tax,
"total": amount + tax,
"currency": "USD"
})
return clients
# Run the full batch pipeline
def run_monthly_invoice_batch(csv_path: str, template_id: str):
"""Generate all monthly invoices from a client CSV in one API call."""
print(f"Loading client data from {csv_path}...")
client_data = load_clients_from_csv(csv_path)
print(f"Loaded {len(client_data)} clients.")
print("Submitting batch to DocForge API...")
result = generate_batch(
template_id=template_id,
records=client_data,
output_format="pdf",
output_dir="./invoices/2026-04"
)
download_batch_results(result, output_dir="./invoices/2026-04")
# Report failures for follow-up
failures = [d for d in result["documents"] if d["status"] == "failed"]
if failures:
print(f"\nFailed documents ({len(failures)}):")
for f in failures:
print(f" {f['request_id']}: {f.get('error', 'Unknown error')}")
return resultStep 4: Production Patterns
Chunking Large Batches
The DocForge batch endpoint handles up to 500 documents per call. For larger volumes, chunk your data:
def chunked_batch(records: List[dict], template_id: str, chunk_size: int = 500):
"""Process records in chunks to stay within API limits."""
all_results = []
for i in range(0, len(records), chunk_size):
chunk = records[i:i + chunk_size]
print(f"Processing chunk {i // chunk_size + 1}: records {i+1}–{i+len(chunk)}")
result = generate_batch(template_id, chunk)
all_results.append(result)
total_success = sum(r["summary"]["succeeded"] for r in all_results)
total_failed = sum(r["summary"]["failed"] for r in all_results)
print(f"\nTotal: {total_success} succeeded, {total_failed} failed")
return all_resultsScheduling Monthly Runs
Integrate with a scheduler (cron, APScheduler, Celery) to run automatically:
from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler()
@scheduler.scheduled_job("cron", day=1, hour=6) # 1st of each month at 6 AM
def monthly_invoices():
print("Running monthly invoice batch...")
run_monthly_invoice_batch(
csv_path="/data/active-clients.csv",
template_id="invoice-standard-v3"
)
scheduler.start()Performance Benchmarks
Based on typical API usage patterns:
| Volume | Sequential API Calls | DocForge Batch API |
|---|---|---|
| 10 docs | ~15 seconds | ~2 seconds |
| 100 docs | ~150 seconds | ~8 seconds |
| 500 docs | ~12 minutes | ~30 seconds |
| 1,000 docs | ~25 minutes | ~55 seconds |
Batch mode achieves 10–20x throughput improvement over sequential calls because DocForge parallelizes rendering server-side.
Real-World ROI
A mid-sized SaaS company with 800 monthly customers that previously spent 3 hours manually generating and emailing invoices can:
- Reduce invoice generation time from 3 hours to under 2 minutes
- Eliminate formatting inconsistencies across documents
- Ensure every invoice uses the latest approved template
- Free one full workday per month for higher-value work
At $35/hour labor cost, that's $2,100/month saved — before counting error reduction and compliance benefits.
Next Steps
- Set up your DocForge account at apivult.com and get your API key
- Upload your template — Word DOCX files or structured JSON templates are both supported
- Test with 5–10 records before scaling to production volumes
- Schedule recurring runs using APScheduler or your existing job system
Batch document generation is one of those infrastructure investments that pays for itself the first month. The DocForge API makes it a 30-minute integration, not a multi-week engineering project.
More Articles
Generate Professional PDF Documents from Templates Using an API
Stop writing PDF generation code. Learn how to use the DocForge API to generate invoices, reports, contracts, and certificates from templates in seconds.
March 30, 2026
How to Automate Customer Onboarding Documents with DocForge API
Learn how to generate personalized onboarding PDFs, welcome packets, and account setup documents programmatically using DocForge API in Python.
April 2, 2026