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:
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.
To pull data from the Reddit Ads API to Google Sheets easily and without writing any code, first install and open the Apipheny Google Sheets add-on.
Apipheny is a no-code API integrator for Google Sheets that you can use to make unlimited API requests, connect to unlimited APIs (including the Reddit Ads API), save API requests, schedule API requests, and more. Click here if you want to learn more about Apipheny.
1. Install Apipheny by clicking the Install button on this page.
2. After you’ve installed Apipheny, open a Google Sheet and then click on Add-Ons option in the menu at the top. In the add-ons dropdown menu, you should see Apipheny. Click Apipheny > Import API to open the Apipheny add-on in your Google Sheet.
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:
On the next page:
Then after filling in those fields, click the create app button:
You can use any valid link as the redirect URL, including https://apipheny.io/
Your app will then be created:
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.
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 page, replace the various identifiers with the following values:
After filling in those details, click the “Allow” button:
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:
Next, open up a terminal 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:
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.
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:
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:
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
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. Do not include the brackets “{}” around any of the values.
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>. Do not include the carrots “<>” around your token.
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.
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, such as:
If you want to integrate your Reddit Ads data with Google Data Studio, just follow the same steps above and then connect your Google Sheet to your Google Data Studio report.
Meelad Mashaw