Crate pexels_api

Source
Expand description

The pexels_api crate provides an API wrapper for Pexels. It is based on the Pexels API Documentation.

To get the API key, you have to request access from Request API Access – Pexels.

This library depends on the serde-json crate to handle the result. Thus, you have to read the documentation serde_json - Rust, especially serde_json::Value - Rust.

§Setup

Add this line to your Cargo.toml file, below [dependencies]:

pexels_api = "0.0.3"

and this to your crate root file, e.g., main.rs:

use pexels_api;

Done! Now you can use this API wrapper.

§Example

This example shows how to get the list of mountains photos.

use dotenvy::dotenv;
use std::env;
use pexels_api::{Pexels, SearchBuilder};

#[tokio::main]
async fn main() {
    dotenv().ok();
    let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
    let pexels_api_client = Pexels::new(api_key);
    let builder = SearchBuilder::new()
        .query("mountains")
        .per_page(15)
        .page(1);
    let response = pexels_api_client.search_photos(builder).await.expect("Failed to get photos");
    println!("{:?}", response);
}

and you can run it using cargo run! It’s as simple as that.

§Random photo

If you want to get a random photo, you can use the curated_photo function and set per_page to 1 and page to a random number between 1 and 1000 to get a beautiful random photo. You can do the same with popular searches if you want to get a random photo with a specific topic.

§Image formats

  • original - The size of the original image is given with the attribute width and height.
  • large - This image has a maximum width of 940 px and a maximum height of 650 px. It has the aspect ratio of the original image.
  • large2x - This image has a maximum width of 1880 px and a maximum height of 1300 px. It has the aspect ratio of the original image.
  • medium - This image has a height of 350 px and a flexible width. It has the aspect ratio of the original image.
  • small - This image has a height of 130 px and a flexible width. It has the aspect ratio of the original image.
  • portrait - This image has a width of 800 px and a height of 1200 px.
  • landscape - This image has a width of 1200 px and height of 627 px.
  • tiny - This image has a width of 280 px and height of 200 px.

Structs§

  • domain module Represents a Pexels collection.
  • Represents a request to fetch a list of collections from the Pexels API.
  • Builder for constructing a Collections request.
  • Represents the response for a list of collections.
  • photos module This endpoint enables you to receive real-time photos curated by the Pexels team.
  • Builder for Curated.
  • collections module Represents a request to fetch all featured collections from the Pexels API.
  • Builder for constructing a Featured request.
  • Retrieve a specific Photo from its id.
  • Builder for FetchPhoto.
  • Represents a request to fetch a specific video by its ID from the Pexels API.
  • Builder for FetchVideo.
  • Represents a hexadecimal color code. Used as an input value for Color::Hex when specifying a hexadecimal color code.
  • Represents a request to fetch a specific media item by its ID from the Pexels API. This endpoint returns all media items (photos and videos) within a single collection. Use the type parameter to filter results to only photos or only videos.
  • Builder for constructing a Media request.
  • Represents the response for a list of media items.
  • Client for interacting with the Pexels API
  • Represents a Pexels photo.
  • Represents different image sizes for a photo.
  • Represents the response for a list of photos.
  • videos module Represents a request for popular videos from the Pexels API.
  • Builder for Popular.
  • Represents a search query to the Pexels API.
  • Builder for Search.
  • Represents a user who created a media item.
  • Represents a Pexels video.
  • Represents a video file with different qualities.
  • Represents a preview picture of a video.
  • Represents the response for a list of videos.
  • Represents a search request to the Pexels API for videos.
  • Builder for Search.

Enums§

  • Represents the desired photo color.
  • Specifies the locale for the search query. Supported values: en-US, pt-BR, es-ES, ca-ES, de-DE, it-IT, fr-FR, sv-SE, id-ID, pl-PL, ja-JP, zh-TW, zh-CN, ko-KR, th-TH, nl-NL, hu-HU, vi-VN, cs-CZ, da-DK, fi-FI, uk-UA, el-GR, ro-RO, nb-NO, sk-SK, tr-TR, ru-RU. Default: en-US.
  • Specifies the order of items in the media collection. Supported values: asc, desc. Default: asc.
  • Specifies the type of media to request. If not provided or invalid, all media types will be returned. Supported values: photos, videos.
  • Desired photo orientation. Supported values: landscape, portrait, square. Default: landscape.
  • Errors that can occur while interacting with the Pexels API. This enum is used as the return type for functions that interact with the API.
  • Specifies the minimum size for videos or photos. Supported values: large, medium, small.