How to get data from Reddit Ads into Google Sheets

In my last post, I showed you how to integrate the Reddit API with Google Sheets.

In this tutorial, I’ll show you how to connect the Reddit Ads API to Google Sheets in 6 steps:

  1. Install the Apipheny Add-on
  2. Create a Reddit API App
  3. Obtain Reddit API Access & Refresh Tokens
  4. Choose your Reddit API Endpoint
  5. Enter Reddit Ads API request into Apipheny
  6. Run the Reddit Ads API request

Note: the Reddit Ads API is available only for whitelisted accounts. To be whitelisted to access the Reddit Ads API, contact your Reddit sales representative. If you don’t have a Reddit sales rep but want to request one, contact their sales team by clicking the “Advertising at Scale” button on this page.

Step 1.) Install and open the Apipheny add-on for Google Sheets

Apipheny is an API connector for Google Sheets. You can use Apipheny to connect your Google Sheets to unlimited API data sources, make unlimited API requests, and more. There is a 30 day free trial included.

1.) Install Apipheny by opening the following link on desktop and then clicking the Install button in the Google Marketplace: https://apipheny.io/install

2.) After you’ve installed Apipheny, open a Google Sheet and then click on the Extensions dropdown in the menu at the top.

In the dropdown list you should see Apipheny. Click Apipheny then click Import API to open the Apipheny sidebar in your Google Sheet.

open apipheny

Tip: you can open a new Google Sheet by entering this URL in your browser: sheet.new

Step 2.) Create a Reddit API App

To create an “app” that’s able to access the Reddit Ads API, first go to https://www.reddit.com/prefs/apps/ and click the “are you a developer? create an app” button:

Create an app for the Reddit Ads API
Create an app for the Reddit Ads API

On the next page:

  • fill in the application name
  • choose the script type for your app
  • add a short optional description
  • add a redirect URL

Then after filling in those fields, click the create app button:

Fill in the app details and then click "create app"
Fill in the app details and then click “create app”

You can use any valid link as the redirect URL, including https://apipheny.io/

Your app will then be created:

Reddit API apps dashboard
Reddit API apps dashboard

On the dashboard for your new app, you will see the app ID and secret. Copy these values to a safe location because you will need them in the upcoming steps.

Note: the “app ID” is the value under “personal use script” in the screenshot above.

Step 3.) Obtain Reddit API Access and Refresh Tokens

Next, give your developer application access to your account by visiting this URL in your browser:

https://www.reddit.com/api/v1/authorize?client_id=CLIENT_ID&response_type=code&state=RANDOM_STRING&redirect_uri=REDIRECT_URL&duration=DURATION&scope=SCOPE_STRING

On that link, replace the various identifiers with the following values:

  • CLIENT_ID – This should be the client ID that you obtained earlier, while creating your developer application
  • RANDOM_STRING – This can be any random string, it will end up being passed to your redirect URL in the following steps
  • REDIRECT_URL – This must be the redirect URL that you entered while creating your developer application
  • DURATION – You should set this to “permanent”. It’s also valid to set it to “temporary”, but in this case, your access token will only be valid for one hour and you will not be able to refresh it
  • SCOPE_STRING – This is a comma separated list of scopes that you want your access token to have. These scopes dictate which actions may be performed with the access token. Each endpoint’s documentation lists the scopes required by that endpoint. You may set this “adsread,history” if you would like your token to access everything in the ads API.

After filling in those details, click the “Allow” button:

Connecting a Reddit Developer App with a Reddit Account
Connecting a Reddit Developer App with a Reddit Account

You will now then be redirected to the redirect URL that you provided earlier. Two query parameters (state and code) will be added in your browser’s address bar, as parameters of this redirect URL. The code parameter is a single use login code that we will now use to acquire a refreshable access token:

Redirect URL with added parameters
Redirect URL with added parameters

Next, open up a command prompt window for Windows OS or a terminal for Mac OS and compose the following curl command:

 curl -X POST https://www.reddit.com/api/v1/access_token \\
   -H 'content-type: application/x-www-form-urlencoded' \\
   -A 'CLIENT_NAME' \\
   -u CLIENT_ID:APP_SECRET \\
   -d 'grant_type=authorization_code&code=CODE&redirect_uri=REDIRECT_URL'

You’ll need to fill in a few parts:

  • CLIENT_NAME – This should be the name of the application using the access token
  • CLIENT_ID – This should be the client ID that you obtained earlier, while creating your developer application
  • APP_SECRET – This should be the application secret, that you obtained earlier while creating your developer application
  • CODE – This should be the value of the code query parameter from the previous step
  • REDIRECT_URL – This must be the redirect URL that you entered while creating your developer application
