Build Your Own Google Search Engine in Under 30 Minutes

Neil Skarphedinsson
4 min readJun 20, 2023

--

Looking for a way to leverage Google’s powerful search functionality for your own projects? Thanks to Google’s Programmable Search Engine, it’s easier than ever. This service allows you to create a search engine for your website, app, or project that utilizes Google’s search technology, all in under 30 minutes. Here’s a step-by-step guide on how to get started.

1. Register for Google Cloud

To start with, you need to register an account with Google Cloud. If you don’t have an account yet, it’s your lucky day! Google Cloud currently offers $300 in free credits for new sign-ups. So go ahead and sign up here.

2. Set Up a Billing Account

Once your account is created, you’ll need to set up a billing account. This is usually a part of the registration process, but if you skipped it, you can set it up by navigating to your account settings.

3. Enable the Custom Search API

Google Cloud hosts a variety of tools and services on its platform. To access them, you need to enable them first. For our purpose, you’ll need to enable the Custom Search API. To do this:

  • Navigate to the left sidebar and find “APIs & Services” > “Enabled APIs & services”.
  • Click on “ENABLE APIS AND SERVICES” at the top of the page.
  • Search for “Custom Search API” in the search field.
  • Once found, click “ENABLE” to activate the service.

4. Create an API Key

With the Custom Search API enabled, it’s time to create an API key. This key will be used to call the Search API from your application. Here are the steps:

  • Go to the “Credentials” page by clicking it on the left sidebar.
  • Click “CREATE CREDENTIALS”, and from the drop-down menu that appears, select “API key”.
  • Once your key is created, make sure to copy it and save it in a secure location.

To enhance security, restrict the API key’s access to only the Custom Search API. This way, even if your key gets lost or stolen, it can’t be misused for other APIs.

5. Create Your Search Engine

Now comes the fun part — creating your programmable search engine. This is done using the Programmable Search Engine Console.

  • Click “Add” to create a new search engine. Each one can have different settings, filters, and more, giving you ample control over your searches.
  • For this tutorial, let’s start by adding “en.wikipedia.org” as a specific site to query. However, feel free to adjust this to suit your needs. You can even specify patterns in a highly flexible way.

Congratulations! You have successfully created your programmable search engine. A piece of code will be provided to you that you can use to embed the search engine in your HTML. You can proceed by clicking “Customize”.

On the same page, notice the “Search Engine ID”. This, along with the API Key you created earlier, will be the two necessary pieces of information for calling the Google Search API in your back-end code.

Conclusion

And that’s it! You’ve now successfully created your own Google Search Engine. As you can see, this process is not only quick and easy but also allows for customization and flexibility in the use of Google’s search technology.

Bonus: call the search engine in code!

import requests
import json

def run_query(search_term, api_key, cx_id):
# The url for the google custom search engine API
url = "https://www.googleapis.com/customsearch/v1"

# The parameters for the query
params = {
'q': search_term,
'key': api_key,
'cx': cx_id
}

# Execute the request
response = requests.get(url, params=params)

# Check the response
if response.status_code == 200:
# Everything went okay, parse the result
result = json.loads(response.text)
return result
else:
# An error occurred, raise an exception
raise Exception(f"Error executing search: {response.status_code}")

# Replace these with your actual values
api_key = "your-api-key"
cx_id = "your-cx-id"

# Run a query
result = run_query("Python Programming", api_key, cx_id)
print(result)
import axios from 'axios';

const apiKey = 'YOUR-API-KEY';
const cxId = 'YOUR-CSE-ID';

const runQuery = async (searchTerm) => {
const url = 'https://www.googleapis.com/customsearch/v1';

try {
const response = await axios.get(url, {
params: {
q: searchTerm,
key: apiKey,
cx: cxId,
},
});

console.log(response.data);
} catch (error) {
console.error(`Error occurred while executing search: ${error}`);
}
};

runQuery('JavaScript Programming');

--

--