Use the pixabay images/video API with nodejs – 2023


What is REST API?
A REST API is a way for different computer programs to communicate with each other over the internet. Think of it like a set of rules for how two programs should talk to each other. One program, called the “client,” sends a request to another program, called the “server,” asking for some information or to do something.

The server then sends a response back to the client with the information it requested or a message saying it completed the task. This way, different programs can work together to share information or perform tasks without having to know how the other program works. The code you’re going to write in NodeJS below will create your server.

I’ve used a telegram bot for the script’s output. You can use it to build the same or use it in a web app, etc.


Pixabay is quite a famous website to download/contribute photos that have a royalty-free license. They offer a REST API, through which we can get the relevant photos based on our search query, users, collection IDs, etc.

They support photos, images, illustrations, vectors, music, sound effects, gifs, and videos. However, music, sound effects, and gifs are not available via the API as of writing this article.
The quality of output is really good, as tons of photographers contribute, including me!


There is an unofficial NodeJS wrapper available for Pixabay like pixabay-api, however, it was last updated 6 years ago at the time of writing so won’t recommend it. We are going to use the Axios library to make requests. You can use another one as per your preference.

So for the development, we’ll use the plain API by making GET requests. If you need more granular control you should go by the plain API.
Check out the above wrapper in case it’s updated.

Step 1: Get the Pixabay key

Go to the docs, and make sure you’re logged in. Once you scroll down a little your key will be displayed there. Copy it as it is required to make it work.

Step 2: Install the dependencies and code

npm i axios dotenv // dotenv for storing the API key in .env file

I’m updating this bot (@FreeStockPhotosBot) of mine which has over 8k+ users. Earlier it used the Unsplash source but they are shutting it down so pixabay is a great option.

const axios = require("axios");
const config = require("../../config"); // Abstracter for .env vars

// Base API endpoint-
let requestUrl = `${ config.pixabay_key }&q=${ searchQuery }&image_type=all&per_page=200`;

   .then(function (response) {
      // API is reatelimited to 100 requests/minute
       ctx.replyWithHTML(ctx.getString(ctx, 'error.too_many_requests'));
// is an array containing the image's URL and other data in objects
// If no output is given, throw an error or tell the user

// As I'm using telegram bot for output, hence.
      ctx.replyWithHTML(ctx.getString(ctx, 'error.photo_not_found'));
 } else {
     // return the output image
  .catch(function (error) {
    // handle errors
    ctx.replyWithHTML(ctx.getString(ctx, 'error.api_error'));

Response from the API after search:-

            "tags":"lizard, multicoloured, head",
        },    {
        "id": 73424,

To get the relevant output, add or remove the request parameters in the base API endpoint like: –

q – The search query, if removed returns all images randomly. Needs to be URL encoded or in plus case. (cute+cat)

per_page – Number of images returned per search query, maximum 200. Default at 20

image_type – Supports “all”, “photo”, “illustration”, and “vector”. “all” is the default.

editors_choice – “true” or “false”, Select images that have received an Editor’s Choice award. If you want variety and more options to choose from don’t use this.

Others are mentioned here in the docs.


Everything stays the same, but the API endpoint changes to –

One additional parameter is added, as you may have guessed.
video_type – Accepted values: “all”, “film”, “animation”, Default is all.

The response changes a bit:

            "tags":"poppy field, poppy, field",

Changes are to be made to the above code for Videos to work.


// Base API endpoint-
let requestUrl = `${ config.pixabay_key }&q=${ searchQuery }&video_type=all&per_page=200`;

  // return the output video


As far as I’ve used the pixabay’s service is very reliable and the output quality is great, thanks to the awesome photographers!

I hope they introduce access to music, sound effects, and gif. Meanwhile, check more posts development posts.

Give me follow on Twitter if this helped 🙂