| title | Azure API Management policy reference - invoke-dapr-binding | Microsoft Docs |
|---|---|
| description | Reference for the invoke-dapr-binding policy available for use in Azure API Management. Provides policy usage, settings, and examples. |
| services | api-management |
| author | dlepow |
| ms.service | azure-api-management |
| ms.topic | reference |
| ms.date | 07/23/2024 |
| ms.author | danlep |
[!INCLUDE api-management-availability-premium-dev]
The invoke-dapr-binding policy instructs API Management gateway to trigger an outbound Dapr binding. The policy accomplishes that by making an HTTP POST request to http://localhost:3500/v1.0/bindings/{{bind-name}}, replacing the template parameter and adding content specified in the policy statement.
The policy assumes that Dapr runtime is running in a sidecar container in the same pod as the gateway. Dapr runtime is responsible for invoking the external resource represented by the binding. Learn more about Dapr integration with API Management.
[!INCLUDE api-management-policy-generic-alert]
<invoke-dapr-binding name="bind-name" operation="op-name" ignore-error="false | true" response-variable-name="resp-var-name" timeout="in seconds" template="Liquid" content-type="application/json">
<metadata>
<item key="item-name"><!-- item-value --></item>
</metadata>
<data>
<!-- message content -->
</data>
</invoke-dapr-binding>| Attribute | Description | Required | Default |
|---|---|---|---|
| name | Target binding name. Must match the name of the bindings defined in Dapr. Policy expressions are allowed. | Yes | N/A |
| operation | Target operation name (binding specific). Maps to the operation property in Dapr. Policy expressions aren't allowed. | No | None |
| ignore-error | If set to true instructs the policy not to trigger "on-error" section upon receiving error from Dapr runtime. Policy expressions aren't allowed. |
No | false |
| response-variable-name | Name of the Variables collection entry to use for storing response from Dapr runtime. Policy expressions aren't allowed. | No | None |
| timeout | Time (in seconds) to wait for Dapr runtime to respond. Can range from 1 to 240 seconds. Policy expressions are allowed. | No | 5 |
| template | Templating engine to use for transforming the message content. "Liquid" is the only supported value. | No | None |
| content-type | Type of the message content. "application/json" is the only supported value. | No | None |
| Element | Description | Required |
|---|---|---|
| metadata | Binding specific metadata in the form of key/value pairs. Maps to the metadata property in Dapr. | No |
| data | Content of the message. Maps to the data property in Dapr. Policy expressions are allowed. | No |
- Policy sections: inbound, outbound, on-error
- Policy scopes: global, product, API, operation
- Gateways: self-hosted
Dapr support must be enabled in the self-hosted gateway.
The following example demonstrates triggering of outbound binding named "external-systems" with operation named "create", metadata consisting of two key/value items named "source" and "client-ip", and the body coming from the original request. Response received from the Dapr runtime is captured in the "bind-response" entry of the Variables collection in the context object.
If Dapr runtime fails for some reason and responds with an error, the "on-error" section is triggered and response received from the Dapr runtime is returned to the caller verbatim. Otherwise, default 200 OK response is returned.
The "backend" section is empty and the request is not forwarded to the backend.
<policies>
<inbound>
<base />
<invoke-dapr-binding
name="external-system"
operation="create"
response-variable-name="bind-response">
<metadata>
<item key="source">api-management</item>
<item key="client-ip">@(context.Request.IpAddress )</item>
</metadata>
<data>
@(context.Request.Body.As<string>() )
</data>
</invoke-dapr-binding>
</inbound>
<backend>
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
<return-response response-variable-name="bind-response" />
</on-error>
</policies>[!INCLUDE api-management-policy-ref-next-steps]