Node.js browshot

Browshot for Node.js is available on npm.

npm i browshot

Here are a couple of basic code samples. You can find more code samples for each API call on the API Documentation page.

Download a screenshot (simple API)

/**********************
* WARNING
* Running this code sample will cost you Browshot credits
***********************/
'use strict';

const browshot = require('browshot');
const fs = require("fs");

var client = new browshot('my_api_key');

// default cache, free screenshot
client.simple({ url: 'http://mobilito.net/', cache: 60 * 60 * 24 * 365, instance_id: 12 }, 
	function(result) {
		// it will return when the screenshot finished or failed
		if (result.code == 200) {
			fs.writeFile("screenshot.png", result.data, (err) => {
				if (err) {
					console.log(`Failed to write image to screenshot.png: ${err}`);
				}
				else {
					console.log("Image saved to screenshot.png");
				}
			});
		}
		else {
			console.log("Screenshot failed!");
			// The reason for the error is sent as part of the HTTP response in the X-Error header but it is not exposed by this library.
		}
	}
);

// quicker way to save a screenshot
client.simpleFile('mobilito.png', 
	{ url: 'http://mobilito.net/', cache: 0, instance_id: 65, screen_width: 1280, screen_height: 1024 }, //no cache, premium browser
	function(data) {
		if (data.file != '')
			console.log(`Screenshot saved to ${data.file}`);
		else
			console.log("The screenshot failed");
	}
);

Download code sample

Download a screenshot (full API)

/**********************
* WARNING
* Running this code sample will cost you Browshot credits
***********************/

const browshot = require('browshot');
const fs = require("fs");

var client = new browshot('my_api_key');


var interval;

// all default parameters, instance_id = 12 (free)
client.screenshotCreate({
		url: 'http://www.google.com/', 
		instance_id: 12, 
		size: 'page'
	},
	function(screenshot) {
		// If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
		if (screenshot.status == 'error') {
			console.log(`Screenshot #${screenshot.id} failed: ${screenshot.error}`);
		}
		else if (screenshot.status == 'finished') {
			screenshotFinished(screenshot);
		}
		else {
			interval = setInterval(checkScreenshot, 1000 * 10, screenshot.id);
		}
	}
);

function checkScreenshot(id) {
	client.screenshotInfo(id, { }, function(screenshot) {
		if (screenshot.status == 'error') {
			clearInterval(interval);
			
			console.log(`Screenshot #${screenshot.id} failed: ${screenshot.error}`);
		}
		else if (screenshot.status == 'finished') {
			clearInterval(interval);
			
			screenshotFinished(screenshot);
		}
		else {
			console.log(`Waiting for screenshot #${screenshot.id} to finish...`);
		}
	});
}

function screenshotFinished(screenshot) {
	// request the thumbnail
	client.screenshotThumbnail(screenshot.id, { }, function(image) {
		fs.writeFile("browshot.png", image, (err) => {
			if (err) {
				console.log(`Failed to write image to browshot.png: ${err}`);
			}
			else {
				console.log("Image saved to browshot.png");
			}
		});
	});
}

Download code sample

Download a 640x480 thumbnail (simple API)

/**********************
* WARNING
* Running this code sample will cost you Browshot credits
***********************/
'use strict';

const browshot = require('browshot');
const fs = require("fs");

var client = new browshot('my_api_key');


client.simpleFile('google-640.png', 
	{ url: 'http://www.google.com/', width: 640, height: 480 },
	function(data) {
		if (data.file != '')
			console.log(`Screenshot saved to ${data.file}`);
		else
			console.log("The screenshot failed");
	}
);

Download code sample

Download a 640x480 thumbnail (full API)

/**********************
* WARNING
* Running this code sample will cost you Browshot credits
***********************/

const browshot = require('browshot');
const fs = require("fs");

var client = new browshot('my_api_key');


var interval;

// all default parameters, instance_id = 12 (free)
client.screenshotCreate({
		url: 'http://www.google.com/', 
		instance_id: 12, 
		size: 'screen'
	},
	function(screenshot) {
		// If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
		if (screenshot.status == 'error') {
			console.log(`Screenshot #${screenshot.id} failed: ${screenshot.error}`);
		}
		else if (screenshot.status == 'finished') {
			screenshotFinished(screenshot);
		}
		else {
			interval = setInterval(checkScreenshot, 1000 * 10, screenshot.id);
		}
	}
);

function checkScreenshot(id) {
	client.screenshotInfo(id, { }, function(screenshot) {
		if (screenshot.status == 'error') {
			clearInterval(interval);
			
			console.log(`Screenshot #${screenshot.id} failed: ${screenshot.error}`);
		}
		else if (screenshot.status == 'finished') {
			clearInterval(interval);
			
			screenshotFinished(screenshot);
		}
		else {
			console.log(`Waiting for screenshot #${screenshot.id} to finish...`);
		}
	});
}

function screenshotFinished(screenshot) {
	// request the thumbnail
	client.screenshotThumbnailFile(screenshot.id, 'google-640.png', 
		{width: 640, height: 480 }, 
		function(file) {
			if (file == '') {
				console.log("Failed to save thumbnail to google-640.png");
			}
			else {
				console.log(`Thumbnail saved to ${file}`);
			}
		}
	);
}

Download code sample

Try it for free

close

sign up for free account

Already have an account?

no credit card required

About Us

Browshot is a web service to create real time web screenshots in a multitude of virtual devices, including mobile devices like the iPhone 3 & 4, iPad, Android Nexus, etc.

You can use the web dashboard, or our full-featured API.

  • Real time screenshots

  • 15+ mobile devices: iPhone, iPad, Android, etc.

  • 30+ desktop resolutions

  • Fast and reliable

  • Thumbnails of any size, any ratio

  • Full API, open-source libraries

Share