This guide shows you how to authenticate requests to the HTML2PDF app API using an API key. It covers how to include the key in your HTTP requests, whether you’re using a simple REST client, cURL, or a programming language like JavaScript or Python. You’ll need an active API key from your HTML2PDF account dashboard.
What You’ll Need
- An HTML2PDF app account with an active API key (usually found in the account settings or developer dashboard).
- The API endpoint URL for the conversion service (typically something like
https://api.html2pdf.app/v1/generate). - An HTTP client: cURL, Postman, or a code environment (Node.js, Python, etc.).
Step-by-Step
- Locate your API key. Log in to your HTML2PDF account and navigate to the API section. Copy the key—it’s usually a long alphanumeric string. Keep it secret; treat it like a password.
- Understand the authentication method. Most HTML2PDF APIs accept the key as a query parameter named
api_keyor as a Bearer token in the Authorization header. Check the official documentation for your provider. This guide covers both common methods. - Test with cURL (query parameter method). Open your terminal and run:
curl -X POST "https://api.html2pdf.app/v1/generate?api_key=YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{"html": "<h1>Hello</h1>"}'Replace
YOUR_API_KEYwith your actual key. If successful, you’ll receive a PDF file in the response. - Test with cURL (Authorization header method). Some APIs prefer the key in the header:
curl -X POST "https://api.html2pdf.app/v1/generate" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{"html": "<h1>Hello</h1>"}' - In JavaScript (fetch). If you’re building a web app, use fetch with the key in the header:
fetch('https://api.html2pdf.app/v1/generate', { method: 'POST', headers: { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' }, body: JSON.stringify({ html: '<h1>Hello</h1>' }) }) .then(res => res.blob()) .then(blob => { /* handle PDF blob */ }); - In Python (requests library). Install
requestsif needed (pip install requests), then:import requests url = 'https://api.html2pdf.app/v1/generate' headers = {'Authorization': 'Bearer YOUR_API_KEY'} data = {'html': '<h1>Hello</h1>'} response = requests.post(url, headers=headers, json=data) if response.status_code == 200: with open('output.pdf', 'wb') as f: f.write(response.content) else: print('Error:', response.status_code, response.text) - Handle the response. A successful request returns a PDF file (binary content). Save it to disk or stream it to the client. Errors return JSON with an error message and appropriate status code (e.g., 401 for invalid key).
Common Mistakes
- Hardcoding the API key in client-side code. If you’re using JavaScript in the browser, the key is exposed to anyone viewing the source. Always proxy requests through a backend server.
- Incorrect parameter name. Some APIs use
apikey,key, ortokeninstead ofapi_key. Verify the exact name in the docs. - Using GET instead of POST. The conversion endpoint usually requires POST, especially when sending HTML content in the body.
- Forgetting to set Content-Type. If you send JSON, include
Content-Type: application/json; otherwise, the API may not parse the body. - Not URL-encoding the HTML. When passing HTML as a query parameter (rare), encode it properly. Better to use the request body.
Tips
- Store the key in environment variables (e.g.,
process.env.HTML2PDF_API_KEY) to avoid hardcoding. - Set a timeout on your HTTP requests (e.g., 30 seconds) to avoid hanging when the API is slow.
- Check the API documentation for rate limits. If you hit them, implement exponential backoff.
- Use a service like Postman to test endpoints quickly before writing code.
- Rotate your API key periodically for security. Most dashboards allow you to regenerate.
Troubleshooting
- 401 Unauthorized: Your API key is invalid, expired, or not being sent correctly. Double-check the key and the header/parameter name.
- 403 Forbidden: The key may not have permission for this endpoint, or your IP is blocked. Check your account plan and allowed IPs.
- 400 Bad Request: The request body is malformed. Ensure your JSON is valid and the HTML field is present.
- 429 Too Many Requests: You’ve exceeded the rate limit. Wait and retry, or upgrade your plan.
- Empty response or connection reset: The HTML content might be too large or contain unsupported elements. Try a smaller snippet to isolate the issue.
- PDF is blank or garbled: Your HTML may rely on external resources (CSS, images) that the API can’t access. Use inline CSS or provide full URLs.
Frequently Asked Questions
Can I use the API key in a URL for GET requests?
Yes, but it’s less secure because URLs can be logged. Prefer the Authorization header for POST requests.
How do I get an API key?
Sign up for an HTML2PDF account, then go to the API section in your dashboard. The key is generated automatically.
Is the API key the same as my login password?
No, it’s a separate credential specifically for API access. Never share it.
What if my API key is exposed in a public repository?
Immediately regenerate the key from your dashboard. The old one will stop working.
Can I send HTML with CSS and images?
Yes, but ensure all resources are accessible via public URLs or embedded as base64 data URIs.
Does the API support asynchronous callbacks?
Some providers do. Check the documentation for webhook support. If not, you’ll need to poll or wait for the synchronous response.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix 0x80070005 DISM Error & the 0x800070005 Typo
- Fix Access Denied Adding Shared Printer in Windows 10
- How to Play Minesweeper: Rules, Tips, and Winning Strategy
- Browser SQL Sandbox for Games: Build Guide
- Snapchat Face Swap: Step-by-Step Guide
- Fix a Computer Chair Height Adjuster: Step-by-Step