Dynamic Endpoints in WSO2 API Manager

From WSO2 APIM 1.10.0, we have introduced new feature to define dynamic endpoints through synapse default endpoints support. In this blog article, I am going to show how we can create an API with dynamic endpoints in APIM.

Assume that you have a scenario where depending on the request payload, the backend URL of the API differs. For instance, if the value of "operation" element in the payload is "menu", you have to route the request to endpoint1 and else you need to route the request to endpoint2.


{
 "srvNum": "XXXX",
 "operation": "menu"
}

In APIM, dynamic endpoints are achieved through mediation extension sequences. For more information about mediation extensions refer this documentation.

For dynamic endpoints we have to set the "To" header with the endpoint address through a mediation In-flow sequence. So let's first create the sequence which sets the "To" header based on the payload. Create a file named dynamic_ep.xml with below content.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="dynamic_ep">
   <property expression="json-eval($.operation)" name="operation" />
   <filter regex="menu" source="$ctx:operation">
      <then>
         <header name="To" value="https://localhost:9448/am/sample/pizzashack/v1/api/menu" />
      </then>
      <else>
         <header name="To" value="https://localhost:9448/defaultep" />
      </else>
   </filter>
   <property expression="get-property('To')" name="ENDPOINT_ADDRESS" />
</sequence>

Supporting Destination based usage tracing for dynamic endpoints.
In here note that we have to set "ENDPOINT_ADDRESS" additional property with the "To" header value which is required for populating destination address for statistics (API Usage by Destination). So if you have statistics enabled in your APIM setup, you have to set this property as well with the endpoint value in order to see the destination address in the statistic views.

Now let's assign this sequence to the API. For that go to the "Implement" tab of the API creation wizard.

  • Select "Dynamic Endpoint" as the "Endpoint Type"
  • Upload dynamic_ep.xml to the "In Flow" under message mediation policies. 
  • Save the API

Now let's try out the API.

With Payload containing "menu"



Wire log showing request going to endpoint 1.


With Payload NOT containing "menu".


Wire log showing request going to endpoint 2.

This way you can write your own logic using mediation extensions and dynamic endpoints in APIM to route your requests to dynamic destinations. 

Comments

  1. Hi Lakmali,

    as you can see the APIm always appends the resource part of your API to the new endpoint, i. e.

    https://localhost:9448/defaultep

    goes to

    https://localhost:9448/defaultep/get

    Do you know if this behavior can be avoided?

    ReplyDelete
    Replies
    1. i am facing same issue. Were you able to find a solution around this

      Delete
    2. I have the same problem. Is there any solution for this.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Hi, I found a configuration to remove REST_URL_POSTFIX property and added it at top.
      Referred: https://medium.com/wso2-learning/wso2-api-manager-tutorial-on-building-apis-with-urls-95fef8c455b9

      Line to add at top:

      Delete
  2. Lakmali Erandi Baminiwatta's, good night, how do I get the Production endpoint, from Message Mediation Policies sequence ?

    ReplyDelete

Post a Comment

Popular posts from this blog

PHP-SOAP web service with out a WSDL

Boomi Mapping - Removing special chars from an input

How to add Maven dependency from a relative path referencing a local jar