Use the Google Custom Search JSON API to retrieve images

 To use the Google Custom Search JSON API to retrieve images, you need two things:


✅ 1. Google API Key

๐Ÿ”ง Steps:

  1. Go to the Google Cloud Console.

  2. Create a new project (or select an existing one).

  3. In the left sidebar, go to APIs & Services > Credentials.

  4. Click "Create credentials" > "API key".

    • Copy the API Key shown — you'll use this in your code.


✅ 2. Custom Search Engine ID (cx)

๐Ÿ”ง Steps:

  1. Go to the Custom Search Engine (CSE) page.

  2. Click "Get Started".

  3. Enter a domain to search (e.g., www.google.com or leave it as *.com to search the whole web).

  4. Click "Create".

  5. After it's created, go to the Control Panel.

  6. Under "Search engine ID", you'll see your cx value — copy this.

๐Ÿ›  Enable Image Search:

  1. In the Custom Search Engine settings (Control Panel):

  2. Go to "Basics" > Sites to search → make sure it’s set to “Search the entire web”.

  3. Go to "Image Search" tab (or advanced settings) and enable "Image Search".

  4. Save the changes.

✅ Final Setup in Code

js

const apiKey = "YOUR_API_KEY"; // from Google Cloud Console const searchEngineId = "YOUR_SEARCH_ENGINE_ID"; // from Custom Search Engine

Then, you can use the code you shared to fetch an image based on a query.


๐Ÿงช Example

If you're searching for a "burger", the API might return:

json

{ "items": [ { "link": "https://upload.wikimedia.org/burger.jpg", ... } ] }

This link can be used as the image source (<img src="..."/>).

Comments

Popular posts from this blog

Top Laravel Security Best Practices: How to Secure Your Web Application ๐Ÿš€

Restoring Your Data in Xampp: A Step-by-Step Guide

Passing array from child to parent component