Examples

These example shows how the interaction with the paging functionality of the Pexels API is greatly abstracted and simplified.

Random

The code below will return random images. Use the parameter per_page to specify how many random images the iterator will allow.

from pypexels import PyPexels
api_key = 'YOUR_API_KEY'

# instantiate PyPexels object
py_pexel = PyPexels(api_key=api_key)

random_photos_page = py_pexel.random(per_page=3)
for photo in random_photos_page.entries:
    print(photo.id, photo.photographer, photo.url)

Single Photo

The code below will return an image object. Use the parameter photo_id to specify the ID of the image.

from pypexels import PyPexels
api_key = 'YOUR_API_KEY'

# instantiate PyPexels object
py_pexel = PyPexels(api_key=api_key)

# access single photo by its ID
photo = py_pexel.single_photo(photo_id=415071)
print(photo.id, photo.photographer, photo.url)

Search Videos

The code below will perform a search of videos based on a query string, then iterate through all the results pages of 40 videos each, retrieve each video in there, and print some of their attributes.

from pypexels import PyPexels
api_key = 'YOUR_API_KEY'

# instantiate PyPexels object
py_pexel = PyPexels(api_key=api_key)

search_videos_page = py_pexel.videos_search(query="red+flower", per_page=40)
while True:
    for video in search_videos_page.entries:
        print(video.id, video.user.get('name'), video.url)
    if not search_videos_page.has_next:
        break
    search_videos_page = search_videos_page.get_next_page()

Single Video

The code below will return a video object. Use the parameter video_id to specify the ID of the video.

from pypexels import PyPexels
api_key = 'YOUR_API_KEY'

# instantiate PyPexels object
py_pexel = PyPexels(api_key=api_key)

# access single video by its ID
video = py_pexel.single_video(video_id=1448735)
print(video.id, video.user.get('name'), video.url)