Sign inSign up

synapsr/shotapi

By synapsr

โ€ขUpdated over 1 year ago

Super-simple, Docker-ready API service that captures screenshots of any webpage.

Image
0

431

synapsr/shotapi repository overview

โ ๐Ÿ“ธ ShotAPI

License: MIT Docker Ready Node.js โ‰ฅ18

ShotAPI is a super-simple, Docker-ready API service that captures screenshots of any webpage. Think of it as your open-source alternative to services like urlbox or thum.io but free and open source with full control and no usage limits!

โ โœจ Features

  • ๐Ÿ–ผ๏ธ Simple Screenshot Capture - A single API endpoint to turn any URL into an image
  • ๐Ÿ”„ Flexible Output Options - PNG, JPEG, or even PDF formats
  • โš™๏ธ Highly Customizable - Control viewport size, image quality, load times, and more
  • ๐Ÿ’พ Smart Caching - Automatic caching for faster responses and reduced load
  • ๐Ÿš€ Fast Deployment - Up and running in a single Docker command
  • ๐Ÿ”’ Production Ready - Built-in rate limiting, security headers, and optional API key authentication
  • ๐ŸŒ Works Everywhere - Captures any public website, including those with JavaScript

โ ๐Ÿš€ Quick Start

โ Using Docker (Easiest)
# Pull and run with a single command
docker run -p 3000:3000 synapsr/shotapi

# Or with Docker Compose
docker-compose up -d
โ Manual Installation
# Clone the repository
git clone https://github.com/Synapsr/ShotAPI.git
cd shotapi

# Install dependencies
npm install

# Start the server
npm start

โ ๐Ÿ” Usage Examples

โ Basic Screenshot
GET http://localhost:3000/screenshot?url=https://example.com
โ High-Resolution Screenshot
GET http://localhost:3000/screenshot?url=https://example.com&width=1920&height=1080
โ Mobile Device Emulation
GET http://localhost:3000/screenshot?url=https://example.com&width=375&height=812&userAgent=Mozilla/5.0+(iPhone;+CPU+iPhone+OS+14_0+like+Mac+OS+X)+AppleWebKit/605.1.15
โ Full-Page JPEG with Quality Setting
GET http://localhost:3000/screenshot?url=https://example.com&format=jpeg&quality=90&fullPage=true
โ Generate a PDF
GET http://localhost:3000/screenshot?url=https://example.com&format=pdf&pdfFormat=A4
โ Dark Mode Capture
GET http://localhost:3000/screenshot?url=https://example.com&darkMode=true

โ ๐Ÿ“ API Parameters

ParameterDescriptionDefaultExample
urlThe URL to capture (required)-https://example.com
widthViewport width in pixels1280width=1920
heightViewport height in pixels800height=1080
formatOutput format (png, jpeg, pdf)pngformat=jpeg
qualityImage quality (1-100, jpeg only)80quality=90
fullPageCapture full page heightfalsefullPage=true
minLoadTimeMinimum page load time in ms0minLoadTime=2000
darkModeEnable dark mode emulationfalsedarkMode=true
selectorCapture specific element-selector=#content
transparentTransparent background (png only)falsetransparent=true

See the full API documentationโ  for all available options.

โ ๐Ÿณ Docker Deployment

ShotAPI is designed to be super easy to deploy with Docker.

โ Simple Docker Run
docker run -p 3000:3000 synapsr/shotapi
โ Using Docker Compose

Create a docker-compose.yml file:

version: '3.8'

services:
  shotapi:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - PORT=3000
    volumes:
      - cache-data:/usr/src/app/cache
    restart: unless-stopped

volumes:
  cache-data:

Then run:

docker-compose up -d
โ Environment Variables
VariableDescriptionDefault
PORTPort to run the server on3000
MAX_CONCURRENT_PAGESMaximum concurrent browser pages5
DEFAULT_CACHE_TIMEDefault cache time in seconds3600
RATE_LIMIT_MAX_REQUESTSRate limit requests per window60
API_KEY_ENABLEDEnable API key authenticationfalse

โ ๐Ÿ”ง Advanced Usage

โ Custom HTTP Headers
GET http://localhost:3000/screenshot?url=https://example.com&headers={"Authorization":"Bearer+token"}
โ Waiting for Elements to Load
GET http://localhost:3000/screenshot?url=https://example.com&waitForSelector=#main-content
โ Capture a Specific Element
GET http://localhost:3000/screenshot?url=https://example.com&selector=#hero-section

โ ๐Ÿ“š Integration Examples

โ Node.js
const fetch = require('node-fetch');
const fs = require('fs');

async function getScreenshot(url) {
  const response = await fetch(
    `http://localhost:3000/screenshot?url=${encodeURIComponent(url)}`
  );
  
  if (!response.ok) throw new Error(`Error: ${response.statusText}`);
  
  const buffer = await response.buffer();
  fs.writeFileSync('screenshot.png', buffer);
  console.log('Screenshot saved!');
}

getScreenshot('https://example.com');
โ Python
import requests

def get_screenshot(url):
    response = requests.get(
        f"http://localhost:3000/screenshot?url={url}", 
        stream=True
    )
    
    response.raise_for_status()
    
    with open('screenshot.png', 'wb') as f:
        for chunk in response.iter_content(chunk_size=8192):
            f.write(chunk)
    
    print("Screenshot saved!")

get_screenshot('https://example.com')

โ ๐Ÿ›ก๏ธ Security Considerations

โ Enabling API Key Authentication

Update your .env file:

API_KEY_ENABLED=true
API_KEY=your-secure-api-key

Then include the API key in your requests:

GET http://localhost:3000/screenshot?url=https://example.com&apiKey=your-secure-api-key

Or using a header:

GET http://localhost:3000/screenshot?url=https://example.com
X-API-Key: your-secure-api-key

โ ๐Ÿงฉ Use Cases

  • ๐Ÿ–ฅ๏ธ Website Monitoring - Capture regular screenshots to monitor visual changes
  • ๐Ÿ“ฑ Social Media Previews - Generate preview images for social media sharing
  • ๐Ÿ›’ E-commerce Thumbnails - Create product thumbnails from product pages
  • ๐Ÿ“Š Report Generation - Create visual reports or dashboards as images or PDFs
  • ๐Ÿงช UI Testing - Visual regression testing for web applications

โ ๐Ÿ› ๏ธ Development

โ Prerequisites
  • Node.js 18+
  • npm or yarn
โ Local Development
# Install dependencies
npm install

# Start with auto-reloading
npm run dev
โ Running Tests
npm test

โ โ“ Troubleshooting

IssueSolution
Error: Shot failedThe page might be using anti-scraping techniques or requires cookies/authentication
Timeout errorTry increasing maxLoadTime parameter for complex pages
Blank screenshotUse minLoadTime or waitForSelector to ensure content is loaded
Missing elementsTry enabling fullPage=true or adjust viewport dimensions

โ ๐Ÿ“œ License

This project is licensed under the MIT License - see the LICENSEโ  file for details.

โ ๐Ÿค Contributing

Contributions are welcome! Check out the Contributing Guideโ  for more information.


Made with โค๏ธ by Synapsr

Star โญ this repo if you found it useful!

Tag summary

Content type

Image

Digest

sha256:4ebf19228โ€ฆ

Size

573.1 MB

Last updated

over 1 year ago

docker pull synapsr/shotapi