docapi-sdk
Official Python SDK for DocAPI — generate PDFs and screenshots from HTML.
pip install docapi-sdk
Zero dependencies. Uses Python's built-in urllib (Python 3.8+).
Quick start
from docapi import DocAPI
client = DocAPI("pk_live_...")
# Generate a PDF
pdf = client.pdf("<h1>Hello World</h1>")
with open("output.pdf", "wb") as f:
f.write(pdf)
# Screenshot a URL
img = client.screenshot(url="https://example.com")
with open("screenshot.png", "wb") as f:
f.write(img)
Get a free API key at docapi.co/signup — 100 calls/month, no credit card.
API
DocAPI(api_key)
client = DocAPI("pk_live_...")
client.pdf(html, **options)
Convert HTML to a PDF. Returns bytes.
pdf = client.pdf(
"""
<html>
<head>
<style>body { font-family: sans-serif; padding: 40px; }</style>
</head>
<body>
<h1>Invoice #123</h1>
<p>Amount due: $4,500.00</p>
</body>
</html>
""",
format="A4",
margin={"top": "0.5in", "bottom": "0.5in", "left": "0.5in", "right": "0.5in"},
)
with open("invoice.pdf", "wb") as f:
f.write(pdf)
Options:
| Option | Type | Default | Description |
|---|---|---|---|
format |
str | "A4" |
"A4", "Letter", "Legal", "Tabloid" |
landscape |
bool | False |
Landscape orientation |
print_background |
bool | True |
Include background colours/images |
margin |
dict | — | {"top": "0.5in", "bottom": "0.5in", ...} |
scale |
float | 1 |
Scale factor 0.1–2 |
page_ranges |
str | — | Pages to print e.g. "1-5, 8" |
client.screenshot(*, url=None, html=None, **options)
Screenshot a URL or render HTML. Returns bytes. Provide url OR html, not both.
# From a URL (e.g. OG image)
img = client.screenshot(url="https://mysite.com/blog/post", width=1200, height=630)
# From HTML
img = client.screenshot(
html="""
<div style="width:1200px;height:630px;background:#0f172a;
display:flex;align-items:center;padding:80px">
<h1 style="color:white;font-size:64px">My Blog Post</h1>
</div>
""",
width=1200,
height=630,
)
with open("og.png", "wb") as f:
f.write(img)
Options:
| Option | Type | Default | Description |
|---|---|---|---|
url |
str | — | URL to screenshot |
html |
str | — | HTML to render |
width |
int | 1200 |
Viewport width |
height |
int | 630 |
Viewport height |
format |
str | "png" |
"png" or "jpeg" |
client.credits()
Check remaining credits and USDC top-up address. Agent accounts only.
info = client.credits()
print(f"{info['credits']} credits remaining")
print(f"Send USDC to {info['usdc_address']}")
client.credits_remaining
After any pdf() or screenshot() call, client.credits_remaining holds the value from the X-Credits-Remaining response header. Use this to trigger proactive USDC top-ups.
pdf = client.pdf(html)
if (client.credits_remaining or 999) < 50:
# top up via USDC
pass
DocAPI.register(notify_email=None)
Register a new agent account programmatically. No API key required.
account = DocAPI.register(notify_email="ops@yourcompany.com")
print(account["api_key"]) # "pk_..."
print(account["usdc_address"]) # "0x..."
print(account["free_calls"]) # 10
Error handling
All errors raise DocAPIError with status and code attributes.
from docapi import DocAPI, DocAPIError
try:
pdf = client.pdf("<h1>Hello</h1>")
except DocAPIError as e:
print(e.status) # 401, 402, 429, 500
print(e.code) # "invalid_api_key", "credits_exhausted", etc.
print(e.message)
| Status | Code | Meaning |
|---|---|---|
| 401 | invalid_api_key |
Invalid or missing API key |
| 402 | credits_exhausted |
Agent account out of credits — send USDC |
| 429 | usage_limit_exceeded |
Monthly plan limit reached |
| 500 | generation_failed |
Rendering error |
Self-managing credits (agent pattern)
from docapi import DocAPI, DocAPIError
API_KEY = "pk_..."
USDC_ADDRESS = "0x..." # from DocAPI.register()
THRESHOLD = 50
TOPUP_USDC = 10
client = DocAPI(API_KEY)
def generate_pdf(html: str) -> bytes:
pdf = client.pdf(html)
if (client.credits_remaining or 999) < THRESHOLD:
# send USDC via Coinbase AgentKit or your wallet
# send_usdc(to=USDC_ADDRESS, amount=TOPUP_USDC)
pass
return pdf
Django example
# views.py
from django.http import HttpResponse
from docapi import DocAPI
import os
client = DocAPI(os.environ["DOCAPI_KEY"])
def invoice_pdf(request, invoice_id):
invoice = Invoice.objects.get(pk=invoice_id)
html = render_to_string("invoice.html", {"invoice": invoice})
pdf = client.pdf(html, format="A4")
return HttpResponse(pdf, content_type="application/pdf")
Flask example
from flask import Flask, send_file
from docapi import DocAPI
import io, os
app = Flask(__name__)
client = DocAPI(os.environ["DOCAPI_KEY"])
@app.route("/invoice/<int:invoice_id>.pdf")
def invoice_pdf(invoice_id):
html = build_invoice_html(invoice_id)
pdf = client.pdf(html)
return send_file(io.BytesIO(pdf), mimetype="application/pdf")
Links
- Documentation
- Pricing
- MCP Server — use DocAPI directly from Claude Desktop
- npm SDK — Node.js version
- GitHub
Metadata
Release files for docapi-sdk 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| docapi_sdk-0.1.0.tar.gz | 5.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| docapi_sdk-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.9 kB
Release files / docapi_sdk-0.1.0.tar.gz
| Download URL | docapi_sdk-0.1.0.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d5a773da63d6ef4e1ee850c00d8c847201bdeb4c14a0d687ad58a84bdde92621
|
|
BLAKE2b-256 checksum How to use checksums |
2e0ff2b5710374d53fce32ca7684c5ba3f25633c1576faf53ee5995c41a082a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.0
|
Release files / docapi_sdk-0.1.0-py3-none-any.whl
| Download URL | docapi_sdk-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
24cb2c2c792cd30e8cea5d8e25371746f19505484f2b0f95b7b0a42ede73126f
|
|
BLAKE2b-256 checksum How to use checksums |
a66d3f22426cb945d94856a98dbae99a8a839d73881c8ffce06b5934bd0f736f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.0
|
Security for docapi-sdk 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.