How to Integrate MoneyPrinterTurbo with Pexels API: Complete Configuration Guide

MoneyPrinterTurbo provides native Pexels API integration through TOML configuration, the search_videos_pexels service function, and a Streamlit web interface, supporting automatic key rotation and multiple video orientations.

MoneyPrinterTurbo (MPT) is an open-source automated video generation framework that fetches stock footage from external providers. Integrating it with the Pexels video API involves configuring your credentials in config.toml, utilizing the service layer in app/services/material.py, and optionally managing keys through the built-in web UI to enable automatic video retrieval for your projects.

Configuring Pexels API Keys in config.toml

Start by adding your Pexels API credentials to the configuration file. The system supports both single keys and lists for load balancing.

According to the repository's config.example.toml, locate the [app] section:

[app]

# Select "pexels", "pixabay", or "local" as the video source

video_source = "pexels"

# Single key or list for rotation (comma-separated if using UI)

pexels_api_keys = ["YOUR_PEXELS_KEY_1", "YOUR_PEXELS_KEY_2"]

Single Key vs. Key Rotation

If you provide a string, the system uses it directly for every request. If you provide a list, the get_api_key function in app/services/material.py automatically rotates through them using a modulo operation based on the global requested_count variable.

Implementing the Service Layer Integration

The core Pexels integration resides in app/services/material.py. The search_videos_pexels function handles authentication, API requests, and response parsing.

The search_videos_pexels Function

This function performs four critical operations:

  1. Key Selection: Calls get_api_key("pexels_api_keys") to retrieve credentials from config
  2. Aspect Mapping: Converts the internal VideoAspect enum (defined in app/models/schema.py) to Pexels-compatible orientation strings (portrait or landscape)
  3. API Request: Builds a GET request to https://api.pexels.com/videos/search with query parameters including per_page=20 and the mapped orientation
  4. Result Filtering: Filters videos by minimum duration and exact resolution matching the requested aspect ratio, wrapping valid results in MaterialInfo objects with provider="pexels"
from app.services.material import search_videos_pexels
from app.models.schema import VideoAspect

videos = search_videos_pexels(
    search_term="city skyline",
    minimum_duration=3,
    video_aspect=VideoAspect.portrait
)

for video in videos:
    print(video.url, video.duration)

Aspect Ratio Resolution Mapping

The VideoAspect enum in app/models/schema.py maps abstract aspect ratios to concrete resolutions. When you specify video_aspect="portrait", the system translates this to specific width and height values (e.g., 1080x1920) to filter Pexels results by exact resolution.

Managing Credentials Through the Streamlit UI

For non-technical users, the web interface in webui/Main.py (lines 69-74) provides a secure input panel that abstracts configuration file editing.

The UI exposes a password-protected text field:

pexels_api_key = get_keys_from_config("pexels_api_keys")
pexels_api_key = st.text_input(
    tr("Pexels API Key"), 
    value=pexels_api_key, 
    type="password"
)
save_keys_to_config("pexels_api_keys", pexels_api_key)

Users can enter comma-separated keys, which the UI sanitizes and persists back to the configuration storage.

Orchestrating Video Downloads

For end-to-end automation, use the download_videos function (lines 97-161 in material.py). This high-level wrapper handles searching, downloading, and local file storage.

from app.services.material import download_videos

video_paths = download_videos(
    task_id="project_001",
    search_terms=["ocean", "waves", "sunset"],
    source="pexels",
    video_aspect="landscape",
    audio_duration=60.0,
    max_clip_duration=5
)

When source="pexels", the function automatically invokes search_videos_pexels, filters results by your audio duration requirements, and saves matching clips via the internal save_video helper, returning a list of local file paths.

Mitigating Rate Limits with Key Rotation

Pexels imposes rate limits on individual API keys. MoneyPrinterTurbo implements automatic load balancing through the get_api_key function's rotation logic:

global requested_count
requested_count += 1
return api_keys[requested_count % len(api_keys)]

By configuring multiple keys in pexels_api_keys, requests distribute across your key pool using a round-robin algorithm. This prevents single-key throttling during high-volume video generation tasks that require dozens of search queries.

Summary

  • Configuration: Store Pexels credentials in config.toml under pexels_api_keys as a string or list, with video_source = "pexels"
  • Service Layer: Import search_videos_pexels from app/services/material.py for direct API access with automatic aspect ratio mapping via VideoAspect
  • UI Management: Use the Streamlit interface in webui/Main.py to securely input and save API keys without manually editing configuration files
  • Orchestration: Call download_videos with source="pexels" to automate the complete fetch-and-save workflow
  • Scalability: Add multiple API keys to enable automatic rotation via get_api_key and avoid hitting Pexels rate limits

Frequently Asked Questions

Where do I enter my Pexels API key in MoneyPrinterTurbo?

You have two options: Edit the config.toml file directly and add your key to the pexels_api_keys field under the [app] section, or use the Streamlit web interface at webui/Main.py which provides a secure password input field that saves to your configuration automatically.

Can I use multiple Pexels API keys simultaneously?

Yes. Configure pexels_api_keys as a list in config.toml (e.g., ["key1", "key2"]). The get_api_key function in app/services/material.py automatically rotates through available keys using a round-robin algorithm (requested_count % len(api_keys)) to distribute API quota across your key pool.

What video orientations does MoneyPrinterTurbo support with Pexels?

The integration supports portrait, landscape, and square orientations through the VideoAspect enum in app/models/schema.py. The system maps these internal values to Pexels API parameters (portrait or landscape) and filters results by exact resolution to match your project's aspect ratio requirements.

How do I switch from Pexels to another video source?

Change the video_source value in config.toml from "pexels" to "pixabay" or "local". When set to "local", the system bypasses API calls and uses video files from your specified local directory instead of fetching from Pexels.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →