Adding SerpApi to a Zap in Zapier

Introduction

There are numerous use cases for integrating a SerpApi API into a Zap. This article provides a building block to help you avoid pitfalls when connecting the two systems without direct integration. The goal is to get you up and running with a basic setup you can customize and mold into your existing workflows.

Objective

In this article, we will build a Zap that triggers weekly to fetch organic results from SerpApi’s Google Search API. We will then parse the results of our query to find a website's ranking (how far down the list of organic results it is).

As an example, we will use Peet’s coffee to determine where it ranks for the query “coffee” in Portland, Oregon.

Requirements

Understanding SerpApi and Zapier

SerpApi

SerpApi provides APIs for extracting structured JSON data from search engine results pages (SERPs) from search engines such as Google, Bing, Yahoo, and Walmart.

These APIs enable users to gather search data for various purposes, such as market research, competition analysis, and training AI models. SerpApi handles the complexities of processing and maintaining parsers for different search engines, allowing developers to focus on using the data rather than scraping it themselves.

Integrating SerpApi with Zapier offers several benefits:

  1. Automated data collection: Zaps can regularly fetch search data without manual intervention.
  2. Streamlined workflows: Easily connect search data to other apps and processes in your business ecosystem.
  3. Real-time data: SerpApi performs a live web scrape each time a search is performed, providing access to real-time search data.
  4. Enhanced analytics: Automatically populate spreadsheets or dashboards with fresh search data.

Zapier

Zapier is a web-based automation platform that connects different apps and services to create workflows called “Zaps” (a collection of actions and a trigger).

Here’s a brief explanation of how Zaps function:

  1. Triggers: These are events that start a Zap. For example, "When a new email arrives in Gmail" or "When an issue is created in GitHub."
  2. Actions: These are tasks that Zapier performs in response to a trigger. For instance, "Create a new task in Asana" or "Send a Slack message."
  3. Data Mapping: Map data between the trigger and actions.
  4. Filters and Formatting: Users can add conditions to determine when a Zap should run and format data as needed between apps.

For this post, we will keep it simple, using only two actions:

  • Code by Zapier - This action allows us to run either JavaScript (using the latest version of NodeJs) or Python (using Python3). We will use JavaScript for this guide, but the same basic concepts apply to Python.
💡
While having a basic understanding of either of these languages is beneficial, code will be provided and explained to get you started with minor changes.
  • Google Sheets: Create Spreadsheet Row - Zapier has a robust Google Sheets integration that can perform various actions in a Sheet, such as looking up, updating, and creating rows or cells. For this post, we will create a row in an existing Sheet.

Trigger Step:

  • Schedule by Zapier: Schedules the Zap to run at a set interval (hourly, weekly, monthly).

Creating Your First Zap with SerpApi

Before we get started, please ensure you have active SerpApi and Zapier accounts. If you do not, please refer to the requirements section before continuing.

SerpApi API Key and Playground

While not needed yet, be sure to have access to your SerpApi key. You can find or regenerate your API_KEY here: https://serpapi.com/manage-api-key.

Additionally, now might be a good time to run some tests in the SerpApi playground to familiarize yourself with SerpApi. The playground is a live and interactive demo environment that allows you to dial in your searches before implementing them.

While many parameters can be used with the Google Search API to fine-tune your results, for our use case outlined in the objective, we will want to set the following parameters:

  • location - Portland, Oregon, United States
    • Defines where we want the search to originate. SerpApi will pick the most popular one if several locations match the requested location. For more precise control, head to the /locations.json API.
  • q - coffee
    • Defines the query you want to search. You can use anything that you would use in a regular Google search. e.g. inurl:site:intitle:.

Here’s an example of the above in the SerpApi playground: https://serpapi.com/playground?q=coffee&location=Portland%2C+Oregon%2C+United+States&gl=us&hl=en.

Creating a Zap

Once you have logged into Zapier from the homepage, click Create > Zap in the top section of the left panel.

💡
If you have an existing Zap that you would like to add SerpApi to, you can skip this section.
Creating a Zap in Zapier

This will take us to the Zap draft editor, where we will be presented with our triggers and actions.

Start by clicking the “Trigger” at the top of the Zap and selecting the “Schedule by Zapier” trigger from the Popular built-in tools section on the model's right side.

