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:
-
Go to the Google Cloud Console.
-
Create a new project (or select an existing one).
-
In the left sidebar, go to APIs & Services > Credentials.
-
Click "Create credentials" > "API key".
-
Copy the API Key shown — you'll use this in your code.
-
✅ 2. Custom Search Engine ID (cx)
๐ง Steps:
-
Go to the Custom Search Engine (CSE) page.
-
Click "Get Started".
-
Enter a domain to search (e.g.,
www.google.com
or leave it as*.com
to search the whole web). -
Click "Create".
-
After it's created, go to the Control Panel.
-
Under "Search engine ID", you'll see your
cx
value — copy this.
๐ Enable Image Search:
-
In the Custom Search Engine settings (Control Panel):
-
Go to "Basics" > Sites to search → make sure it’s set to “Search the entire web”.
-
Go to "Image Search" tab (or advanced settings) and enable "Image Search".
-
Save the changes.
✅ Final Setup in Code
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:
This link
can be used as the image source (<img src="..."/>
).
Comments
Post a Comment
What is your thought about this?