How to send messages to Azure Service Bus from Azure API Management

Availability

Important

This feature is available in the Premium, Standard, Basic, and Developer tiers of API Management.

This article describes how to send messages from API Management to Azure Service Bus by using policy-based integration. Use API Management to provide a secure and scalable way to send messages to Service Bus.

Diagram of integration of API Management with Service Bus for messaging.

Azure Service Bus is a fully managed enterprise messaging service designed to decouple applications and services, enabling reliable cloud messaging between distributed systems. It supports AMQP (Advanced Message Queuing Protocol) for systems to send messages to message queues for one-to-one communication and topics for publish/subscribe patterns. Service Bus is ideal for scenarios requiring asynchronous operations, load leveling, or integration across hybrid cloud environments. For more information, see the Azure Service Bus documentation.

With policy-based integration, API Management provides:

  • Secure REST-based messaging for external clients - External systems and mobile apps that lack native AMQP support can send messages to Service Bus by using standard HTTP/REST APIs via API Management. This approach simplifies integration and enhances security by eliminating the need for custom intermediaries.
  • Governed third-party integrations - Enterprises can expose Service Bus endpoints through API Management with built-in managed identity authentication, enabling secure and observable messaging patterns for partners and third-party applications.
  • Fire and forget model - No other backend services are required to send messages to Service Bus, allowing for simpler architectures and reduced operational overhead.

Note

  • Integration of API Management with Service Bus is currently in preview.
  • Only sending messages to Service Bus is supported.

Prerequisites

Configure access to the service bus

To send messages to the service bus, you need to configure a managed identity for your API Management instance:

  1. Enable a system-assigned or user-assigned managed identity for API Management in your API Management instance.

    • If you enable a user-assigned managed identity, take note of the identity's Client ID.
  2. Assign the identity the Azure Service Bus Data Sender role, scoped to the service bus. To assign the role, use the Azure portal or other Azure tools.

Configure send-service-bus-message policy

Configure the send-service-bus-message policy to send messages to the desired queue or topic.

For example, use the send-service-bus-message policy in the inbound policy section to send the request body of an API request when the gateway receives it:

  1. In the Azure portal, go to your API Management instance.

  2. In the left-hand menu, select APIs, then select the API where you want to add the policy.

  3. Select All operations.

  4. At the top of the screen, select the Design tab.

  5. In the Inbound processing or Outbound processing window, select the </> (code editor) icon. For more information, see How to set or edit policies.

  6. Position your cursor in the inbound or outbound policy section.

  7. Add the send-service-bus-message policy to the policy configuration, then configure the attributes and elements as needed.

    For example, send the request body as a message:

    <send-service-bus-message queue-name="my-queue">
        <payload>@(context.Request.Body.As<string>())</payload>
    </send-service-bus-message>
    

    You can use any expression that returns a string as the value for the payload element.

  8. Select Save to save the updated policy configuration. As soon as you save it, the policy is active and messages are sent to the designated service bus.