From the App & Event tab of the Schedule trigger, set a frequency for the Zap to run. I've selected a weekly event that triggers Mondays at 6:00 a.m., but feel free to configure these settings to your use case.

If you’re following my example case, your Trigger should look something like this:

Schedtriggergger in Zapier

Click Continue to proceed to the Test tab. Zapier requires you to test each step to ensure it’s working correctly and that any needed test data is generated. Since this is a scheduled trigger, there isn’t much to test, but we will be able to see what data is output.

After completing the test, select Continue with selected record to move on to our action step.

Configuring the API Call

Now that we have configured a trigger, we can move on to the fun stuff—actions!

Zapier provides a built-in tool called “Code by Zapier” that we will utilize to run Node.js code within our Zap. This enables us to fetch from SerpApi and transform the data returned to make it easier to use in subsequent actions.

After selecting Continue with selected record in the previous section, a model will appear to choose an action. If the model does not appear, click the placeholder action in your Zap or the plus (+) sign under the trigger.

Select the “Code by Zapier” action from the Popular built-in tools section on the model's right side.

For the event, we want to choose Run Javascript and click Continue. Feel free to use Python instead if that’s what you’re more comfortable with, but please note that the code provided in this article will all be Javascript.

Our code will aim to accomplish the following:

  • Fetch Google Search Results from SerpApi’s Google Search API
  • Parse the organic results returned from the search to determine the rank of our link
  • Output the data we want to send to Google Sheets

Input Data

Zapier allows us to pass data into our code step using Input Data fields. While we could skip this step, utilizing this feature will make future changes to our query easier.

I recommend adding the following Input Data fields and values:

  • API_KEY - Your SerpApi key
  • QUERY - coffee
  • LOCATION - Portland, Oregon, United States
  • LINK_TO_MATCH - peets.com
Input data fields for code action in Zapier

Moving on to the code we'll first need to destructor the inputData configured in the previous section:

const { API_KEY, QUERY, LOCATION, LINK_TO_MATCH } = inputData;

Next, we will add a function that calls SerpApi's Google Search API with the input data provided by Zapier using fetch, and return JSON data:

// Fetch Google Search Results from SerpApi
async function fetchGoogleSearchResults(query, location) {
  try {
    const response = await fetch(
      `https://serpapi.com/search.json?engine=google&q=${encodeURIComponent(
        query
      )}&location=${encodeURIComponent(
        location
      )}&gl=us&hl=en&api_key=${API_KEY}`
    );

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json();
    return data;
  } catch (error) {
    console.error("There was a problem with the fetch operation:", error);
  }
}

Now that we have data, we need to be able to parse the data to determine if our LINK_TO_MATCH exists in the organic search results. For this, we can use a simple for loop that iterates through the organic_results array, checking the link attribute of each element for our link to match.

SerpApi returns the organic results in the order that they appear on the Google Search page, so we can simply return the first organic result that appears in the array:

// Find the first organic result that contains the link to match
function findMatchingLink(searchResults, linkMatch) {
  if (!searchResults || !searchResults.organic_results) {
    return null;
  }

  for (let i = 0; i < searchResults.organic_results.length; i++) {
    if (searchResults.organic_results[i].link.includes(linkMatch)) {
      return searchResults.organic_results[i];
    }
  }

  return null;
}

Finally, we can pull everything together and output the data in our Google Sheets. To output data from a Javascript code action in Zapier, we return a single output variable, which can be an object or array of objects:

// Fetch data from SerpApi
const searchResults = await fetchGoogleSearchResults(QUERY, LOCATION);

// Store our searchId for troubleshooting
const searchId = searchResults.search_metadata.id;

// Parse the results, returning the first result with our LINK_TO_MATCH
const matchingResult = findMatchingLink(searchResults, LINK_TO_MATCH);

// Output object to be used in the next step
output = {
  matchingResult: matchingResult || "No Organic Result Found",
  query: QUERY,
  location: LOCATION,
  linkToMatch: LINK_TO_MATCH,
  inspectSearchLink: `https://serpapi.com/searches/${searchId}/inspect`,
};
💡
To copy the code in its entirety, please see this GitHub gist: link

Testing

After adding the code to the Zapier action, we must test it and generate data to map in our Google Sheets action later.

