npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pexels-api-client

v1.0.7

Published

Client for Pexels API

Downloads

4

Readme

PexelsAPIClient

JavaScript Client for the Pexels API (pexels.com)

npm install pexels-api-client --save
let PhotoAPI = require('pexels-api-client')

Implements several methods for using the pexels API

Provides methods for popular searches that are a subset of popular searches found on pexels.com:

https://www.pexels.com/popular-searches/

You can also get curated photos.

All of the methods that are popular search methods have the type of photo in the name of the method and either start with 'get' or 'query' such as

getCurated() or queryCurated()

get methods are a simple query that always returns 20 photos from the 1st page.

query methods allow defining options. See below for more info.

If you are getting undefined for the results you most likely need to parse the returned photos object, i.e., JSON.parse(photos)

Configuration:

Create a .env file in your route directory and enter your pexels api key for a variable titled: PexelsAPIKey

All methods return a promise.

Default port used is 443. These options can be changed on every function by providing an options object in the parameter written in the form of

   {
        url: ...
        port ...
        headers {
            "Authorization": process.env.PexelsAPIKey
        }
   }

examples:

Basic get method for popular searches.

// curated
PhotoAPI.getCurated().then((photos) => {
    console.log(photos);
});

Searching:

// parameters: type, photo amount, page
PhotoAPI.search("mountains", 10, 1).then((photos) => {
    console.log(photos);
});

Set your own options in the request:

let options = { ... }

PhotoAPI.getCurated(options).then((photos) => {
    console.log(photos)
});

// For searching:
// parameters: type, photo amount, page, options
PhotoAPI.search("mountains", 10, 1, options).then((photos) => {
    console.log(photos);
});

Methods starting with query

Be aware that attempting to ask for more then 40 photos will always return a bad request as that is the max amount pexels.com allows for one call.

Each Method that starts with the word 'query' takes two parameters which are objects

The first object is a query options object that is defined as:

let queryOptions = {
    perPage: 15,
    page: 1
}

The only thing to remember is to just use the names perPage and page in an object where perPage is the amount of photos to get and page is the page on pexels.com to get them from.

The second object is the options object mentioned above for changing the way the request is made.

PhotoAPI.queryAdventure(queryOptions, requestOptions).then((photos) => {
    console.log(photos)
});

// Or 

PhotoAPI.queryAdventure({perPage: 15, page: 1}).then((photos) => {
    console.log(photos)
});

Other search methods

There are a couple of other search query method that allows defining what to search for as well as the authorization key.

searchWithKey(url, key)

Where the url is the complete url for the pexels search such as

https://api.pexels.com/v1/curated?per_page=20&page=1

And The key is:

"Bearer YOUR_AUTHORIZATION_KEY"

Note that it is never a good idea to put API keys in JavaScript, Always use an environment variable in production.

Another search method:

    queryWithKey(queryOptions, key)
    
    // Where queryOptions example is
    {
        type: "city"
        perPage: 15,
        page: 1,
    }
    
    // And key is 
    "Bearer YOUR_AUTHORIZATION_KEY"
    
    

The only difference in this method is it allows you to set the type of search and the API key without having to specify an object for the request, but you do have to set the object for the query options.