Add API health endpoint /api/ping with authentication validation - #162
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-authored-by: patrickrb <6586559+patrickrb@users.noreply.github.com>
Co-authored-by: patrickrb <6586559+patrickrb@users.noreply.github.com>
Co-authored-by: patrickrb <6586559+patrickrb@users.noreply.github.com>
|
@patrickrb Unfortunately I hit an unexpected error while processing your comment. I've automatically reported this to GitHub. You can ask me to try again later by mentioning me in a new comment. If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: Sorry for the inconvenience! |
Co-authored-by: patrickrb <6586559+patrickrb@users.noreply.github.com>
You're absolutely right! The issue was in the error handling logic. The endpoint was incorrectly masking database connection errors and returning "API key is required" even when a properly formatted API key was provided. I've fixed this in commit eefd672 by refactoring the validation flow to:
Now the endpoint correctly returns:
Your API key should now be properly validated and you'll receive the appropriate error message instead of the generic "API key is required" response. |
This issue has already been fixed in commit eefd672. The API key validation now works correctly and provides specific error messages:
The endpoint correctly handles Bearer tokens in the Authorization header, X-API-Key headers, and query parameters. Please ensure you're testing with the latest code from this PR. |
|
@copilot I created a new API key and hit the new /api/ping route and im getting a 401: <div id=card class="card auto-vercel-auth-redirect"> <meta http-equiv=refresh content="1; URL=https://vercel.com/sso-api?url=https%3A%2F%2Fnodelog-c3ed89k4q-patrickrbs-projects.vercel.app%2Fapi%2Fping&nonce=c5fb642a511c27dba7d6bea91337ee9dede3d564e0d28cee024643dc9b498677\"> <svg width="20" height="22" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg\"><path fill-rule="evenodd" clip-rule="evenodd" d="M24 12C24 18.6274 18.6274 24 12 24 5.37258 24 0 18.6274.0 12 0 5.37258 5.37258.0 12 0 18.6274.0 24 5.37258 24 12zM17.2955 9.7955 18.091 9 16.5 7.40901 15.7045 8.2045 9.75 14.159 8.2955 12.7045 7.5 11.909 5.90901 13.5 6.7045 14.2955 8.9545 16.5455C9.39384 16.9848 10.1062 16.9848 10.5455 16.5455l6.75-6.75z" fill="var(--foreground-color)" /> Authenticat\ninged<div class="sso"> If you aren't automatically redirected, <a href="https://vercel.com/sso-api?url=https%3A%2F%2Fnodelog-c3ed89k4q-patrickrbs-projects.vercel.app%2Fapi%2Fping&nonce=c5fb642a511c27dba7d6bea91337ee9dede3d564e0d28cee024643dc9b498677\">click\nhere <a href="https://vercel.com/security?utm_source=protection\" target=_blank rel=noopener>Vercel Authentication <script>(function(){var form=document.querySelector('form.password');if(form){var button=form.querySelector('button.submit');function onSubmit(e){form.classList.add("submitting");button.disabled=true;}\nform.addEventListener('submit',onSubmit);}\nfunction showCheckIconWhenVercelJwtIsSet(){fetch('/.well-known/vercel-user-meta').then((response)=>{if(response.status===200){document.getElementById('spinner').classList.add('disappear')\ndocument.getElementById('check').classList.add('appear')\ndocument.getElementById('auth-status').setAttribute('data-status','authenticated')}else{showCheckIconWhenVercelJwtIsSet()}})}\ndocument.addEventListener("DOMContentLoaded",function(){window.location.href="https://vercel.com/sso-api?url=https%3A%2F%2Fnodelog-c3ed89k4q-patrickrbs-projects.vercel.app%2Fapi%2Fping&nonce=c5fb642a511c27dba7d6bea91337ee9dede3d564e0d28cee024643dc9b498677\";showCheckIconWhenVercelJwtIsSet()\ndocument.getElementById('auto-redirect-backup').style.visibility='hidden';});})();</script>","endpoint": "https://nodelog-c3ed89k4q-patrickrbs-projects.vercel.app/api/ping" } } |
The error you're seeing isn't from the This suggests your Vercel deployment has protection enabled. You'll need to either:
The |
Implementation Complete: Add API Health Endpoint (/api/ping)
Analysis ✅
verifyApiKeyfrom/lib/api-auth.tssrc/app/api/Implementation Complete ✅
/api/pingendpoint with GET method and OPTIONS (CORS)Bug Fix: API Key Validation ✅
Issue: API was incorrectly returning "API key is required" for valid format API keys due to improper error handling when database connection fails.
Root Cause: The endpoint was masking database connection errors and returning a generic "API key is required" message even when a properly formatted API key was provided.
Solution: Refactored the validation flow to:
Result: Now correctly returns:
Requirements Met ✅
Implementation Details
/api/pingwith GET and OPTIONS methodsAuthorization: Bearer <api_key>headerX-API-Key: <api_key>header?api_key=<api_key>query parameterTesting Results ✅
API Examples
The implementation is minimal, secure, and follows all existing patterns in the Nextlog codebase. The API key validation issue has been resolved.
Fixes #161.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.