Unparalleled suite of productivity-boosting Web APIs & cloud-based micro-service applications for developers and companies of any size.

APIAutomationJavascript

Easily Build Your Own Website Screenshot CDN In NodeJS

Easily Build Your Own Website Screenshot CDN In NodeJS

Screenshotlayer is a lightweight REST API built to deliver high-quality PNG, JPEG & GIF website screenshots at unparalleled speeds and through a simple interface. To ensure the highest possible level of image resolution, the Screenshotlayer API relies on a powerful rendering engine capable of processing, storing, and returning all sorts of website content within just a few seconds.

In this blog post, we’ll take a look at how we can easily build our own website screenshot CDN in NodeJS using the Screenshotlayer API.

What are the minimum Node dependencies that are required for this project?

As part of the CDN development process, we will initially require express and fs Node packages to seamlessly achieve the development experience. If these packages are already part of node_modules in your Node project then well and good. If not, go ahead and install these two dependencies using npm

Once installed, import the dependencies as follows. 

How can I create a REST endpoint for taking screenshots with Screenshotlayer?

In this section, we’ll create a REST API endpoint /full-height-screenshot in our Node application that will internally hit the Screenshotlayer API endpoint to get the screenshot of the website passed in the request payload. 

The Screenshotlayer API endpoint URL looks as follows. 

Once we have the endpoint URL ready, let’s hit the API endpoint to get the screenshot of the requested website. The following code snippet can do this job. 

Next up, we need to save this screenshot as an image file on our server. Why are we doing that? Well, hold on a bit, it will get clearer in the next section. 

We can save the screenshot as an image on our server as follows. 

Now that we have the individual components ready, let’s combine them under our /full-height-screenshot endpoint to serve it to the client over a network. 

How can I use the cached API results returned from Screenshotlayer?

Up until here, we are hitting the Screenshotlayer API every time we get a request on our server. This increases our server response time as numerous secondary API calls are being made against each API request. 

We can optimize this process by using caches. In the above code snippets, we have already implemented the caching mechanism by saving the API results as an image file on our server, but now, let’s actually use these image files to optimize the request-response cycle. 

Using the following piece of code, we can check whether the file already exists or not. If the file exists for less than 24 hours, we’ll return the cached file. Otherwise, we’ll make a fresh API call and cache its results again. 

Our all-in-all REST endpoint with caching looks something like below. 

How can I serve the screenshot REST endpoint using Express?

Now that we have the main CDN engine set up, it’s time to serve it and open our server connection to listen to API requests. Using express, we can easily achieve this by writing the following lines of code. 

As you can see, creating your own website screenshot CDN in NodeJS using Screenshotlayer is pretty simple and quick. Not only that, Screenshotlayer is reliable and easy on your budget to help you enable a diverse digital ecosystem that focuses on user experience. Moreover, the free plan of Screenshotlayer is an ideal choice for experimentation, trials, and proof of concepts.

Head over to Screenshotlayer and get creative with your custom screenshot REST endpoints.

Related posts
API

12 Steps to Find the Perfect API to Verify Email Address

API

API Integration: How to Integrate API into WordPress Page

APIIPLocation

Why Are IP Trackers Important For Cybersecurity And Threat Intel?

API

API Development | A Guide to Develop RESTful API with Node JS

Leave a Reply

Your email address will not be published. Required fields are marked *