js-web-renderer REST API
REST API for rendering JavaScript-heavy web pages using js-web-renderer.
Features
- Render pages and return HTML content
- Take screenshots
- Capture network requests
- Session persistence with browser profiles
- API key authentication
Installation
Quick Install (on target server)
git clone https://github.com/iceman1010/js-web-renderer-REST-API.git
cd js-web-renderer-REST-API
chmod +x install.sh
./install.sh
Manual Install
- Install dependencies:
pip3 install -r requirements.txt
- Create
.env from example:
cp .env.example .env
# Edit .env with your settings
- Run the server:
python3 -m uvicorn app.main:app --host 0.0.0.0 --port 9000
Configuration
Edit .env:
API_KEY=your-secret-api-key
PROFILES_DIR=/opt/js-web-renderer/profiles
JS_WEB_RENDERER_PATH=/opt/js-web-renderer/bin/fetch-rendered.py
HOST=0.0.0.0
PORT=9000
MAX_INSTANCES=4
API Endpoints
Rendering
| Method |
Endpoint |
Description |
POST |
/render |
Render page, return HTML + current URL |
POST |
/screenshot |
Render page, return PNG image |
POST |
/network |
Render page, return network requests |
Profiles
| Method |
Endpoint |
Description |
GET |
/profiles |
List all saved profiles |
POST |
/profiles |
Create a new empty profile |
GET |
/profiles/{name} |
Get profile info |
DELETE |
/profiles/{name} |
Delete a profile |
System
| Method |
Endpoint |
Description |
GET |
/health |
Health check (no auth required) |
GET |
/docs |
OpenAPI documentation |
Authentication
All endpoints except /health require an API key in the X-API-Key header.
Usage Examples
Render a page
curl -X POST http://localhost:9000/render \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com", "wait": 5}'
Take a screenshot
curl -X POST http://localhost:9000/screenshot \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com", "wait": 5, "width": 1280, "height": 900}' \
--output screenshot.png
Login and save session
# Create profile
curl -X POST http://localhost:9000/profiles \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"name": "my-session"}'
# Login with profile
curl -X POST http://localhost:9000/render \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com/login",
"wait": 5,
"profile": "my-session",
"type_actions": [
{"selector": "input[name=username]", "value": "myuser"},
{"selector": "input[name=password]", "value": "mypass"}
],
"click_actions": ["button[type=submit]"],
"post_wait": 10
}'
# Use saved session
curl -X POST http://localhost:9000/render \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/dashboard", "wait": 5, "profile": "my-session"}'
Capture network requests
curl -X POST http://localhost:9000/network \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com", "wait": 5}'
Response:
{
"success": true,
"requests": [
{"url": "https://example.com/"},
{"url": "https://example.com/style.css"},
{"url": "https://example.com/script.js"}
],
"current_url": null
}
Request Parameters
Common Parameters
| Parameter |
Type |
Default |
Description |
url |
string |
required |
URL to render |
wait |
int |
5 |
Seconds to wait for page load (0-60) |
profile |
string |
null |
Profile name for session persistence |
type_actions |
array |
null |
List of {selector, value} to type |
click_actions |
array |
null |
List of CSS selectors to click |
post_wait |
int |
null |
Seconds to wait after actions (0-120) |
exec_js |
string |
null |
JavaScript to execute before load |
post_js |
string |
null |
JavaScript to execute after actions |
Screenshot Parameters
| Parameter |
Type |
Default |
Description |
width |
int |
1280 |
Viewport width (320-3840) |
height |
int |
900 |
Viewport height (240-2160) |
Deployment
Local Deploy Script
The project includes a deploy.sh script for deploying to the production server:
# Deploy to whisper1 server
./deploy.sh
The script will:
- Pull latest code from GitHub on the server
- Fix permissions (ensure CLI is executable)
- Restart the service
Manual Deployment
# On the server
cd /opt/js-web-renderer-api
git fetch origin master
git reset --hard origin/master
sudo systemctl restart js-web-renderer-api
Testing
Prerequisites
pip3 install pytest pytest-asyncio httpx
Environment Variables
Set these environment variables before running tests:
export API_KEY="your-api-key" # From .env file on server
export TEST_BASE_URL="http://whisper1:9000"
Run Tests
# Run all tests
pytest
# Run specific test suites
pytest tests/test_api.py # REST API tests
pytest tests/test_cli.py # CLI tool tests (via SSH)
pytest tests/test_concurrency.py # Concurrency limiting tests
Test Coverage
- API Tests: Health, render, screenshot, network, profiles CRUD, authentication
- CLI Tests: Basic render, screenshot, network capture, console output, help
- Concurrency Tests: Instance limiting, 429 responses when limit exceeded
Health Endpoint
The /health endpoint returns:
{
"status": "healthy",
"renderer_available": true,
"active_instances": 0,
"max_instances": 4
}
active_instances: Number of browsers currently rendering
max_instances: Maximum concurrent browsers allowed (configured via MAX_INSTANCES)
When active_instances reaches max_instances, new requests receive HTTP 429 (Too Many Requests).
License
MIT