Build a Custom Search Engine


Build a personalised search engine with Google’s search API.

We are going to build a custom search engine using Google’s Search API to fetch queries and display the most relevant search results.

Final Output

To build this search engine, we’re going to need:

  • HTML
  • CSS
  • Javascript
  • account
  • Google account

We’ll also be getting our hands on Google APIs, and you don’t need to worry if you don’t know about how you can use it, because we’ll be taking you through that as well.

Getting Started

We are providing you the starter files for this project and fork it so you can make the changes and get started with building the search engine. The starter file includes the styles and preliminary code to help you kickstart this project.

Starter files

Once this is ready, head on over to the index.html file and add the following between the tags:

This will reference the image with it’s corresponding styling! Feel free to add your own to make it personalized. Then, we are going to add a search input and button that calls submit() when clicked.

Now, we need to create a section for the content to appear! Add the following right below the

Finally, let’s reference the javascript files to connect the queries to the web!

Google API

Supercool—the interface is now complete! Now let’s fetch some queries. In your query.js file, you should see a function that calls submit(). Within that function, paste the following code:

document.getElementById('buttons').style.display = 'block';
document.getElementById('content').innerHTML = '';
var val = document.getElementById('search').value;
var newelement = document.createElement('script');
newelement.src = `${val}&callback=hndlr`; = 'mainscript';

Now, we need to set up the Google Search API! To get started, head on over to Google’s Developer Portal and make sure that you are logged in. Once complete, scroll down to Get a Key and create a project called Search. Once you click next, copy your API key and click Done. In your query.js file, you will see a variable called newelement.src. Where it says API_KEY, paste in your API Key and you’re good to go!

Google Developer Portal

So what is this URL and why is it so long?

  • lets us know that we are using the Google Custom Search API, the first version of it
  • key=API_KEY indicates the user of the API, as well as any limitations. For example, this will associate the number of queries per day with your account
  • &cx= associates any searches with a search engine Id
  • q=${val} is the actual query that a user inputs, which we will fetch from the submit() function in our HTML
  • callback=hndlr is used as a parameter that is called after the function is executed

Formatting Query Results

Now that this is completed, let’s format our content. Head over to the script.js file and you should see a function called hdnlr(response). Inside this function, add the following code:

try {

} catch(error) {


Essentially, the function will try some code, and if it does not work, will catch & output an error. Within the try function, add the following code:

document.getElementById('content').innerHTML += `
Wohooo! About ${response.searchInformation.formattedTotalResults} results in ${response.searchInformation.formattedSearchTime} seconds!

This fetches the amount of results and the time it took to retrieve, just like how you see on Google! Then, to fetch the actual information, add the following to your try function:

for (var i = 0; i < response.items.length; i++) { 
  document.getElementById('content').innerHTML += `

Now, your code will fetch the link, title and htmlSnippet to display for each query. To output any errors, add the following to the catch function:

document.getElementById('content').innerHTML = 'error!';

And just like that, you’ve successfully fetched your meta data for each query! Now, click ‘Run’ at the top of your Repl and watch the magic happen!

If your code outputs an error, feel free to reference the final code!

Magic Time!

Search Engine

Search query results

And that’s it! If you click run and head to your replit link, you should see a full functional search engine at your service! Here’s a link to the final code (excluding the API key) if you need any help!

Few important points to remember:

  • Please do not share your API key with anyone
  • Do not exceed 100 queries per day (Because Google provides only 100 free queries a day, or else you can exceed your limit by paying the extra charges)

Happy Hacking and join Hack Club TechBrewers for building more amazing stuff 🚀

Leave a Reply

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

Previous Post

Graphman: generate a postman collection for any GraphQL endpoint

Next Post

Greetings! Remini – Effective state management in React 🌴

Related Posts