This backend service integrates seamlessly with the Google Search API, leveraging Google's robust search infrastructure to provide accurate and current search results.
To optimize performance, I've implemented a caching mechanism that stores accessed search results in our local database upon initial retrieval. This approach significantly enhances response times for subsequent queries by minimizing reliance on external API calls.
In the event of API usage limits being exceeded (Google Search API allows only 100 requests per day), users will receive timely notifications. However, previously accessed search results will remain accessible from our local database, ensuring uninterrupted service and user convenience. This proactive strategy aims to mitigate temporary limitations and maintain seamless functionality.
- Installation
- Configuration
- Running the Application
- API Endpoints
- Database Model
- Error Handling
- Logging
- License
-
Clone the repository:
git clone https://github.com/niyontwali/google-search-api-backend.git cd google-search-api-backend -
Install dependencies:
npm install
-
Set up environment variables:
Before running the backend, ensure you have a
.envfile configured with the necessary environment variables. You can create this file by executing the following command in your terminal:cp .env.example .env
This command duplicates the
.env.examplefile and sets up your.envfile with placeholders. Make sure to replace these placeholders with your actual environment-specific values.Here is an example of how your
.env.examplefile should look:# Environment NODE_ENV=development HOST_NAME=127.0.0.1 # Database Configuration PORT=8080 DB_HOST= DB_NAME= DB_USER= DB_PASSWORD= DB_PORT= # Google API Configuration GOOGLE_BASE_URL= GOOGLE_API_KEY= GOOGLE_CX=
Replace each variable (
PORT,DB_HOST, etc.) with your specific configuration values. Ensure you fill in theGOOGLE_BASE_URL,GOOGLE_API_KEY, andGOOGLE_CXvariables with the appropriate values from your Google API credentials. -
Run Migrations:
npm run migrate
Start the server in development:
npm run devThe server will start on the port specified in the .env file, defaulting to 8080 or you can as well change it to
another port in your .env.
Endpoint: /search
Method: GET
Query Parameters:
q(required): The search query string.
Response:
- 200 OK: Returns search results.
- 400 Bad Request: If the query parameter
qis missing. - 403 Forbidden: If the Google API request limit is exceeded.
- 500 Internal Server Error: For other errors.
Example:
GET http://localhost:8080/search?q=John%20NiyontwaliResponse:
{
"ok": true,
"data": [
{
"id": "47a55ac7-b400-4069-8d0b-6895814425ca",
"searchQuery": "John Niyontwali",
"displayLink": "njohn.netlify.app",
"title": "John - Personal Portfolio",
"snippet": "JohnNiyontwali · Address :Kigali - Rwanda · Phone :+250 786 266 073 · Email :[email protected] · github and gitlab usernames:niyontwali and nijohn ...",
"image": null,
"createdAt": "2024-07-11T12:56:23.000Z",
"updatedAt": "2024-07-11T12:56:23.000Z"
},
...
]
}This backend includes Swagger documentation, which can be accessed both locally and online through the following links:
- Local Documentation: http://localhost:8080/v1/docs. Note that is if your port set in
.envis 8080 - Online Documentation: https://api.google-clone.nijohn.dev/v1/docs. You can access it from anywhere.
Below is the look of the swagger documentation page

The Search model is used to store search results with the following fields:
id: UUID, primary keysearchQuery: STRING, the search querydisplayLink: STRING, the link to the resulttitle: STRING, the title of the resultsnippet: STRING, the snippet of the resultimage: TEXT, the URL of the image (if available)
- Google API Limit Exceeded: Returns a
403status code with a message indicating that the request limit has been exceeded. - General Errors: Logs the error and returns a
500status code with a generic error message.
The application uses a logger to record errors and other important information. Logs are configured in
logs/configs.js.
This project is licensed under the MIT License. See the LICENSE file for details.