Create custom Google search engine for your domain(s) and fetch results as JSON
Tags: javascript, api, google
Published: 2018-08-09 04:39:55

---
title: Create custom Google search engine for your domain(s) and fetch results as JSON
published: true
description: Create a Custom Google Search Engine for your website domain(s) and access it programmatically with barely any code.
tags: javascript, api, google
---

I decided to write this simple 5 minute tutorial before I go to sleep.

## Create a Custom Google Search Engine for your website domain(s) and access it programmatically with barely any code.

Navigate to [https://cse.google.com/cse/all](https://cse.google.com/cse/all) where you can create a custom search engine. Click on the "Add" button and provide one or multiple sites to search on. In this case just add _dev.to/*_ to the list.
If you _enable_ the setting called 'Search the entire web', your search results will be augmented with results from the web if nothing (or not enough) is found. That means _disable_ this to make sure it will only ever show results from your web domain(s).

![s1](https://thepracticaldev.s3.amazonaws.com/i/vb01s68vf05j86jf6kat.png)

If you want to embed your custom search engine on a webpage with a simple copy/paste then you can choose between 7 different layouts of your search engine. I personally prefer the compact one. These layouts show advertisements. 

![s2](https://thepracticaldev.s3.amazonaws.com/i/jn5b89yp1ei8k9gz945j.png)

There are two options for programmatic access. The option _Custom Search JSON API_ is free and has a limit of 10,000 queries per day. This is what you want in this case.

> If your Custom Search Engine is restricted to only searching specific sites (10 or fewer), you can use the Custom Search Site Restricted JSON API. This API is similar to the JSON Custom Search API except this version has no daily query limit. Custom Search Site Restricted JSON API requests cost $5 per 1000 queries and there is no daily query limit. You may sign up for billing in the API Console.

### You need
API _key_ (get it here: [https://developers.google.com/custom-search/json-api/v1/introduction](https://developers.google.com/custom-search/json-api/v1/introduction))
Search Engine identifier _cx_ (which you can find in your search engine public URL: [https://cse.google.com/cse?cx=009833334622897458665:rtvizlbvdpk](https://cse.google.com/cse?cx=009833334622897458665:rtvizlbvdpk))

To query your search engine for 'open source', simply make a GET request to https://www.googleapis.com/customsearch/v1?key=YOUR_API_KEY&cx=SEARCH_ENGINE_ID&q=open+source

Let's say you're on top of things. Add a _sort=date_ parameter to the querystring to sort the results by date (newest first). Google picks up on changes pretty fast as you can see in this screenshot.

![s3](https://thepracticaldev.s3.amazonaws.com/i/dzkq180ifcuav7tkmhtj.png)

```js
/* since this is a public API, it permits cross origin XMLHttpRequests from the browser */
fetch('https://www.googleapis.com/customsearch/v1?key=YOUR_API_KEY&cx=SEARCH_ENGINE_ID&q=open+source&sort=date').then(response => response.json()).then(json => {
   // json.items has the results 
}).catch(console.error)
```

ZzzzZZ..