Note: For Mac OS you may need to use single quotes ( ' ) instead of double quotes ( " ).
curl command

The response access_token may be used in order to access the ads API for up to one hour. The refresh_token may be used to get a new access token when that hour is up. Refreshing the access token can be done with the following command:

 curl -X POST https://www.reddit.com/api/v1/access_token \\
   -A 'CLIENT_NAME' \\
   -u CLIENT_ID:APP_SECRET \\
   -d 'grant_type=refresh_token&refresh_token=REFRESH_TOKEN'

CLIENT_NAME, CLIENT_ID and APP_SECRET are the same as described before. REFRESH_TOKEN should be replaced with the value of the refresh_token field from the previous command’s response.

command to refresh the access token
command to refresh the access token
Note: If the token/key that you generate has an expiration time, you will need to complete this same process again to get a new token when the old one expires.

Step 4.) Choose a Reddit API Endpoint URL

In this section, I’ll show you how to browse the Reddit Ads API documentation to find an API endpoint URL that retrieves the specific information you need from your Reddit Ads account. If you already know your API URL, or you want to use the same example URL as us, just skip to Step 5.

First, open the Reddit Ads API documentation page: https://ads-api.reddit.com/docs/

The menu on the left contains general information about Reddit Ads and a list of grouped API endpoints:

Reddit Ads API Endpoints
Reddit Ads API Endpoints

An example endpoint group is Ad Groups. This endpoint group contains two endpoints: Get all groups belonging to an advertiser and Get an ad group by ID.

In this case, we will describe the Get an ad group by ID endpoint, that retrieves an ad group by its ID:

https://ads-api.reddit.com/api/v2.0/accounts/account_id/ad_groups/ad_group_id

The documentation for this endpoint contains a short description of the endpoint’s purpose, the authorizations used by the endpoint, the accepted path parameters, and the responses it can return:

Get an ad group by ID endpoint
Get an ad group by ID endpoint documentation

All API calls to the Reddit Ads API should be made using the https://ads-api.reddit.com/api/v2.0 base domain. All endpoints and parameters would be appended to this base domain.

Reddit Ads API base domain/root URL:
https://ads-api.reddit.com/api/v2.0

Step 5.) Enter the Reddit Ads API Request into the Apipheny add-on

Okay, we’re in the home stretch. Now go back to your Google Sheet and make sure that the Apipheny add-on is open on the Import tab. With the Import Tab open, enter these details into the add-on:

Method: At the top of the Apipheny sidebar, select the HTTP method (GET or POST) as required by your Reddit Ads API endpoint. For this example, we are using the GET method.

API URL: In Step 4, I explained how you can find the Reddit Ads API endpoint that you need. Now copy the complete API URL into the Apipheny add-on, where it says API URL Path (JSON / CSV), followed by any GET parameters required for your query.

For this example, we are using the Get an account by ID endpoint, that gets information about an account by its ID. The corresponding URL for this endpoint is:

https://ads-api.reddit.com/api/v2.0/accounts/account_id

The account_id parameter in this API URL is the account ID that is obtained by accessing the following link in your browser: https://www.reddit.com/user/your_username/about.json

Replace your_username with your Reddit username.

In the Headers of the Apipheny add-on section, add two rows, with the following keys and values:

Header 1
Key:
Content-Type
Value:
application/json
Header 2
Key:
Authorization
Value:
Bearer your_token

You can copy the first header exactly. In the second header, your_token is the value of the token you obtained previously, in Step 3. There should be a literal space between Bearer and your_token.

Reddit Ads API Request Entered into the Apipheny Google Sheets add-on
Reddit Ads API Request Entered into the Apipheny Google Sheets add-on

Step 6.) Run the Reddit Ads API Request in your Google Sheets

The last step is to click the Run button at the bottom of the Apipheny add-on and then your Reddit Ads data will be imported into your Google Sheet.

Reddit Ads data imported into my Google Sheet using the Apipheny add-on
Reddit Ads data imported into my Google Sheet using the Apipheny add-on

After making a successful request to the Reddit Ads API, try querying a different Reddit Ads API endpoint, or try using one of the more advanced features in the Apipheny add-on.


Crypto API Tutorials:


API Tutorials


API Knowledge

What is an API?

What is an API URL?

What are parameters?

What is an endpoint?

What is an API key/token?

What is basic authentication?

What are headers?

What is a GET request?

What is a POST request?