{"id":99626,"date":"2024-04-12T11:43:48","date_gmt":"2024-04-12T03:43:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/99626.html"},"modified":"2024-04-12T11:43:49","modified_gmt":"2024-04-12T03:43:49","slug":"%e4%bd%bf%e7%94%a8firebase-functions%e8%b0%83%e7%94%a8%e5%a4%96%e9%83%a8api","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/99626.html","title":{"rendered":"\u4f7f\u7528Firebase Functions\u8c03\u7528\u5916\u90e8API"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02154820\/a6f77d15-fa4f-4dc2-81dc-5d3dd71a21eb.webp\" alt=\"\u4f7f\u7528Firebase Functions\u8c03\u7528\u5916\u90e8API\" \/><\/p>\n<p><p>Firebase Functions\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u7f16\u5199\u5728\u670d\u52a1\u5668\u7aef\u8fd0\u884c\u7684\u4ee3\u7801\uff0c\u800c\u65e0\u9700\u8bbe\u7acb\u4e13\u95e8\u7684\u670d\u52a1\u5668\u3002<strong>\u901a\u8fc7Firebase Functions\u8c03\u7528\u5916\u90e8API\u975e\u5e38\u4fbf\u6377\u3001\u5b89\u5168\u4e14\u53ef\u6269\u5c55<\/strong>\u3002\u8c03\u7528\u5916\u90e8API\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u5404\u79cd\u529f\u80fd\uff0c\u4f8b\u5982\u83b7\u53d6\u5929\u6c14\u4fe1\u606f\u3001\u8fdb\u884c\u652f\u4ed8\u5904\u7406\u7b49\u3002\u5176\u4e2d\u6700\u5173\u952e\u7684\u4e00\u70b9\u662f\uff0cFirebase Functions\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b89\u5168\u7684\u73af\u5883\u6765\u5b58\u50a8\u654f\u611f\u4fe1\u606f\uff0c\u5982API\u5bc6\u94a5\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4e86\u9ad8\u5ea6\u7684\u81ea\u5b9a\u4e49\u80fd\u529b\u548c\u5b9e\u65f6\u6027\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aHTTP\u89e6\u53d1\u7684Function\uff0c\u5f53\u6536\u5230\u8bf7\u6c42\u65f6\uff0c\u5b83\u4f1a\u8c03\u7528\u4e00\u4e2a\u5916\u90e8\u5929\u6c14\u670d\u52a1API\uff0c\u4ece\u800c\u83b7\u53d6\u5b9e\u65f6\u5929\u6c14\u4fe1\u606f\u5e76\u54cd\u5e94\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u8bbe\u7f6eFIREBASE FUNCTIONS\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u8c03\u7528\u5916\u90e8API\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Node.js\u548cFirebase CLI\uff0c\u5e76\u4e14\u5df2\u7ecf\u5728\u672c\u5730\u73af\u5883\u4e2d\u521d\u59cb\u5316Firebase Functions\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5 <strong>Node.js<\/strong>\uff0c\u5b83\u662fFirebase Functions\u7684\u8fd0\u884c\u73af\u5883\u3002\u7136\u540e\uff0c\u901a\u8fc7npm\u5b89\u88c5Firebase CLI\u5de5\u5177\uff0c\u8fd9\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\u53ef\u4ee5\u8ba9\u4f60\u521b\u5efa\u5e76\u7ba1\u7406\u4f60\u7684Firebase\u9879\u76ee\u548cFunctions\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u521d\u59cb\u5316Firebase\u9879\u76ee\uff0c\u8fd0\u884c<code>firebase init<\/code>\uff0c\u5e76\u9009\u62e9Functions: Configure and deploy Cloud Functions\u3002\u8fd9\u6837\u5c31\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u57fa\u7840\u914d\u7f6e\u7684Functions\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199FUNCTIONS\u4ee5\u8c03\u7528\u5916\u90e8API<\/h3>\n<\/p>\n<p><p>\u5f00\u59cb\u7f16\u5199Functions\u524d\u9700\u4e86\u89e3\u7684\u4e00\u4e9b\u57fa\u7840\u662f\uff1aFirebase Functions\u57fa\u4e8eNode.js\uff0c\u6240\u4ee5\u4f60\u5c06\u4f1a\u7528JavaScript\u6216\u8005TypeScript\u6765\u5199Function\u3002\u4e00\u4e2a\u7b80\u5355\u7684Function\u6837\u4f8b\u4f1a\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const functions = require(&#039;firebase-functions&#039;);<\/p>\n<p>const axios = require(&#039;axios&#039;); \/\/ \u4f7f\u7528axios\u8fdb\u884cHTTP\u8bf7\u6c42<\/p>\n<p>exports.getWeather = functions.https.onRequest(async (request, response) =&gt; {<\/p>\n<p>  try {<\/p>\n<p>    const weatherApiResponse = aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t axios.get(&#039;http:\/\/api.openweathermap.org\/data\/2.5\/weather&#039;, {<\/p>\n<p>      params: {<\/p>\n<p>        q: &#039;London,uk&#039;,<\/p>\n<p>        appid: &#039;YOUR_API_KEY&#039;<\/p>\n<p>      }<\/p>\n<p>    });<\/p>\n<p>    response.json(weatherApiResponse.data);<\/p>\n<p>  } catch (error) {<\/p>\n<p>    console.error(&quot;Error calling the weather service: &quot;, error);<\/p>\n<p>    response.status(500).send(error);<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u4f7f\u7528\u4e86<code>axios<\/code>\u8fd9\u4e2aHTTP\u5ba2\u6237\u7aef\u6765\u53d1\u8d77\u5bf9\u5916\u90e8\u5929\u6c14\u670d\u52a1\u7684\u8c03\u7528\u3002<strong>\u6ce8\u610f\u5b89\u5168\u5730\u5b58\u50a8API\u5bc6\u94a5<\/strong>\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5c06\u5176\u5b58\u50a8\u5728Firebase\u7684\u73af\u5883\u914d\u7f6e\u4e2d\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5c06\u5bc6\u94a5\u516c\u5f00\u5728\u4ee3\u7801\u4ed3\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u90e8\u7f72FUNCTIONS\u5e76\u8fdb\u884c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u90e8\u7f72\u4f60\u7684Function\u4e4b\u524d\uff0c\u5148\u5728\u672c\u5730\u8fdb\u884c\u8c03\u8bd5\u3002Firebase CLI\u63d0\u4f9b\u4e86\u672c\u5730\u8fd0\u884cFunctions\u7684\u80fd\u529b\u3002\u4f7f\u7528<code>firebase serve --only functions<\/code>\u53ef\u4ee5\u5728\u672c\u5730\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\uff0c\u6a21\u62dfCloud Functions\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u8c03\u8bd5\u5b8c\u6210\u5e76\u786e\u4fdd\u4e00\u5207\u6b63\u5e38\u5de5\u4f5c\u540e\uff0c\u4f7f\u7528<code>firebase deploy --only functions<\/code>\u547d\u4ee4\u628a\u4f60\u7684Functions\u90e8\u7f72\u5230\u4e91\u4e0a\u3002\u90e8\u7f72\u5b8c\u6210\u540e\uff0c\u4f60\u7684Function\u5c06\u4f1a\u63a5\u6536\u5230\u6765\u81eaFirebase\u4e91\u7aef\u7684\u8bf7\u6c42\u5e76\u89e6\u53d1\u4f60\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u90e8\u7f72\u540e\u5bc6\u5207\u5173\u6ce8\u65e5\u5fd7\u548c\u6027\u80fd\u76d1\u63a7\u4fe1\u606f<\/strong>\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u53d1\u73b0\u6f5c\u5728\u7684\u95ee\u9898\u5e76\u5feb\u901f\u54cd\u5e94\u3002Firebase\u63d0\u4f9b\u4e86\u65e5\u5fd7\u67e5\u770b\u529f\u80fd\u6765\u8ddf\u8e2a\u4f60\u7684Functions\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b89\u5168\u63aa\u65bd\u548c\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5f53\u6d89\u53ca\u5230\u8c03\u7528\u5916\u90e8API\u65f6\uff0c\u5fc5\u987b\u8c28\u614e\u5904\u7406\u5b89\u5168\u6027\u3002\u9996\u5148\uff0c\u786e\u4fdd<strong>\u4fdd\u62a4\u4f60\u7684API\u5bc6\u94a5\u4e0d\u88ab\u6cc4\u9732<\/strong>\u3002\u6b64\u5916\uff0c\u4f7f\u7528HTTPS\u8bf7\u6c42\u6765\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u7684\u52a0\u5bc6\u3002\u5bf9\u4e8e\u51fd\u6570\u4ee3\u7801\u672c\u8eab\uff0c\u786e\u4fdd\u6ca1\u6709\u66b4\u9732\u5bb9\u6613\u906d\u53d7\u653b\u51fb\u7684\u7aef\u70b9\u6216\u903b\u8f91\u7f3a\u9677\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c<strong>\u63a7\u5236\u51fd\u6570\u7684\u8c03\u7528\u9891\u7387\u548c\u6765\u6e90<\/strong>\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u51fd\u6570\u7684\u8c03\u7528\u914d\u989d\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u9a8c\u8bc1HTTP\u8bf7\u6c42\u5934\u4e2d\u7684API\u5bc6\u94a5\u6765\u786e\u8ba4\u8bf7\u6c42\u662f\u5426\u6765\u81ea\u4e8e\u6388\u6743\u7684\u6765\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u76d1\u6d4b\u548c\u4f18\u5316FUNCTIONS\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u76d1\u89c6Function\u7684\u6027\u80fd\u548c\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u81f3\u5173\u91cd\u8981\u3002\u4f7f\u7528Firebase\u7684\u76d1\u63a7\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u8ffd\u8e2a\u5230\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u7684\u6570\u636e\uff0c\u5982\u6267\u884c\u65f6\u95f4\u3001\u5185\u5b58\u4f7f\u7528\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u4e3a\u4e86\u4f18\u5316Function\u7684\u54cd\u5e94\u65f6\u95f4\u548c\u8d44\u6e90\u6d88\u8017\uff0c<strong>\u8003\u8651\u51fd\u6570\u7684\u51b7\u542f\u52a8\u65f6\u95f4\u548c\u8d44\u6e90\u5229\u7528\u7b56\u7565<\/strong>\u3002\u4f8b\u5982\uff0c\u53ef\u91cd\u7528\u7684\u5916\u90e8API\u7684\u8fde\u63a5\u5e94\u8be5\u88ab\u7f13\u5b58\uff0c\u4ee5\u51cf\u5c11\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u65f6\u7684\u521d\u59cb\u5316\u6210\u672c\u3002<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u786e\u4fdd<strong>\u4ee3\u7801\u9ad8\u6548\u4e14\u5c3d\u53ef\u80fd\u65e0\u72b6\u6001<\/strong>\uff0c\u56e0\u4e3a\u65e0\u72b6\u6001\u51fd\u6570\u80fd\u66f4\u5bb9\u6613\u5730\u6269\u5c55\uff0c\u5e76\u51cf\u5c11\u8d44\u6e90\u6d88\u8017\u3002\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5916\u90e8API\u8c03\u7528\uff0c\u53ea\u5728\u5fc5\u8981\u65f6\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u5176\u4ed6FIREBASE\u670d\u52a1\u548c\u5916\u90e8API<\/h3>\n<\/p>\n<p><p>Firebase Functions\u5e76\u4e0d\u662f\u5b64\u7acb\u5b58\u5728\u7684\uff0c\u4f60\u53ef\u4ee5\u4e0eFirebase\u5176\u4ed6\u670d\u52a1\u5982Firestore\u3001Authentication\u7b49\u7ed3\u5408\u8d77\u6765\uff0c\u5b9e\u73b0\u66f4\u52a0\u5f3a\u5927\u548c\u590d\u6742\u7684\u5e94\u7528\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aFunction\uff0c\u5f53\u5b8c\u6210\u7528\u6237\u8ba4\u8bc1\u4e8b\u4ef6\u65f6\uff0c\u8c03\u7528\u5916\u90e8API\u6765\u589e\u5f3a\u7528\u6237\u7684\u8d44\u6599\u4fe1\u606f\u3002\u8fd9\u79cd\u96c6\u6210\u53ef\u4ee5\u4f7f\u5f97\u5e94\u7528\u66f4\u52a0\u667a\u80fd\u548c\u54cd\u5e94\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c<strong>Firebase Functions\u4e3a\u8c03\u7528\u5916\u90e8API\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5e73\u53f0<\/strong>\u3002\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u5145\u5206\u5229\u7528Firebase\u63d0\u4f9b\u7684\u76d1\u63a7\u548c\u4f18\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u786e\u4fdd\u4f60\u7684Functions\u5b89\u5168\u3001\u9ad8\u6548\u5730\u8fd0\u884c\u3002\u901a\u8fc7\u7ed3\u5408Firebase\u7684\u5176\u4ed6\u670d\u52a1\uff0c\u53ef\u4ee5\u521b\u5efa\u51fa\u529f\u80fd\u5f3a\u5927\u7684\u5e94\u7528\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u4f18\u8d28\u4e14\u65e0\u7f1d\u7684\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Firebase Functions\u4e2d\u8c03\u7528\u5916\u90e8API\uff1f<\/strong><\/p>\n<p>\u8981\u5728Firebase Functions\u4e2d\u8c03\u7528\u5916\u90e8API\uff0c\u53ef\u4ee5\u4f7f\u7528Node.js\u5185\u7f6e\u7684HTTP\u8bf7\u6c42\u6a21\u5757\u6216\u4efb\u4f55\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982Axios\u6216Fetch\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728Firebase Functions\u9879\u76ee\u4e2d\u5b89\u88c5\u6240\u9700\u5e93\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6240\u9009\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u5230\u76ee\u6807API\u7684URL\uff0c\u5e76\u5904\u7406\u54cd\u5e94\u3002\u786e\u4fdd\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5f02\u6b65\u64cd\u4f5c\uff0c\u4ee5\u514d\u963b\u585e\u8bf7\u6c42\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u51fd\u6570\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Axios\u5e93\u5728Firebase Functions\u4e2d\u8c03\u7528\u5916\u90e8API\uff1a<\/p>\n<pre><code>const functions = require(&#039;firebase-functions&#039;);\nconst axios = require(&#039;axios&#039;);\n\nexports.callAPITest = functions.https.onRequest(async (req, res) =&gt; {\n  try {\n    const response = await axios.get(&#039;https:\/\/api.example.com\/data&#039;);\n    \/\/ \u8fd9\u91cc\u53ef\u4ee5\u5904\u7406API\u54cd\u5e94\n    res.send(response.data);\n  } catch (error) {\n    console.error(error);\n    res.status(500).send(&#039;Error calling external API&#039;);\n  }\n});\n<\/code><\/pre>\n<p>\u6b64\u793a\u4f8b\u51fd\u6570\u4f7f\u7528<code>axios.get<\/code>\u53d1\u9001GET\u8bf7\u6c42\u5230&#039;<a href=\"https:\/\/api.example.com\/data'%EF%BC%8C%E5%B9%B6%E5%B0%86%E5%93%8D%E5%BA%94%E5%8F%91%E9%80%81%E5%9B%9E%E8%B0%83%E7%94%A8%E6%96%B9%E3%80%82%E6%82%A8%E5%8F%AF%E4%BB%A5%E6%A0%B9%E6%8D%AE%E8%87%AA%E5%B7%B1%E7%9A%84%E9%9C%80%E6%B1%82%E4%BF%AE%E6%94%B9%E5%92%8C%E6%89%A9%E5%B1%95%E6%AD%A4%E5%87%BD%E6%95%B0%EF%BC%8C%E4%BB%A5%E5%9C%A8Firebase\">https:\/\/api.example.com\/data&#039;\uff0c\u5e76\u5c06\u54cd\u5e94\u53d1\u9001\u56de\u8c03\u7528\u65b9\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u4fee\u6539\u548c\u6269\u5c55\u6b64\u51fd\u6570\uff0c\u4ee5\u5728Firebase<\/a> Functions\u4e2d\u8c03\u7528\u4efb\u4f55\u5916\u90e8API\u3002\u8bb0\u5f97\u5728\u51fd\u6570\u4e2d\u5904\u7406\u9519\u8bef\u60c5\u51b5\uff0c\u5982\u7f51\u7edc\u9519\u8bef\u6216API\u8bf7\u6c42\u5931\u8d25\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u6388\u6743\u624d\u80fd\u8c03\u7528\u5916\u90e8API\u5417\uff1f<\/strong><\/p>\n<p>\u662f\u5426\u9700\u8981\u6388\u6743\u6765\u8c03\u7528\u5916\u90e8API\u53d6\u51b3\u4e8eAPI\u7684\u8981\u6c42\u3002\u67d0\u4e9bAPI\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u5bc6\u94a5\uff0c\u5728\u4f7f\u7528\u4e4b\u524d\u60a8\u9700\u8981\u5148\u83b7\u53d6\u5bc6\u94a5\u3002\u60a8\u53ef\u4ee5\u5728\u8bf7\u6c42\u4e2d\u5305\u542b\u8eab\u4efd\u9a8c\u8bc1\u5bc6\u94a5\uff0c\u6216\u8005\u5728Headers\u4e2d\u8bbe\u7f6e\u6388\u6743\u4ee4\u724c\u3002\u786e\u4fdd\u6309\u7167API\u6587\u6863\u4e2d\u7684\u6307\u793a\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\uff0c\u5e76\u6839\u636e\u9700\u8981\u5904\u7406\u4efb\u4f55\u6388\u6743\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5f02\u6b65API\u8c03\u7528\u7684\u54cd\u5e94\uff1f<\/strong><\/p>\n<p>\u5f53\u8c03\u7528\u5916\u90e8API\u65f6\uff0c\u54cd\u5e94\u901a\u5e38\u662f\u5f02\u6b65\u8fd4\u56de\u7684\u3002\u5728\u5904\u7406\u54cd\u5e94\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u7b49\u5f85API\u8c03\u7528\u5b8c\u6210\u3002\u5728Node.js\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>async\/await<\/code>\u6216<code>Promise<\/code>\u6765\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3002\u5728Firebase Functions\u4e2d\uff0c\u5efa\u8bae\u4f7f\u7528<code>async\/await<\/code>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u51fd\u6570\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>async\/await<\/code>\u5904\u7406\u5f02\u6b65API\u8c03\u7528\u7684\u54cd\u5e94\uff1a<\/p>\n<pre><code class=\"language-javascript\">exports.callAPITest = functions.https.onRequest(async (req, res) =&gt; {\n  try {\n    const response = await axios.get(&#039;https:\/\/api.example.com\/data&#039;);\n    \/\/ \u8fd9\u91cc\u53ef\u4ee5\u5904\u7406API\u54cd\u5e94\n    res.send(response.data);\n  } catch (error) {\n    console.error(error);\n    res.status(500).send(&#039;Error calling external API&#039;);\n  }\n});\n<\/code><\/pre>\n<p>\u901a\u8fc7\u5728\u51fd\u6570\u5b9a\u4e49\u4e2d\u4f7f\u7528<code>async<\/code>\u5173\u952e\u5b57\u548c\u5728API\u8c03\u7528\u524d\u4f7f\u7528<code>await<\/code>\u5173\u952e\u5b57\uff0c\u51fd\u6570\u5c06\u7b49\u5f85API\u8c03\u7528\u5b8c\u6210\uff0c\u5e76\u5728\u6536\u5230\u54cd\u5e94\u540e\u7ee7\u7eed\u6267\u884c\u3002\u5728\u5f02\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528<code>try\/catch<\/code>\u5757\u6765\u5904\u7406\u9519\u8bef\u5e76\u5411\u8c03\u7528\u65b9\u53d1\u9001\u9002\u5f53\u7684\u9519\u8bef\u54cd\u5e94\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Firebase Functions\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u7f16\u5199\u5728\u670d\u52a1\u5668\u7aef\u8fd0\u884c\u7684\u4ee3\u7801\uff0c\u800c\u65e0\u9700\u8bbe\u7acb\u4e13\u95e8\u7684\u670d\u52a1\u5668\u3002\u901a\u8fc7Fir [&hellip;]","protected":false},"author":3,"featured_media":99628,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/99626"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=99626"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/99626\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/99628"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=99626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=99626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=99626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}