If you’re getting a 401 Unauthorized error from an HTML/CSS to image API, it almost always means your API key is missing, invalid, or not being sent in the request header. The fix is to verify your key, ensure it’s included in the Authorization header (or the API’s required auth field), and check that your account has permission to use that endpoint.
What Causes a 401 Error on Image Generation APIs
A 401 response means the server didn’t recognize you. Unlike a 403 (which means you’re recognized but not allowed), a 401 is an authentication failure. Common causes include:
- Missing API key – you forgot to add the header or parameter.
- Typo in the key – extra spaces, truncated characters, or copied a placeholder like
YOUR_API_KEY. - Wrong header name – some APIs use
Authorization: Bearer <key>, others useX-API-Key: <key>. - Expired or revoked key – many services rotate keys for security.
- Key not activated – some providers require you to confirm your email or enable billing before the key works.
- Region or environment mismatch – a key from a sandbox may not work in production, or a US-based key may fail on an EU endpoint.
How HTML/CSS to Image APIs Work
These services (like Html2Image, ApiFlash, or ScreenshotOne) take your HTML and CSS, render it in a headless browser, and return a PNG, JPEG, or WebP. The typical flow:
- You send a POST request with your HTML/CSS (or a URL) and options like width, height, and format.
- The API authenticates your request using an API key.
- It renders the page, takes a screenshot, and returns the image or a URL to it.
The authentication step is where the 401 happens. If the key is missing or wrong, the server stops before rendering anything.
Step-by-Step Fix for a 401 Unauthorized
Follow these steps in order – most 401s are resolved by step 3.
- Check your API key – Log into your dashboard and copy the key again. Make sure you didn’t include extra spaces or line breaks.
- Look at the API documentation – Confirm the exact header name and format. For example, some APIs expect
Authorization: Bearer sk_live_123, others expectX-Api-Key: sk_live_123. - Test with a simple tool – Use Postman or cURL to isolate the issue. A cURL example for a typical API:
curl -X POST https://api.example.com/v1/image \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{"html":"<h1>Hello</h1>"}'If this works, the problem is in your code.
- Check for key rotation – If you recently regenerated a key, the old one is dead. Update your app.
- Verify account status – Some providers require a credit card on file or a paid plan for production keys. A free trial key may expire.
- Check the endpoint URL – A typo in the URL can route you to a different service that doesn’t recognize your key.
Common Mistakes in Code That Cause 401
Beyond the key itself, subtle code bugs often trigger 401s:
- Using the wrong variable – If you stored the key in an environment variable, make sure it’s actually loaded (e.g.,
process.env.API_KEYin Node.js). - Hardcoding a placeholder – Search your code for
YOUR_API_KEYorxxx– these are often left in by mistake. - Incorrect header casing – HTTP headers are case-insensitive, but some client libraries require exact casing. Use the exact casing from the docs.
- Sending the key in the body instead of the header – Most APIs expect it in a header, but a few use a form field. Read the docs.
- Using a client-side key in a server-side call – If you’re calling from a browser, your key is exposed and might be blocked by CORS. Always use a backend proxy.
Comparing Authentication Methods Across Popular APIs
Different services handle auth differently. Here’s a quick look at common patterns (not specific to any one provider, but typical):
- Bearer Token –
Authorization: Bearer <key>– common in REST APIs, used by many screenshot services. - X-API-Key Header –
X-API-Key: <key>– simple, used by many SaaS products. - Query Parameter –
?api_key=<key>– easy but less secure (key appears in logs). - Basic Auth –
Authorization: Basic base64(username:password)– rare for image APIs but possible.
Always check the documentation – using the wrong method is a guaranteed 401.
How to Test Your API Key Without Writing Code
If you’re not sure whether your key is valid, use a tool like Postman or Insomnia:
- Create a new request to the API’s test endpoint (often
/v1/testor a simple GET). - Add the required header with your key.
- Send the request – if you get 200, the key works. If 401, the key is invalid.
You can also use cURL in your terminal:
curl -I https://api.example.com/v1/status -H "Authorization: Bearer YOUR_KEY"
If that returns HTTP/2 200, your key is fine – the issue is elsewhere in your code.
Preventing 401 Errors in Production
Once you fix the immediate problem, set up safeguards to avoid future failures:
- Use environment variables – Never hardcode keys in your source code. Store them in
.envor your hosting provider’s secrets manager. - Add monitoring – Log 401 responses and alert on them. A sudden spike might mean your key expired.
- Set up key rotation – Many providers let you create multiple keys. Rotate them periodically to reduce risk.
- Cache successful responses – If you’re generating the same image repeatedly, cache it to reduce API calls and avoid hitting rate limits.
Frequently Asked Questions
Why do I get 401 even though my key is correct?
Double-check the header name and format. Some APIs require Authorization: Bearer while others use X-API-Key. Also, make sure you’re not accidentally sending the key in the body or as a query param when the API expects a header.
Can a 401 be caused by an expired subscription?
Yes. Many services deactivate keys when a trial ends or a payment fails. Log into your dashboard and check your plan status. If the plan is inactive, you’ll get 401 or 403.
Does the API key work in both sandbox and production?
Usually not. Sandbox keys are often separate from production keys. Check if you’re using the right key for the environment. Some providers use a prefix like test_ vs live_.
What’s the difference between 401 and 403?
401 means you haven’t authenticated – the server doesn’t know who you are. 403 means you’re authenticated but don’t have permission to access that resource. If you get 403, your key is valid but your plan may not include that endpoint.
How do I find my API key in the dashboard?
Look for a section called “API Keys,” “Developer Settings,” or “Integrations.” If you can’t find it, check the documentation or contact support. Some providers hide keys behind a “Reveal” button.
Is it safe to expose my API key in a frontend app?
No. Anyone can steal it from your browser’s network tab. Always proxy requests through a backend server that holds the key. This also helps you avoid CORS issues and rate limiting.
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
- Access Your Computer From Anywhere: Step-by-Step
- How to Get More Disk Space on Mac: 12 Fixes
- Wii Browser Games With a USB or Bluetooth Controller
- Delete Focus on iPhone: Quick Steps
- Laptop Bluetooth: How to Check If It’s On and Working
- How to Reinstall Windows 10 Fast & Easily