When to Use
When the user asks you to:
- Upload HTML or ZIP static pages and generate a shareable public preview link
- List, query, and update the title, notes, or expiration of preview items
- Replace preview content (keeping the same public URL)
- Move previews to the recycle bin, restore them, or permanently delete them
- Interact with HTML Preview programmatically (CI, Agent, curl)
Authentication Configuration
HTML Preview uses API Key for programmatic authentication. The Key format is a URL-safe random string starting with hp_ ; it is displayed in full only once on the Web settings page when created.
First Use (Configuration Persistence)
If ~/.config/html-preview/config.yaml does not exist,you mustask the user for the following information:
- Base URL: service address, for example
http://localhost:7849orhttps://preview.example.com(default port 7849) - API Key: create it in Web "Settings → API Key" (starting with
hp_)
Then write the configuration to the file:
# ~/.config/html-preview/config.yaml
base_url: "http://localhost:7849"
api_key: "hp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Note:
~/.configthe directory usually already exists on macOS/Linux; if it does not exist, create the directory first.
Permission Boundaries:API Key You cannotcreate, list, or revoke API Keys (
/api/keys*only supports Session Cookie). Bearer access to Key management endpoints returns40300. Key creation and revocation must be done in the Web management interface.
Subsequent Use
Directly read ~/.config/html-preview/config.yaml from base_url and api_key,do not ask the user again。
Request Headers
JSON request:
Authorization: Bearer <api_key>
Content-Type: application/json; charset=utf-8
multipart upload (POST /api/items、PUT /api/items/:id/content):
Authorization: Bearer <api_key>
(automatically set by curl -F Auto-set Content-Type: multipart/form-data, do not manually override it to JSON.)
Web Management Entry
| Path | Description |
|---|---|
/ |
Home: search, grid, upload |
/trash |
Recycle bin: restore, permanently delete |
/settings |
Change password, API Key, one-click browser install, data path |
/setup |
First-time password setup (when uninitialized) |
/login |
Login |
Listens on 0.0.0.0:7849by default; local management page example:http://localhost:7849/。
Public Preview URL
Each preview item has a fixed public path (no authentication required):
{base_url}/c/{id}/
Example:http://localhost:7849/c/AbCdEfGhIjKlMnOp/
- Only
activeitems that have not expired are accessible; expired items or those already in the recycle bin are unavailable. - Responses carry security headers such as CSP sandbox; the management grid does not embed iframe previews.
The public_path field in Item details is an in-site path (such as /c/{id}/), full URL = base_url + public_path。
Upload Limits
| Type | Limit |
|---|---|
| HTML single file | ≤ 2 MB |
| ZIP archive | ≤ 20 MB |
| Total size after ZIP extraction | ≤ 50 MB |
| Number of files in ZIP | ≤ 200 |
| ZIP root directory | Must contain index.html |
| Security | Reject .., absolute paths, weird symlinks,nested zip |
HTML uploads are normalized and saved as index.html; ZIP is extracted to the project directory by relative path.
Validity period
Can be specified on upload or PATCH:
expires_in |
Meaning |
|---|---|
1d |
Expires after 1 day |
7d |
After 7 days |
30d |
After 30 days |
90d |
After 90 days |
never |
Permanent |
| (not passed) | Upload default 30d; if PATCH does not pass an expiration field, it is not modified |
If both expires_at(ISO 8601 / RFC3339) and expires_inare passed, expires_at takes precedence. Permanent must be explicitly passed as never。
After expiration, it is automatically moved to the recycle bin in the background; the recycle bin retains it for 30 days before permanent deletion.
Core endpoints
All successful responses:{"code":0,"data":...}; failures:{"code":<int>,"message":"..."}。
Upload preview
curl -X POST "$BASE_URL/api/items" \
-H "Authorization: Bearer $API_KEY" \
-F "file=@page.html" \
-F "title=演示页" \
-F "notes=可选备注" \
-F "expires_in=7d"
Fields:file(required),title、notes、expires_in、expires_at。
List
curl -G "$BASE_URL/api/items" \
-H "Authorization: Bearer $API_KEY" \
--data-urlencode "q=关键词" \
--data-urlencode "status=active" \
--data-urlencode "favorite=true" \
--data-urlencode "page=1" \
--data-urlencode "page_size=24"
| Parameter | Description |
|---|---|
q |
Title/note keyword |
status |
active(default) or trash |
favorite |
Optional, accepts only the literal true(returns only favorited items; other values return 40001) |
page |
Page number, default 1 |
page_size |
Items per page, default 24, maximum 100 |
Sorting is fixed to updated_at descending;favorite can be freely combined with q、status、page、page_size .
Response data:{items, page, page_size, total}。
Details
curl "$BASE_URL/api/items/$ITEM_ID" \
-H "Authorization: Bearer $API_KEY"
Update metadata / validity period
curl -X PATCH "$BASE_URL/api/items/$ITEM_ID" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json; charset=utf-8" \
-d '{"title":"新标题","notes":"新备注","expires_in":"30d"}'
Favorite / unfavorite
curl -X POST "$BASE_URL/api/items/$ITEM_ID/favorite" \
-H "Authorization: Bearer $API_KEY"
curl -X DELETE "$BASE_URL/api/items/$ITEM_ID/favorite" \
-H "Authorization: Bearer $API_KEY"
Both return the updated item; idempotent, repeated favoriting/unfavoriting both succeed (repeated favoriting keeps the first favorited_at). Only active items: recycle bin items return 40900, nonexistent returns 40400。
- Favorite toggleNot updated
updated_at, does not affect list sorting. favoritecannot be modified viaPATCH /api/items/{id}(unknown fields are ignored); the dedicated endpoint above must be used.- Favorites are global state; all API Keys share the same favorites.
Item fields:favorite(bool)、favorited_at(RFC3339 UTC string or null, favorite time; after unfavoriting it is null)。
Replace content (ID and public URL unchanged, triggers re-screenshot)
curl -X PUT "$BASE_URL/api/items/$ITEM_ID/content" \
-H "Authorization: Bearer $API_KEY" \
-F "file=@updated.zip"
Move to recycle bin
curl -X DELETE "$BASE_URL/api/items/$ITEM_ID" \
-H "Authorization: Bearer $API_KEY"
Restore from recycle bin
curl -X POST "$BASE_URL/api/items/$ITEM_ID/restore" \
-H "Authorization: Bearer $API_KEY"
Permanently delete (irrecoverable)
curl -X DELETE "$BASE_URL/api/items/$ITEM_ID/permanent" \
-H "Authorization: Bearer $API_KEY"
Thumbnail (requires authentication)
curl "$BASE_URL/api/items/$ITEM_ID/thumb/desktop" \
-H "Authorization: Bearer $API_KEY" -o thumb.webp
variant:desktop or mobile; 404 when there is no thumbnail.
API Key permission scope
| Resource | Session(Cookie) | API Key(Bearer) |
|---|---|---|
| Items read/write (upload, PATCH, replace, favorite, recycle bin, restore, permanent delete) | ✅ | ✅ |
| Thumbnail | ✅ | ✅ |
API Key CRUD(/api/keys*) |
✅ | ❌(40300) |
Password change, browser install (/api/settings/*) |
✅ | ❌(40300) |
Public page /c/{id}/ |
No login required | No login required |
Error handling
| HTTP status | Code | Meaning |
|---|---|---|
| 400 | 40001 | Parameter/validation failure (invalid expiration, unsafe zip, missing index.html, etc.) |
| 401 | 40100 | Not logged in or Key invalid/revoked |
| 403 | 40300 | Forbidden (Key accessing Key management, Session-only endpoints, etc.) |
| 404 | 40400 | Resource does not exist |
| 409 | 40900 | State conflict (e.g., erroneous operation on a deleted item) |
| 413 | 41300 | File too large |
| 500 | 50000 | Internal error |
When receiving 401, check:
Authorization: Bearer hp_...is correctbase_urlpoints to the correct instance and port (default 7849)- Key has been revoked on the Web settings page
Notes
- Update and delete operations use the Item's
id(URL-safe public id), not the filename. - For screenshot status, see
screenshot_status:pending/ready/failed/no_browser; uploads still succeed when no browser is available. - All time fields are in ISO 8601 (RFC3339) format.
- For JSON requests, it is recommended to use
--data-binary @body.jsonto avoid shell encoding issues.