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 = "*"
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§
- Collection
- domain module Represents a Pexels collection.
- Collections
- Represents a request to fetch a list of collections from the Pexels API.
- Collections
Builder - Builder for constructing a
Collections
request. - Collections
Response - Represents the response for a list of collections.
- Curated
- photos module This endpoint enables you to receive real-time photos curated by the Pexels team.
- Curated
Builder - Builder for
Curated
. - Featured
- collections module Represents a request to fetch all featured collections from the Pexels API.
- Featured
Builder - Builder for constructing a
Featured
request. - Fetch
Photo - Retrieve a specific Photo from its id.
- Fetch
Photo Builder - Builder for
FetchPhoto
. - Fetch
Video - Represents a request to fetch a specific video by its ID from the Pexels API.
- Fetch
Video Builder - Builder for
FetchVideo
. - Hex
- Represents a hexadecimal color code.
Used as an input value for
Color::Hex
when specifying a hexadecimal color code. - Media
- 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. - Media
Builder - Builder for constructing a
Media
request. - Media
Response - Represents the response for a list of media items.
- Pexels
- Client for interacting with the Pexels API
- Photo
- Represents a Pexels photo.
- Photo
Src - Represents different image sizes for a photo.
- Photos
Response - Represents the response for a list of photos.
- Popular
- videos module Represents a request for popular videos from the Pexels API.
- Popular
Builder - Builder for
Popular
. - Search
- Represents a search query to the Pexels API.
- Search
Builder - Builder for
Search
. - User
- Represents a user who created a media item.
- Video
- Represents a Pexels video.
- Video
File - Represents a video file with different qualities.
- Video
Picture - Represents a preview picture of a video.
- Video
Response - Represents the response for a list of videos.
- Video
Search - Represents a search request to the Pexels API for videos.
- Video
Search Builder - Builder for
Search
.
Enums§
- Color
- Represents the desired photo color.
- Locale
- 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
. - Media
Sort - Specifies the order of items in the media collection.
Supported values:
asc
,desc
. Default:asc
. - Media
Type - Specifies the type of media to request.
If not provided or invalid, all media types will be returned.
Supported values:
photos
,videos
. - Orientation
- Desired photo orientation.
Supported values:
landscape
,portrait
,square
. Default:landscape
. - Pexels
Error - 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.
- Size
- Specifies the minimum size for videos or photos.
Supported values:
large
,medium
,small
.