Firebase Functions中使用Express调用PayPal API出现404错误的解决求助
Let's break down exactly why you're hitting that 404 error and walk through how to fix it:
Root Issues
Looking at your error logs and code, two critical mistakes are causing this problem:
Misplaced Proxy Configuration
YourgenerateLinkfunction adds a proxy pointing tolocalhost:5001(your local Firebase Functions server). This means Axios is routing your PayPal API request to your own local service instead of PayPal's sandbox endpoint. Since your server doesn't have the/v2/customer/partner-referralsroute, you get a 404.Incorrect Payload Placement
You're passing the PayPal request payload via theparamsoption in Axios, which appends the data as URL query parameters. But PayPal'spartner-referralsendpoint expects the payload in the request body (standard for POST requests), not as URL params. Even if you fixed the proxy, this would cause a validation error from PayPal.
Step-by-Step Fixes
1. Remove the Unnecessary Proxy
Delete the proxy property from your Axios request entirely. You don't need a proxy to call PayPal's public sandbox API.
2. Move Payload to data Instead of params
Change the params key to data in your Axios config. This ensures the payload is sent as JSON in the request body, matching how you tested it successfully in Postman.
Fixed generateLink Function
export const generateLink = async ( requestBody: Record<string, any> | null = null, ): Promise<any> => { const result = await axios({ url: 'https://api-m.sandbox.paypal.com/v2/customer/partner-referrals', method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${config.paypal.access_token}`, }, // Use data for POST request body (instead of params) data: requestBody ?? requestBodyEx, // Proxy line removed entirely }); return result; };
Quick Additional Checks
- Confirm
config.paypal.access_tokenis valid and not expired. If it's hardcoded, grab a fresh token from PayPal's sandbox dashboard. - Double-check that
requestBodyExmatches the exact payload you used in Postman (same structure, all required fields included). - For local testing, ensure your machine has active internet access to reach PayPal's servers.
After making these changes, restart your local Firebase Functions server and test the endpoint again in Insomnia. This should resolve the 404 error and let your request reach PayPal's sandbox API correctly.
内容的提问来源于stack exchange,提问作者dean

