从 Azure 逻辑应用中的工作流部署和调用自定义 APIDeploy and call custom APIs from workflows in Azure Logic Apps

创建自定义 API 以供逻辑应用工作流使用后,必须先部署 API 才能调用它们。After you create custom APIs for use in logic app workflows, you must deploy your APIs before you can call them. 虽然可以将 API 部署为 Web 应用,但请考虑将 API 部署为 API 应用,便于更加轻松地在云端和本地生成、托管和使用 API。You can deploy your APIs as web apps, but consider deploying your APIs as API apps, which make your job easier when you build, host, and consume APIs in the cloud and on premises. 不必更改 API 中的任何代码 - 可直接将代码部署到 API 应用。You don't have to change any code in your APIs - just deploy your code to an API app. 可在 Azure App Service 上托管API,它是一款平台即服务 (PaaS) 产品,可提供简单的高缩放性 API 托管。You can host your APIs on Azure App Service, a platform-as-a-service (PaaS) offering that provides highly scalable, easy API hosting.

虽然可从逻辑应用调用任何 API,但为获得最佳体验,请添加 OpenAPI(以前称为 Swagger)元数据,用于说明 API 的操作和参数。Although you can call any API from a logic app, for the best experience, add OpenAPI (previously Swagger) metadata that describes your API's operations and parameters. 此 OpenAPI 文件有助于使 API 与逻辑应用集成更轻松、运行更顺畅。This OpenAPI file helps your API integrate more easily and work better with logic apps.

将 API 部署为 Web 应用或 API 应用Deploy your API as a web app or API app

若要从逻辑应用调用自定义 API,首先需将 API 作为 Web 应用或 API 应用部署到 Azure 应用服务。Before you can call your custom API from a logic app, deploy your API as a web app or API app to Azure App Service. 此外,若要使逻辑应用设计器能够读取 OpenAPI 文件,请为 Web 应用或 API 应用设置 API 定义属性并开启跨域资源共享 (CORS)Also, to make your OpenAPI file readable by the Logic Apps Designer, set the API definition properties and turn on cross-origin resource sharing (CORS) for your web app or API app.

  1. Azure 门户中,选择 Web 应用或 API 应用。In the Azure portal, select your web app or API app.

  2. 在打开的应用菜单的 API 下方,选择“API 定义” 。In the app menu that opens, under API, choose API definition. 将“API 定义位置”设置为 OpenAPI swagger.json 文件的 URL 。Set the API definition location to the URL for your OpenAPI swagger.json file.

    通常,URL 的格式为:https://{name}.azurewebsites.net/swagger/docs/v1)Usually, the URL appears in this format: https://{name}.azurewebsites.net/swagger/docs/v1)

    链接到自定义 API 的 OpenAPI 文件

  3. 在“API”下,选择“CORS” 。Under API, choose CORS. 将“允许的源” 的 CORS 策略设置为 '*' (允许所有)。Set the CORS policy for Allowed origins to '*' (allow all).

    此设置允许来自逻辑应用设计器的请求。This setting permits requests from Logic App Designer.

    允许从逻辑应用设计器向自定义 API 发送请求

有关详细信息,请参阅在 Azure 应用服务中使用 CORS 托管 RESTful APIFor more information, see Host a RESTful API with CORS in Azure App Service.

从逻辑应用工作流调用自定义 APICall your custom API from logic app workflows

设置 API 定义属性和 CORS 后,应可将自定义 API 的触发器和操作包含在逻辑应用工作流中。After you set up the API definition properties and CORS, your custom API's triggers and actions should be available for you to include in your logic app workflow.

  • 要查看具有 OpenAPI URL 的网站,可在逻辑应用设计器中浏览订阅网站。To view websites that have OpenAPI URLs, you can browse your subscription websites in the Logic Apps Designer.

  • 若要通过指向 OpenAPI 文档查看可用操作和输入,请使用 HTTP + Swagger 操作To view available actions and inputs by pointing at an OpenAPI document, use the HTTP + Swagger action.

  • 无论调用任何 API(包括不具有或未公开 OpenAPI 文档的 API),始终可以使用 HTTP 操作创建请求。To call any API, including APIs that don't have or expose an OpenAPI document, you can always create a request with the HTTP action.

后续步骤Next steps