Expand description
The pexels_api crate provides API wrapper for Pexels. It based on Pexels API Documentation.
To get the API key, you have to request the access from Request API Access – Pexels.
This library depends on 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 = "*"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 dotenv::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! Simply 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 940px and a maximum height of 650px. It has the aspect ratio of the original image.
- large2x - This image has a maximum width of 1880px and a maximum height of 1300px. It has the aspect ratio of the original image.
- medium - This image has a height of 350px and a flexible width. It has the aspect ratio of the original image.
- small - This image has a height of 130px and a flexible width. It has the aspect ratio of the original image.
- portrait - This image has a width of 800px and a height of 1200px.
- landscape - This image has a width of 1200px and height of 627px.
- tiny - This image has a width of 280px and height of 200px.
Structs§
- domain module Represents a Pexels collection.
- Represents a request to fetch a list of collections from the Pexels API.
- Builder for constructing a
Collectionsrequest. - 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
Featuredrequest. - Retrieve a specific Photo from its id.
- Builder for
FetchPhoto. - Retrieve a specific video from its ID.
- Builder for
FetchVideo. - Used as an input value for
Color::Hexwhen specifying a hexadecimal color code. - Retrieve a specific media item by its ID. This endpoint returns all media items (photos and videos) within a single collection. Use the
typeparameter to filter results to only photos or only videos. - Builder for
Media. - 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 Fetches the current popular Pexels videos.
- Builder for
Popular. - This endpoint enables you to search Pexels for any topic that you would like. For example, your query could be something broad like Nature, Tigers, People. Or it could be something specific like a Group of people working.
- 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.
- This endpoint allows you to search Pexels for any topic. For example, your query could be broad like `Nature`, `Tigers`, `People`, or specific like `Group of people working`.
- Builder for
Search.
Enums§
- Desired photo color.
- The locale of the search you are performing.
- The order of items in the media collection. Supported values are: asc, desc. Default: asc
- The type of media you are requesting. If not given or if given with an invalid value, all media will be returned. Supported values are photos and videos.
- Desired photo orientation.
- Errors that can occur while interacting with the Pexels API.
- Minimum videos/photo size.