The Netlify function wasn't working because of a URL format mismatch:
- App calls:
/.netlify/functions/upc/product/123456789012 - Function expects:
/.netlify/functions/upc?barcode=123456789012
I've updated netlify/functions/upc.js to handle both formats:
- β
Query parameter:
?barcode=123 - β
Path-based:
/product/123or/product/123/
After deploying, test these URLs in your browser:
Query parameter format:
https://your-site.netlify.app/.netlify/functions/upc?barcode=0123456789012
Path format (now supported):
https://your-site.netlify.app/.netlify/functions/upc/product/0123456789012
Both should return JSON product data.
Make sure in Netlify Dashboard β Site Settings β Environment Variables:
Required:
UPC_API_KEY=4190D3F1E6057DD921DA7E426A79AAF3SPOONACULAR_API_KEY=6da1ac7558c34c9d9c314d1172952a6a
Scopes: Make sure "Functions" is checked β
- Go to Netlify Dashboard β Functions
- Click on
upcfunction - View logs to see if there are errors
- Look for:
- Missing
UPC_API_KEYerrors - Network errors
- API response errors
- Missing
- Open your Netlify site
- Open DevTools (F12) β Console
- Scan a barcode
- Look for:
- Network requests to
/.netlify/functions/upc - Any error messages
- Check if
ITEM_INFO_PROXY_URLis set correctly
- Network requests to
Solution:
- Verify
netlify/functions/upc.jsexists in your repo - Check that function was deployed (should see it in Functions tab)
- Trigger a new deploy
Solution:
- Go to Netlify Dashboard β Environment Variables
- Verify
UPC_API_KEYis set - Make sure "Functions" scope is checked
- Trigger a new deploy after adding variables
Solution:
- The function handles CORS automatically
- If you still see CORS errors, check browser console
- Make sure you're calling the function from the same domain
Solution:
- Check Netlify function logs
- Verify UPC Database API is accessible
- Check if API key is valid
- Function has 10-second timeout by default
-
Push the fix:
git add netlify/functions/upc.js git commit -m "Fix Netlify function to handle path-based URLs" git push -
Wait for Netlify to rebuild (automatic)
-
Test again:
- Test function directly (see URLs above)
- Test in your app
- Check browser console for errors
-
Check if function is deployed:
- Netlify Dashboard β Functions β Should see
upcandrecipe
- Netlify Dashboard β Functions β Should see
-
Test function directly:
- Use the test URLs above
- Should return JSON (even if error, means function works)
-
Check environment variables:
- Netlify Dashboard β Environment Variables
- Verify all required variables are set
-
Check browser console:
- Open DevTools β Console
- Look for errors when scanning
- Check Network tab for API calls
-
Check function logs:
- Netlify Dashboard β Functions β upc β View logs
- Look for errors or missing variables
After the fix:
- β
Function accepts both
?barcode=123and/product/123formats - β
App automatically detects Netlify and uses
/.netlify/functions/upc - β Product information loads when scanning barcodes
- β No CORS errors
- β Function logs show successful API calls
The fix has been pushed. After Netlify rebuilds, test again!