Click Continue to navigate to the Test step of our action, then click the Test Step button. Zapier will run our code and display the results from the output object we returned in the previous section.

If the code ran successfully, we should see the output of our code in the Data Out tab:

Output data from Zapier

Troubleshooting

If Zapier returned an error, here are a few common errors that might be the cause:

  • Verify your API key is correct.
  • Test your query in the playground. Does it work there?

Additionally, you can use console.log to output data at various points in the code for better visibility of issues within the code. Logs are returned in a runtime_meta object added to the output,

You can also refer to Zapier's documentation on troubleshooting errors within a Code action.

Updating Google Sheets

I've created a Google Sheet template for this section; you can find it here. I recommend using the template in conjunction with this article first before making adjustments to better suit your use case.

To create a copy from the template, open Google Sheets, click File > Make a copy, and save it to your Google Drive.

Adding Sheets Action

In the Zap draft editor, click the plus (+) sign below the code action to add a new action. From the list of top apps, select the Google Sheets option.

First, we must select an event, which is the operation we want to make on our Google Sheet. Search the list of events and find the Create Spreadsheet Row event, which creates a row with data from our SerpApi search on a specified spreadsheet.

Configuring Create Spreadsheet Row Action

Click Continue to be taken to the Account tab. If you have not done so previously, you may need to authorize Zapier to use Google Sheets. Google Sheets uses OAuth to authenticate your account in Zapier.

  • Log into Google Sheets to authenticate.
  • Grant Zapier permission to access your account if prompted to.

After verifying that your Google account is connected, click Continue again. You should now be viewing the Action tab, where we will select the Sheet we would like to write to and map the data from the output of step 2 of our Zap to our Google Sheet headers.

Select the Google Drive from the Action tab containing your Sheet, the spreadsheet you wish to write to, and the worksheet. Your action should now look something like this:

Create Spreadsheet Row configuration

After selecting the worksheet, Zapier should automatically populate additional fields from the headers (first row of your spreadsheet). We can now map the data from SerpApi to our Google Sheet.

Map each field to data output from step 2:

Here's how the fields should look after selecting all of the output data:

💡
For the Date field, we are using the Prettified Date output from the Schedule trigger. This will be the date that our Zap triggered.

After mapping the output data to fields in the Google Sheet, we are ready to test the action. Click Continue once more to view the Test tab, where we will see the test data we generated in our Code step.

Click Test Step, and Zapier will run the action. Once complete, if your test was successful, you should be presented with the Data Out tab with information about the row created.

Created Spreadsheet Row test output

We should also see the data sent from Zapier in your spreadsheet.

Going Further and Alternatives

Typically, you will want to search for multiple keywords to track SEO changes, but due to limitations in Zapier, we were only able to perform a single API call to SerpApi in this demo. Zapier requires that all code actions resolve within a specific time limit or RAM usage; see Code rate limits.

Depending on your Zapier plan, you can run multiple API requests in a single code action as long as the requests can finish executing before hitting your plan's rate limit. In most cases, an enterprise-level plan would be required if you are searching for more than a few queries in a single Code action.

Alternatively, you can use Zapier's Loop action to iterate over a list of keywords and write the results of each request to a row. Loop actions require at least a Professional tier plan.

SerpApi Sheets Extension

If your use case allows, you can skip Zapier altogether and accomplish all your data fetching entirely in your Google Sheet using the SerpApi Google Sheets Extension; see Connect SERP API with Google Sheets (No Code).

Conclusion

Integrating SerpApi with Zapier opens up powerful possibilities for automating SEO data collection and analysis. By following the steps outlined in this guide, you can create a Zap that regularly fetches Google search results, analyzes rankings for specific websites, and populates a Google Sheet with the data - all without writing complex code or managing infrastructure.

This automation is a starting point that you can customize and expand upon. Some potential enhancements could include:

  • Tracking multiple keywords and websites
  • Integrating with other tools in your marketing stack
  • Setting up alerts for ranking changes
  • Generating automated reports

While we've focused on a specific use case, the concepts covered here can be applied to many other scenarios where you must programmatically access search data.

As you continue to explore these tools, remember that SerpApi offers a wide range of search APIs beyond just Google, and Zapier can connect to thousands of apps. This gives you nearly limitless possibilities for creating custom, automated workflows tailored to your business needs.

Resources