Cloud ServicesHow-Tos

[How to] Implement Google Programmable Search in Your Node.js App

How to series

AI Generated image. Doesn’t depict the actual product.

Google Custom Programmable Search Engine allows you to embed search functionalities within your web or mobile applications. It lets you programmatically retrieve and display search results from a custom-defined search engine (or Google Search). This guide will walk you through integrating the Google Programmable Search Engine using Node.js and retrieving search results.

Prerequisites

  1. Google Account: You need a Google account to access Google Cloud Console and enable the API.
  2. API Key: Create an API key through the Google Cloud Platform.
  3. Search Engine ID: Create a Custom Search Engine (CSE) and get the Search Engine ID.

Steps to Get API Key and Search Engine ID

Create a Programmable Search Engine:

  • Go to Google Programmable Engine and click “Create a custom search engine”.
  • Specify the sites you want to include in your search (optional).
  • Get the Search Engine ID from the Custom Search Engine control panel.

Enable Google Custom Search API:

  • Go to the Google Cloud Console.
  • Enable the Custom Search API from the API Library.
  • Go to APIs & Services Dashboard to generate an API Key by navigating to Credentials and clicking “Create Credentials”.

Store API Key and Search Engine ID:

  • Save the API_KEY and SEARCH_ENGINE_ID in your environment variables to secure sensitive data.

Node.js Implementation

Once you have the API_KEY and SEARCH_ENGINE_ID, you can integrate the Google Custom Search API into your Node.js project.

Setting Up the Project

  • Install Axios and dotenv: Run the following commands to install the required packages:
npm install axios dotenv
  • Create a .env file to store your environment variables:
API_KEY=your_google_api_key
SEARCH_ENGINE_ID=your_custom_search_engine_id
  • Write the Code: Below is the sample Node.js code to call Google Custom Search API, perform a search, and return unique URLs from the search results.
require('dotenv').config();
const axios = require('axios');

// Fetching the API key and Search Engine ID from environment variables
const apiKey = process.env.API_KEY;
const searchEngineId = process.env.SEARCH_ENGINE_ID;
const query = "query"; // Your search query
const numResults = 10; // Number of results you want to fetch

const performSearch = async () => {
try {
// Making a GET request to Google Custom Search API
const response = await axios.get('https://www.googleapis.com/customsearch/v1', {
params: {
key: apiKey,
cx: searchEngineId,
q: query,
num: numResults
}
});

// Extracting the search results from the API response
const results = response.data.items || [];

// Extracting and filtering unique URLs from the search results
const uniqueUrls = new Set(results.map(result => result.link));

// Creating a list of unique URLs
const searchResults = Array.from(uniqueUrls).map(url => ({ url }));

return searchResults;
} catch (error) {
console.error("Error performing search:", error);
return [];
}
};

// Example usage of the performSearch function
performSearch().then(searchResults => {
console.log("Search Results:", searchResults);
});

Code Explanation:

  • dotenv: The dotenv package is used to load the environment variables from a .env file.
  • axios: We are using axios to make HTTP requests to the Google Custom Search API.
  • performSearch function: This asynchronous function sends a GET request to the API with the query and API key. It then processes the response and extracts URLs of search results, ensuring that only unique URLs are returned.

Important Notes:

  • The API response includes many useful fields, such as the page title, snippet, and link. You can modify the code to include these fields if needed.
  • Google Custom Search API has rate limits. Ensure you monitor your API usage to avoid exceeding the quota.

Using Google Custom Search API with Node.js allows you to embed Google’s powerful search functionality directly into your app. The flexibility of the API lets you customize the search results to your needs, whether for a website or a mobile app. You can further enhance this setup by integrating it into a more extensive service or system.


A Note from the writer…

Welcome to my How-to Tech Blog Series, where I break down technical tasks into simple, actionable steps. Whether you’re coding in your favorite language, navigating the complexities of cloud technologies, or tackling day-to-day programming challenges, this series aims to provide clear and concise solutions. Stay tuned for quick guides that empower you to solve problems and enhance your skills!

Rajesh Mishra

I'm a developer who loves sharing insights, technical how-tos, and lessons learned from the world of code. While much of what I write may not be groundbreaking, I believe in documenting for future me—and for anyone else who might find it useful. Beyond tech, I also dive into life's experiences and moments, reflecting on personal growth and sharing stories that resonate. Whether you're here for practical tips or a fresh perspective on life, I hope you find something meaningful.

Leave a Reply

Your email address will not be published. Required fields are marked *