Python Reddit API Wrapper - simple access to Reddit's API
—
The Reddit class serves as the main entry point for all PRAW functionality. It handles authentication, manages API requests, and provides access to all Reddit features through helper objects and direct methods.
Create a Reddit instance with various authentication methods and configuration options.
def __init__(
self,
site_name: str = None,
config_interpolation: str = None,
requestor_class = None,
requestor_kwargs: dict = None,
token_manager = None,
**config_settings
):
"""
Initialize Reddit instance.
Parameters:
- site_name: Configuration site name from praw.ini
- client_id: OAuth client ID
- client_secret: OAuth client secret
- user_agent: User agent string (required)
- username: Reddit username (for script auth)
- password: Reddit password (for script auth)
- refresh_token: OAuth refresh token
- redirect_uri: OAuth redirect URI
- config_interpolation: Configuration interpolation method
- requestor_class: Custom requestor class
- requestor_kwargs: Requestor configuration
- token_manager: Custom token manager
"""Usage Examples:
# Script authentication
reddit = praw.Reddit(
client_id="your_client_id",
client_secret="your_client_secret",
username="your_username",
password="your_password",
user_agent="MyBot v1.0 by /u/username"
)
# Web application authentication
reddit = praw.Reddit(
client_id="your_client_id",
client_secret="your_client_secret",
redirect_uri="http://localhost:8080",
user_agent="MyApp v1.0 by /u/username"
)
# Read-only mode
reddit = praw.Reddit(
client_id="your_client_id",
client_secret="your_client_secret",
user_agent="MyApp v1.0 by /u/username"
)
reddit.read_only = TrueAccess specific Reddit content directly by ID or URL.
def comment(self, id: str | None = None, *, url: str | None = None):
"""
Return a lazy Comment instance.
Parameters:
- id: Comment ID (base36)
- url: Comment URL
Returns:
Comment instance
"""
def submission(self, id: str | None = None, *, url: str | None = None):
"""
Return a lazy Submission instance.
Parameters:
- id: Submission ID (base36)
- url: Submission URL
Returns:
Submission instance
"""
def redditor(self, name: str | None = None, *, fullname: str | None = None):
"""
Return a lazy Redditor instance.
Parameters:
- name: Redditor username
- fullname: Reddit fullname (t2_xxxxx)
Returns:
Redditor instance
"""
def subreddit(self, display_name: str):
"""
Return a Subreddit instance.
Parameters:
- display_name: Subreddit name (without r/ prefix)
Returns:
Subreddit instance
"""Discover random content and check availability.
def random_subreddit(self, *, nsfw: bool = False):
"""
Return a random Subreddit.
Parameters:
- nsfw: Include NSFW subreddits
Returns:
Subreddit instance
"""
def info(self, *, fullnames: Iterable[str] | None = None, subreddits: Iterable[Subreddit | str] | None = None, url: str | None = None):
"""
Fetch info about Reddit objects.
Parameters:
- fullnames: List of Reddit fullnames
- subreddits: List of subreddit names or Subreddit objects
- url: URL to get info about
Returns:
Generator of Reddit objects
"""
def username_available(self, name: str) -> bool:
"""
Check if username is available.
Parameters:
- name: Username to check
Returns:
True if available, False otherwise
"""Access domain-specific content listings.
def domain(self, domain: str):
"""
Return a DomainListing for a domain.
Parameters:
- domain: Domain name (e.g., "reddit.com")
Returns:
DomainListing instance
"""Make direct HTTP requests to Reddit's API.
def get(self, path: str, *, params: str | dict[str, str | int] | None = None):
"""
Make GET request to Reddit API.
Parameters:
- path: API endpoint path
- params: Query parameters
Returns:
API response data
"""
def post(self, path: str, *, data: dict[str, str | Any] | bytes | IO | str | None = None, files: dict[str, IO] | None = None, json: dict[Any, Any] | list[Any] | None = None, params: str | dict[str, str] | None = None):
"""
Make POST request to Reddit API.
Parameters:
- path: API endpoint path
- data: Form data
- files: File uploads
- json: JSON data
- params: Query parameters
Returns:
API response data
"""
def put(self, path: str, *, data: dict[str, str | Any] | bytes | IO | str | None = None, json: dict[Any, Any] | list[Any] | None = None):
"""
Make PUT request to Reddit API.
Parameters:
- path: API endpoint path
- data: Form data
- json: JSON data
Returns:
API response data
"""
def patch(self, path: str, *, data: dict[str, str | Any] | bytes | IO | str | None = None, json: dict[Any, Any] | list[Any] | None = None, params: str | dict[str, str] | None = None):
"""
Make PATCH request to Reddit API.
Parameters:
- path: API endpoint path
- data: Form data
- json: JSON data
- params: Query parameters
Returns:
API response data
"""
def delete(self, path: str, *, data: dict[str, str | Any] | bytes | IO | str | None = None, json: dict[Any, Any] | list[Any] | None = None, params: str | dict[str, str] | None = None):
"""
Make DELETE request to Reddit API.
Parameters:
- path: API endpoint path
- data: Form data
- json: JSON data
- params: Query parameters
Returns:
API response data
"""
def request(self, *, method: str, path: str, params: str | dict[str, str | int] | None = None, data: dict[str, str | Any] | bytes | IO | str | None = None, files: dict[str, IO] | None = None, json: dict[Any, Any] | list[Any] | None = None):
"""
Make generic request to Reddit API.
Parameters:
- method: HTTP method
- path: API endpoint path
- params: Query parameters
- data: Form data
- files: File uploads
- json: JSON data
Returns:
API response data
"""Control client behavior and access helper objects.
@property
def read_only(self) -> bool:
"""Whether client is in read-only mode."""
@read_only.setter
def read_only(self, value: bool):
"""Set read-only mode."""
@property
def validate_on_submit(self) -> bool:
"""Whether to validate submissions (deprecated)."""
@validate_on_submit.setter
def validate_on_submit(self, value: bool):
"""Set validation on submit (deprecated)."""Access specialized helper objects for different Reddit functionality areas.
auth: Auth # Authentication management
front: Front # Front page listings
inbox: Inbox # User inbox functionality
user: User # Current user operations
subreddits: Subreddits # Subreddit discovery
redditors: Redditors # Redditor discovery
drafts: DraftHelper # Draft management
live: LiveHelper # Live thread management
multireddit: MultiredditHelper # Multireddit management
notes: RedditModNotes # Moderator notesPRAW can be configured through praw.ini files, environment variables, or constructor parameters. The configuration system supports multiple sites and environments.
Configuration File (praw.ini):
[DEFAULT]
user_agent=MyBot v1.0 by /u/username
[my_bot]
client_id=your_client_id
client_secret=your_client_secret
username=your_username
password=your_passwordEnvironment Variables:
praw_client_idpraw_client_secretpraw_usernamepraw_passwordpraw_user_agentclass Config:
"""PRAW configuration management."""
CONFIG_NOT_SET: str # Sentinel for unset config valuesInstall with Tessl CLI
npx tessl i tessl/pypi-praw