How to Post on Reddit API with Python (PRAW example)

In this post, we will see how to post to a subreddit using the PRAW wrapper for the Reddit API.

For this tutorial you will need:

  1. Have Python Installed
  2. Have your Reddit API credentials

If you know nothing about Python, make sure that you start by reading the complete guide on Python for SEO.


Subscribe to my Newsletter


Be Smart and Read this Important Note

I love Reddit and I don’t want you to spam my favourite subs.

If you have never posted to Reddit before, it is not the right way to start by automating it with the API. The community will ban you.

That being said.

I created a subreddit to help SEOs learn how to make bots in a safe environment, without spamming Reddit or getting you banned by moderators.

I give you full permission to use this subreddit to make your tests, please use it.


https://www.reddit.com/r/pythonsandlot


Getting Started

The first need that you will need to do is to install and load the required packages: praw, json and requests.

Open the Terminal and type in:

$ pip install praw
$ pip install json
$ pip install requests

In your python script, load the packages

import json
import praw
import requests

Read the Credential File

Here, we have created the client_secrets.json file in the previous post “Get Reddit API Credentials with PRAW“.

We need to read the file.

credentials = 'client_secrets.json'

with open(credentials) as f:
    creds = json.load(f)

Authenticate the Reddit API

To authenticate with PRAW, we will use the praw.Reddit() function.

Add the parameters from the creds variable.

reddit = praw.Reddit(client_id=creds['client_id'],
                     client_secret=creds['client_secret'],
                     user_agent=creds['user_agent'],
                     redirect_uri=creds['redirect_uri'],
                     refresh_token=creds['refresh_token'])

Make Your Post to a Subreddit

Now the interesting stuff.

Let’s make a simple text post to the Subreddit that I created.

subr = 'pythonsandlot' # Choose your subreddit

subreddit = reddit.subreddit(subr) # Initialize the subreddit to a variable

title = 'Just Made My first Post on Reddit Using Python.'

selftext = '''
I am learning how to use the Reddit API with Python using the PRAW wrapper.
By following the tutorial on https://www.jcchouinard.com/post-on-reddit-api-with-python-praw/
This post was uploaded from my Python Script
'''

Run the Function

subreddit.submit(title,selftext=selftext)

Success!

Full Code

import json
import praw
import requests

subr = 'pythonsandlot'
credentials = 'client_secrets.json'

with open(credentials) as f:
    creds = json.load(f)

reddit = praw.Reddit(client_id=creds['client_id'],
                     client_secret=creds['client_secret'],
                     user_agent=creds['user_agent'],
                     redirect_uri=creds['redirect_uri'],
                     refresh_token=creds['refresh_token'])

subreddit = reddit.subreddit(subr)

title = 'Just Made My first Post on Reddit Using Python.'
selftext = '''
I am learning how to use the Reddit API with Python using the PRAW wrapper.
By following the tutorial on https://www.jcchouinard.com/post-on-reddit-with-python-praw/
This post was uploaded from my Python Script
'''

subreddit.submit(title,selftext=selftext)

What’s Next?

What is an API?

Get Top Posts From Subreddit With Reddit API and Python

Reddit API JSON’s Documentation

How to use Reddit API With Python (Pushshift)

Get Reddit API Credentials with PRAW (Authentication)

Post on Reddit API With Python (PRAW)

Show Random Reddit Post in Terminal With Python

This is it, you now have posted your first post on a subreddit using the Reddit API and Python.

5/5 - (1 vote)