Get Started with the Reddit API
Follow step-by-step instructions to configure the Reddit API tokens.
Overview
In this lesson, we'll set up the Reddit API tokens. You should sign up and create a new Reddit account in order to use the API.
Note: Using the API can occasionally result in your account being flagged as a bot. We strongly recommend not using your personal account and continue the course by creating a new Reddit account.
Sign up
We need to perform the following steps to complete the signup process:
Enter a valid email address and click “Continue.” It will take you to the next page, where you must enter the “Username” and “Password” before selecting the “Sign up” option.
Select how you identify yourself. You can also skip this option if you want.
After that, you will get an option to select at least three interests so that Reddit can recommend newsfeeds according to these interests.
Now, it’s time for you to select the communities you’d like to join.
Next, you will select your account avatar.
In the end, it will redirect to the greeting page—the account creation process is complete.
Get the API tokens
Once you’ve logged in to your Reddit account, go to this Reddit App's webpage and follow the steps below.
Click the “are you a developer? create an app...” button.
In the fields, add the following information.
Add the name of your application.
Select the “web app” option.
Add the “description” (optional).
Skip the “about url”.
Add the URL in the widget below in the “redirect url” field.
{{EDUCATIVE_LIVE_VM_URL}}
Click the “create app” button.
Once the application has been created, copy the “developers” name, the “secret,” and the client ID tokens into the widget shown in the next section. These will be called USERNAME, CLIENT_SECRET, and CLIENT_ID, respectively, throughout the course.
Request an access token from Reddit
The access token is a string of characters used to authenticate a user without a password. We can use the token to make API calls on behalf of the user, which in this case is going to be ourselves. To obtain the access token, use the following endpoint:
https://www.reddit.com/api/v1/access_token
Furthermore, the received access token will temporarily last for 24 hours. After it expires, we can either refresh the same access token or make a new one by calling the same endpoint. We use the refresh token to renew the access token.
Request parameters
Some important (required) parameters that we'll use to call the endpoint are as follows:
Parameter | Type | Category | Description |
| string | required | This refers to the way an application gets an access token. Since we want to use OAuth to get the access token, we'll set the value of |
| string | required | This is the username you use to log into Reddit. |
| string | required | This is a one-time use code that may be exchanged for a bearer token. |
| string | required | This is a string of random characters used to authenticate a user. |
| string | required | This is a string of random characters used to authenticate a user. |
| string | required | This is used to identify the source of a network request. You need a unique and descriptive |
Next, add your username and tokens to the widget below and press "Run" to start the express server.
{ "name": "lesson1auth", "type": "module", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "buffer": "^6.0.3", "express": "latest", "node-fetch": "^3.2.10", "path": "^0.12.7", "querystring": "^0.2.1", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "6.3.0", "react-scripts": "5.0.1", "web-vitals": "^2.1.4", "zlib": "^1.0.5" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": [ "react-app", "react-app/jest" ] }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } }
Follow these instructions to get access and refresh the token.
Click the hyperlink reflecting on the widget to open Reddit's permission page in a new tab.
After you press the "Allow" button, you will see two tokens on the screen.
Copy the access and refresh tokens to the widget below to continue the course.
The code that is used to get the access token is explained below:
Lines 1–2: We import the required modules.
Line 10: We encode the redirect URI to use it in the
URL
. We made the URL that would take us to the permissions page of Reddit in line 35.Lines 12–28: We make and send a request to the
https://www.reddit.com/api/v1/access_token
endpoint with the value of thecode
in the request's body. We receive a refresh and an access token in response to this request.Line 42: We redirect to the
URL
of the Reddit page to request the code from Reddit's permission page.
Verify your access token
Add the refresh and access tokens in the code widget below. Click "Run" to verify them.
verifyTokens();
We can start making API calls now that we’ve saved our tokens.