Package Exports
- snapapi-js
- snapapi-js/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (snapapi-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
snapapi-js
Official JavaScript/TypeScript SDK for SnapAPI — a fast, reliable Screenshot & PDF generation API powered by headless Chrome.
Free tier available — 5 screenshots free, no credit card required. Get your API key at opspawn.com/snapapi
Quick Start
npm install snapapi-jsconst SnapAPI = require('snapapi-js');
const snap = new SnapAPI({ apiKey: 'YOUR_API_KEY' });
const image = await snap.screenshot('https://example.com');
require('fs').writeFileSync('screenshot.png', image);That's it. Three lines of code to capture any webpage as a PNG.
Installation
# npm
npm install snapapi-js
# yarn
yarn add snapapi-js
# pnpm
pnpm add snapapi-jsUsage
Screenshot a URL
const SnapAPI = require('snapapi-js');
const fs = require('fs');
const snap = new SnapAPI({ apiKey: 'YOUR_API_KEY' });
// Basic screenshot
const png = await snap.screenshot('https://github.com');
fs.writeFileSync('github.png', png);
// Full-page screenshot in JPEG
const jpg = await snap.screenshot('https://example.com', {
fullPage: true,
format: 'jpeg',
quality: 85,
width: 1440,
height: 900,
});
fs.writeFileSync('full-page.jpg', jpg);
// Screenshot a specific element
const element = await snap.screenshot('https://example.com', {
selector: '#hero-section',
});
fs.writeFileSync('hero.png', element);Generate PDF from a URL
const snap = new SnapAPI({ apiKey: 'YOUR_API_KEY' });
// Basic PDF (A4)
const pdf = await snap.pdf('https://example.com');
fs.writeFileSync('page.pdf', pdf);
// Custom PDF options
const report = await snap.pdf('https://my-report.com', {
format: 'Letter',
landscape: true,
marginTop: '20mm',
marginBottom: '20mm',
printBackground: true,
});
fs.writeFileSync('report.pdf', report);Render from HTML
const snap = new SnapAPI({ apiKey: 'YOUR_API_KEY' });
const html = `
<html>
<body style="background: #1a1a2e; color: white; padding: 40px; font-family: sans-serif;">
<h1>Hello from SnapAPI!</h1>
<p>Generated at ${new Date().toISOString()}</p>
</body>
</html>
`;
// Screenshot from HTML
const image = await snap.fromHTML(html);
fs.writeFileSync('from-html.png', image);
// PDF from HTML
const pdf = await snap.fromHTML(html, { type: 'pdf', format: 'A4' });
fs.writeFileSync('from-html.pdf', pdf);TypeScript
import SnapAPI, { ScreenshotOptions, PDFOptions } from 'snapapi-js';
const snap = new SnapAPI({ apiKey: 'YOUR_API_KEY' });
const options: ScreenshotOptions = {
width: 1280,
height: 800,
fullPage: true,
format: 'png',
};
const buffer: Buffer = await snap.screenshot('https://example.com', options);API Reference
new SnapAPI(options)
| Option | Type | Default | Description |
|---|---|---|---|
apiKey |
string |
required | Your SnapAPI key |
baseUrl |
string |
'https://api.opspawn.com' |
API base URL |
timeout |
number |
30000 |
Request timeout in ms |
snap.screenshot(url, options?)
Captures a screenshot of the given URL.
Returns: Promise<Buffer> — PNG or JPEG image buffer
| Option | Type | Default | Description |
|---|---|---|---|
width |
number |
1280 |
Viewport width in pixels |
height |
number |
800 |
Viewport height in pixels |
format |
'png' | 'jpeg' |
'png' |
Output image format |
quality |
number |
90 |
JPEG quality (1-100) |
fullPage |
boolean |
false |
Capture full scroll height |
selector |
string |
— | CSS selector for element capture |
delay |
number |
0 |
Wait delay in ms before capture |
darkMode |
boolean |
false |
Enable dark mode emulation |
deviceScaleFactor |
1 | 2 | 3 |
1 |
Device pixel ratio |
snap.pdf(url, options?)
Generates a PDF from the given URL.
Returns: Promise<Buffer> — PDF buffer
| Option | Type | Default | Description |
|---|---|---|---|
format |
string |
'A4' |
Paper format ('A4', 'Letter', 'Legal', 'A3', 'Tabloid') |
landscape |
boolean |
false |
Landscape orientation |
printBackground |
boolean |
true |
Print CSS backgrounds |
marginTop |
string |
'10mm' |
Top margin |
marginBottom |
string |
'10mm' |
Bottom margin |
marginLeft |
string |
'10mm' |
Left margin |
marginRight |
string |
'10mm' |
Right margin |
displayHeaderFooter |
boolean |
false |
Show header/footer |
headerTemplate |
string |
— | HTML header template |
footerTemplate |
string |
— | HTML footer template |
delay |
number |
0 |
Wait delay in ms before capture |
snap.fromHTML(html, options?)
Renders a screenshot or PDF from raw HTML content.
Returns: Promise<Buffer> — Image or PDF buffer
Accepts all options from screenshot() and pdf(), plus:
| Option | Type | Default | Description |
|---|---|---|---|
type |
'screenshot' | 'pdf' |
'screenshot' |
Output type |
Error Handling
try {
const image = await snap.screenshot('https://example.com');
} catch (err) {
if (err.message.includes('Invalid API key')) {
// Get your key at https://opspawn.com/snapapi
} else if (err.message.includes('Usage limit')) {
// Upgrade your plan at https://opspawn.com/snapapi
} else if (err.message.includes('Rate limit')) {
// Slow down requests or upgrade plan
} else {
console.error(err.message);
}
}Pricing
| Plan | Screenshots | Price | |
|---|---|---|---|
| Free | 5/month | 5/month | Free, no credit card |
| Pro | 1,000/month | 1,000/month | $19/month |
| Business | 10,000/month | 10,000/month | $99/month |
Get your API key at opspawn.com/snapapi →
Why SnapAPI?
- Faster than self-hosting — No Puppeteer/Playwright setup, no browser process management
- Reliable — Managed infrastructure, 99.9% uptime SLA
- Simple — One npm install, three lines of code
- TypeScript-ready — Full type definitions included
- Drop-in replacement — Designed to replace complex Puppeteer screenshot pipelines
License
MIT — see LICENSE for details.
Built by OpSpawn — autonomous AI agent infrastructure.