Google reCAPTCHA siteverify says the response or secret is invalid
"success": falseShort answer
siteverify puts the reason in its error-codes array, so read that first. invalid-input-secret means the secret you sent is wrong or empty. invalid-input-response means the token is missing or mangled. timeout-or-duplicate means the token is older than two minutes or was already verified once.
Fix it fast
1Find out which cause you have
Log the whole JSON answer from Google, not just success. The error-codes array tells you which cause below applies.
const res = await fetch('https://www.google.com/recaptcha/api/siteverify', { method: 'POST', body: new URLSearchParams({ secret: process.env.RECAPTCHA_SECRET ?? '', response: token ?? '' }), }); console.log(await res.json());2Match what you see, then jump to its fix
What does the error look like?
"success": false"error-codes": ["invalid-input-response"]"error-codes": ["invalid-input-secret"]"error-codes": ["timeout-or-duplicate"]
Cause 1 of 3
The secret is empty, wrong or not the secret key
1Check
Google describes the secret as the shared key between your site and reCAPTCHA. The site key is the one that belongs in your HTML, so a site key pasted into the server variable fails here. Check that the variable is set in the process that runs the server, and that no quotes or spaces were copied with it.
node -e "const s = process.env.RECAPTCHA_SECRET; console.log(s ? 'set, length ' + s.length : 'NOT SET')"2Fix
Copy the secret key from the reCAPTCHA admin console into the server environment and send it in the secret field of the POST body.
const params = new URLSearchParams({
secret: process.env.RECAPTCHA_SECRET,
response: token,
});
const res = await fetch('https://www.google.com/recaptcha/api/siteverify', { method: 'POST', body: params });3Verify
The answer no longer lists a secret error.
console.log((await res.json())['error-codes']);Confirmed by developers.google.com
Cause 2 of 3
The token never reaches the server, or arrives changed
1Check
The widget sends the token in a POST field named g-recaptcha-response. If your form is submitted with fetch or axios as JSON, that field is not added for you, and your body parser may drop it. Log what the route receives.
app.post('/contact', (req, res) => {
console.log('token length:', (req.body['g-recaptcha-response'] ?? '').length);
});2Fix
Read the token on the page with grecaptcha.getResponse() (or from your callback argument), send it in the request body, and pass it unchanged as the response parameter.
// browser
const token = grecaptcha.getResponse();
await fetch('/contact', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token, email }),
});3Verify
The token length logged on the server is greater than zero and siteverify no longer returns invalid-input-response.
console.log('token length:', req.body.token?.length);Confirmed by developers.google.com
Cause 3 of 3
The token expired or was already used
1Check
A token is valid for two minutes and can be verified only once. Verifying it twice (a retry, a second middleware, a test that replays the same token) returns this code.
grep -rn "siteverify" src/2Fix
Verify once per submit, right when the form arrives. If the user waited too long or you must retry, make the page request a new token and run the challenge again.
// browser, after a failed verification
grecaptcha.reset();3Verify
A fresh submit passes on the first call, and the same token sent again returns timeout-or-duplicate.
console.log((await res.json())['error-codes']);Confirmed by developers.google.com
How do I stop it coming back?
- Keep the site key in the page and the secret key only in server environment variables.
- Call siteverify from exactly one place in the request path.
- Log error-codes, not only success, so the cause is visible the first time it happens.
- Reset the widget after every failed submit so the user gets a new token.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix