So you set up your Google Custom Search Engine, you have the API key published in your application, and you are ready to conduct your search. But Google has returned an error message indicating that your API key is invalid.
Perhaps you’re perplexed because the same API key was just working fine a couple of days ago.
But don’t worry… an invalid API key error doesn’t mean you cannot fix this issue and you have to regenerate your key. Usually, the reasons for the error can be found in the restrictions on your API or in the wrong settings of your project or just in the way how you implement your API key.
Let’s analyze the reasons of the error, how to find out what the actual problem is and how to fix it.
Why Is Google Custom Search Rejecting Your API Key?
When making a search inquiry, Google Custom Search JSON API requires the use of an API key to identify the application. Furthermore, each inquiry must also have a Programmable Search Engine ID (cx) and a query (q).
A typical request looks like this:
https://www.googleapis.com/customsearch/v1?key=YOUR_API_KEY&cx=YOUR_SEARCH_ENGINE_ID&q=website+search
The failure of the inquiry may be attributed to Google not being able to authenticate it or the lack of permission for the project to utilize the API.
Some of the reasons may be:
- API key is incorrect: The API key may have been copied, cut off or changed to another one.
- Restrictions on API key: The API key may be limited to other APIs, websites, IP addresses etc.
- Wrong Google Cloud project: The API key and enabled Custom Search API may belong to different projects.
- API is not enabled: The Custom Search API might not be enabled in the project connected to the key.
- Deleted or regenerated key: Your application may still be using an older key that does not work anymore.
It should be noted that the error message saying that the project cannot connect to the Custom Search JSON API is not always a problem of invalid keys. Restrictions regarding the API need to be verified in a different way.
First, Make Sure You’re Looking at the Right Error
Before changing your API settings, check the actual error response. Google returns different error messages depending on what went wrong.
| Error message | What it usually indicates |
|---|---|
| API key not valid | Incorrect, deleted, or unrecognized API key |
| API key expired | Key validity or lifecycle issue |
| API key not authorized | Key restrictions or permissions |
| 403 PERMISSION_DENIED | Project access, API restrictions, or authorization issue |
| API has not been used or enabled | API configuration problem |
For example, if your response contains API_KEY_INVALID, focus on the key itself.
If you receive 403 PERMISSION_DENIED, don’t immediately generate another key. Check whether the project is authorised to access the service.
This small distinction can save you a lot of unnecessary troubleshooting.
How to Fix the Invalid API Key Error
Check Whether You’ve Copied the Correct API Key
This may seem obvious, but it is one of the easiest things to be ignored.
Access the Google Cloud Console, choose the project linked to your search app, and go to APIs & Services → Credentials.
Locate the API key you are using and compare it with one that you have set up in your app.
Make sure to check:
- Value of the key at the beginning of end.
- Extra blank spaces and quotation marks.
- Outdated keys in your environment variables.
- Using a different key for development and production environments.
In case the old key is deleted, generate a new one and update your application.
Review Your API Key Restrictions
Google Cloud offers developers way to impose restrictions on API keys. Unfortunately, errors in restrictions sometimes cause legitimate requests to become unable to reach Custom Search API.
To review restrictions of an API key, go to the Credentials section, and select your API key.
By looking at API restrictions, you will find the necessary Custom Search API in the allowed ones.
As well, don’t forget to check the restrictions on applications:
- HTTP referers: Make sure your website’s domain is on the allowed one.
- IP addresses: Check the outgoing IP address of your server.
- Android or iOS restrictions: Confirm that the application is configured correctly.
Confirm That the Custom Search API Is Enabled
Even if you possess a valid API key, it can be deemed ineffective if it is not valid for the Google Cloud project that requires permission for the service. Here’s how to check it.
- Go to Google Cloud Console.
- Select the required project.
- Go to APIs & Services → Library.
- Type Custom Search API in the search engine.
- Find if the required API is enabled.
In case the required API is not active, activate it and try again.
Keep in mind that enabling the required API and getting authorization to use it are two completely different things. Both should be checked.
Check Your Programmable Search Engine ID
At times developers waste precious hours determining the problem with their API key when the thing they should be looking for is an invalid Search Engine ID.
The cx parameter points to the Programmable Search Engine to use with Google.
Go to the Programmable Search Engine Control Panel and find your search engine ID.
Please ensure that:
- The cx parameter is valid.
- The search engine hasn’t been removed.
- The search engine that corresponds to your application is used.
Although your API key and Search Engine ID are two different things, you still have to make sure not mixing these two up.
Test the Request outside Your Application
Still encountering the same issue? Attempt to send a request straight from Postman or other API testing tool.
Follow the URL structure like:
https://www.googleapis.com/customsearch/v1?key=YOUR_API_KEY&cx=YOUR_SEARCH_ENGINE_ID&q=test
Replace all the placeholder values accordingly.
If your request works in Postman and fails in your application, investigate your application’s request creation and transmission.
Look for:
- Incorrect URL encoding.
- Some missing parameters.
- Outdated credentials recorded in the environment variables.
- Requests are sent using a different API key.
If the request returns error in both cases, then it is highly likely that the issue lies in the configuration of Google Cloud, key validity or service access.
Security tip: Do not share your real API key in the screenshots, public debugging tools or support.
What If Your API Key Is Correct but the Error Still Appears?
In 2026, developers are facing a completely new problem.
Google has stated that Custom Search JSON API will stop taking on new users starting January 20, 2026. Customers are going to have until January 1, 2027, to switch to a different API.
This means that developers might not be able to use the API regardless of how many Google Cloud projects they create and valid API keys they generate to enable the API.
If you see a message that reads:
This project has no access to Custom Search JSON API
Before you waste your time on changing keys, make sure you check your eligibility for using the API and Google’s migration details.
This is a good time for companies that rely on search function on their websites to look for alternatives since it seems that the API is going to be shut down soon.
Looking for a More Flexible Website Search Solution?
Solving API problems is one aspect of the equation, but it is another task to guarantee that your visitors have a good search experience on your website. If you are developing your own website or if you are managing a store in the eCommerce space, it is crucial that you have something more than an API that simply gives you search results and allows your visitors to find the information they need.
This is where ExpertRec comes in.
ExpertRec provides adjustable site search capabilities that work for both websites and eCommerce stores. Instead of assembling and maintaining your own search system around the Google Custom Search JSON API, you can use a custom site search solution with the following features:
- Autocomplete: Help people make relevant searches as they type.
- Advanced search: Allow people to filter their search results to find what they need.
- Search relevance: Improve the quality of the search results displayed to users.
- Custom search experience: Adjust the search functions according to your website’s requirements.
If you are a firm that wants to have more control over your internal website search system, ExpertRec is a perfect solution.
Don’t Let an API Key Error Disrupt Your Search Experience
Getting an invalid Google Custom Search API key error can be the result of something as straightforward as a credential being copied over or as complex as restrictions being placed at the project level.
To troubleshoot this, start by checking the error message to see what is being flagged, make sure that your API key is working, check any restrictions, double-check that you are using the right project and Search Engine ID.
Most importantly, understand the difference between an invalid API key and that of an access issue. Since Google’s Custom Search JSON API is no longer accepting new customers, being able to discern that difference is extremely important.
And if keeping your website search experience seamless is your greater concern, using a professional system such as ExpertRec could be a good way of saving yourself from worrying about API maintenance.




