A powerful, unofficial Python wrapper and API for Perplexity AI. This project provides a FastAPI-based server that enables programmatic access to Perplexity's search capabilities, featuring multi-account support, persistent conversations, intelligent request queuing, and access to Perplexity Spaces (Collections).
- Full Perplexity AI Access: Query using various modes (auto, pro, reasoning, deep research) and models (GPT-4o, GPT-4.5, Claude 3.7 Sonnet, Gemini 2.0 Flash, Grok-2, o3-mini, R1, etc.).
- Multi-Account Management: Seamlessly manage and rotate between multiple Perplexity accounts with persistent cookie storage.
- Persistent Conversations:
- Maintain visual threads in your Perplexity history using
frontend_context_uuid. - Continue logical conversation context using
backend_uuid.
- Maintain visual threads in your Perplexity history using
- Streaming Support: Real-time server-sent events (SSE) for token-by-token responses.
- Perplexity Collections (Spaces): List, access, and query within your Collections with full threading support.
- Source Filtering: Restrict searches to Web, Scholar, or Social sources.
- Human-Like Behavior Simulation: Configurable delays, peak hours detection, weekend patterns, burst behavior, and idle periods.
- Priority-Based Queuing: Four priority levels (LOW, NORMAL, HIGH, URGENT) with intelligent processing.
- Automatic Account Rotation: Load balancing and intelligent account selection based on usage patterns.
- Concurrent Request Management: Configurable limits per account with semaphore-based control.
- Real-Time Monitoring: Queue statistics, request tracking, and performance metrics.
- Web Dashboard: Interactive HTML interface for account management and queue monitoring.
- RESTful API: Comprehensive endpoints with OpenAPI documentation at
/docs. - Robust Error Handling: Detailed error reporting with account context.
- Flexible Deployment: Railway, Heroku, and self-hosted support with environment configuration.
-
Clone the repository:
git clone https://github.com/yourusername/perplexity-wrapper.git cd perplexity-wrapper -
Install dependencies:
pip install -r requirements.txt
-
Run the server:
python run_server.py
uv pip install -r requirements.txt
python run_server.py- Dashboard:
http://localhost:8000/ - API Documentation:
http://localhost:8000/docs - Health Check:
http://localhost:8000/health
Extract TNB electricity bill information from PDF files.
Endpoint: POST /api/extract-tnb
Parameters:
file: PDF file (required)account_name: Account to use (optional, default: first available)model: Model to use (optional, default: gemini-3-flash)
Response:
{
"status": "success",
"data": {
"customer_name": "Mak Kian Keong",
"address": "3, Jalan Flora 3F/5, Bandar Rimbayu, 42500 Telok Panglima Garang, Selangor",
"tnb_account": "220012905808",
"bill_date": "25.06.2025",
"state": "Selangor",
"post_code": "42500"
}
}Example:
curl -X POST "http://localhost:8000/api/extract-tnb" \
-F "file=@TNB1.pdf" \
-F "account_name=my_account"Python Example:
import requests
with open('TNB1.pdf', 'rb') as f:
response = requests.post(
'http://localhost:8000/api/extract-tnb',
files={'file': f},
data={'account_name': 'my_account'}
)
result = response.json()
print(result['data'])Extract personal information from MYKAD cards or customer namecards.
Endpoint: POST /api/extract-mykad
Parameters:
file: Image (JPG, JPEG, PNG) or PDF file (required)account_name: Account to use (optional, default: first available)model: Model to use (optional, default: gemini-3-flash)
Response:
{
"status": "success",
"data": {
"name": "John Doe",
"mykad_id": "123456-01-5678",
"address": "123 Street Name, City, Postal Code",
"contact_number": "+60 12-34567890"
}
}Example:
curl -X POST "http://localhost:8000/api/extract-mykad" \
-F "file=@mykad_front.jpg" \
-F "account_name=my_account"Health Check: GET /api/tnb-health or GET /api/mykad-health
Extract cookies from a logged-in Perplexity session using a browser extension like EditThisCookie.
Method: POST /api/account/add
Parameters:
account_name: Unique identifier (e.g., "personal", "work")cookie_data: Raw JSON cookie array from browser extensiondisplay_name: (Optional) Friendly display name
Example:
curl -X POST "http://localhost:8000/api/account/add" \
-F "account_name=my_account" \
-F "cookie_data=@cookies.json" \
-F "display_name=My Account"Endpoint: GET /api/query_sync
Key Parameters:
q: Search query stringaccount_name: Account to use for the querymode: Search mode (auto,pro,reasoning,deep research)model: AI model (e.g.,gpt-4o,claude 3.7 sonnet,r1,o3-mini)sources: Comma-separated sources (web,scholar,social)frontend_context_uuid: UUID for thread persistence (keeps queries in same conversation)backend_uuid: Backend UUID from previous response (maintains conversation context)collection_uuid: (Optional) Search within a specific Collection/Space
Example:
curl "http://localhost:8000/api/query_sync?q=What+is+quantum+computing&account_name=my_account&mode=pro&model=gpt-4o"Endpoint: GET /api/query_async
Response: Server-Sent Events (SSE) stream with token-by-token output
Parameters: Same as query_sync
Example:
curl -N "http://localhost:8000/api/query_async?q=Explain+AI&account_name=my_account&mode=auto"Use the queue system for human-like behavior patterns and automatic rate limiting.
Endpoint: GET /api/query_queue_sync
Additional Parameters:
priority: Request priority (low,normal,high,urgent)timeout: Maximum wait time in seconds (default: 300)
Example:
curl "http://localhost:8000/api/query_queue_sync?q=Research+topic&priority=high&timeout=120"Endpoint: GET /api/query_queue_async
Returns: Request ID for tracking
- List Recent Threads:
GET /api/threads?account_name={account} - Get Thread Details:
GET /api/threads/{slug}?account_name={account} - Delete Thread:
DELETE /api/threads/{thread_uuid}?account_name={account}
- List Collections:
GET /api/collections?account_name={account} - Get Collection Details:
GET /api/collections/{collection_slug}?account_name={account} - List Collection Threads:
GET /api/collections/{collection_slug}/threads?account_name={account}
- Queue Status:
GET /api/queue/status - Start Queue:
POST /api/queue/start - Stop Queue:
POST /api/queue/stop - Get Behavior Settings:
GET /api/queue/settings/behavior - Update Behavior Settings:
POST /api/queue/settings/behavior
Example - Update Queue Settings:
curl -X POST "http://localhost:8000/api/queue/settings/behavior" \
-H "Content-Type: application/json" \
-d '{
"min_delay_seconds": 10.0,
"max_delay_seconds": 30.0,
"peak_hours_start": 9,
"peak_hours_end": 17,
"weekend_factor": 0.3,
"burst_probability": 0.1,
"burst_size": 3,
"idle_probability": 0.05
}'Maintain coherent multi-turn conversations using two UUID systems:
frontend_context_uuid: Visual thread ID - keeps all queries in the same conversation thread in Perplexity UIbackend_uuid: Logical context ID - maintains conversation memory between queries
1. Start a new conversation:
# Generate a UUID for your thread (e.g., using uuidgen or online generator)
THREAD_ID="550e8400-e29b-41d4-a716-446655440000"
curl "http://localhost:8000/api/query_sync?q=What+is+Python&account_name=my_account&frontend_context_uuid=${THREAD_ID}" > response1.json
# Extract backend_uuid from response
BACKEND_UUID=$(jq -r '.backend_uuid' response1.json)2. Continue the conversation:
curl "http://localhost:8000/api/query_sync?q=Show+me+an+example&account_name=my_account&frontend_context_uuid=${THREAD_ID}&backend_uuid=${BACKEND_UUID}" > response2.json3. Keep going:
BACKEND_UUID=$(jq -r '.backend_uuid' response2.json)
curl "http://localhost:8000/api/query_sync?q=Explain+decorators&account_name=my_account&frontend_context_uuid=${THREAD_ID}&backend_uuid=${BACKEND_UUID}"All queries will appear in a single thread in your Perplexity history, maintaining full conversation context.
-
STORAGE_ROOT: Custom storage path for accounts and logs- Default:
/app/storage(production) or local directory - Example:
export STORAGE_ROOT=/path/to/storage
- Default:
-
PORT: Server port (default: 8000) -
HOST: Server host (default: 0.0.0.0)
- Accounts:
accounts.json- Cookie storage with metadata - Logs:
logs/directory - API response logs with timestamps - Format:
API-{account}-{timestamp}-{count}orAPI-QUEUE-{account}-{timestamp}
Configure human-like timing patterns via API or directly in code:
from lib.queue_manager import HumanBehaviorSettings
settings = HumanBehaviorSettings(
min_delay_seconds=5.0, # Minimum delay between requests
max_delay_seconds=20.0, # Maximum delay between requests
peak_hours_start=9, # Peak activity start (9 AM)
peak_hours_end=17, # Peak activity end (5 PM)
weekend_factor=0.3, # 30% activity on weekends
burst_probability=0.1, # 10% chance of burst behavior
burst_size=3, # Up to 3 requests in a burst
idle_probability=0.05 # 5% chance of idle periods
)Railway: Uses railway.toml with NIXPACKS builder
Heroku: Uses Procfile with Python 3.11+
Docker: Standard Python 3.11+ with requirements.txt or uv.lock
perplexity-wrapper/
βββ api/
β βββ main.py # FastAPI app with all core endpoints
β βββ queue_endpoints.py # Queue management endpoints
β βββ config.py # Storage configuration
β βββ utils.py # Response processing utilities
β βββ templates/ # HTML dashboard templates
βββ lib/
β βββ perplexity.py # Perplexity AI client (async)
β βββ cookie_manager.py # Multi-account cookie management
β βββ queue_manager.py # Human-like request queuing
βββ run_server.py # Server entry point
βββ requirements.txt # Pip dependencies
βββ pyproject.toml # Modern Python packaging
βββ uv.lock # UV dependency lock file
FastAPI Application (api/main.py)
- All HTTP endpoints with OpenAPI schema
- CORS middleware for cross-origin requests
- SSE streaming support for real-time responses
- Template rendering for web dashboard
Perplexity Client (lib/perplexity.py)
- Async HTTP client using
curl-cffifor browser impersonation - Chrome 128 user agent spoofing
- Full support for all Perplexity modes and models
- File upload handling with S3 integration
Cookie Manager (lib/cookie_manager.py)
- Chrome extension cookie format conversion
- Persistent JSON storage with metadata
- Account validation and usage tracking
- Async file operations with
aiofiles
Queue Manager (lib/queue_manager.py)
- Priority-based asyncio queues (4 levels)
- Human behavior simulation with configurable patterns
- Account rotation with semaphore-based concurrency control
- Request statistics and monitoring
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is provided as-is for educational and research purposes.
This is an unofficial API wrapper and is not affiliated with Perplexity AI.
Important Usage Notes:
- Use responsibly and in accordance with Perplexity's Terms of Service
- Respect rate limits and avoid aggressive querying
- The queue system helps mimic human behavior patterns
- Account cookies are stored locally - ensure proper security
- This tool is for personal/educational use; commercial use may violate ToS
- API Documentation: Access
/docswhen server is running - TNB Extractor: See TNB_FIX_SUMMARY.md for detailed fix information
- Queue Features: See QUEUE_FEATURES.md
- Development Guide: See AGENTS.md
- Collections Support: See add-custom-space.md