Dynamically Setting the endpoint URI in WSO2 API-Manager

Lashan Sivaganeshan
2 min readApr 21, 2018

--

Hi Folks,

The WSO2 API-Manager provides a comprehensive API management platform to cater various requirements related to managing APIs, which includes maintaining a production and a sandbox endpoint for a given API. The following post explains a mechanism which can be used to set the production or the sandbox endpoint URL dynamically during the run time by passing the URL as a parameter in the request.

Note: If the endpoint URLs are known and if they need to be changed and applied in the run time based on the request, it’s possible set the “To” header with the use of a “filter” mediator as explained in the following blog [1]. The following post contrasts from the above methodology because here the production/sandbox endpoint URL is passed as a parameter of the request and it’s not defined in the synapse configurations of the API-Manager.

In order to retrieve the endpoint URL as a parameter of the request, it requires to set the endpoint URL as a variable as follows while creating the API from the API-Manager’s publisher portal. This was tested in API-Manager-2.1.0.

Setting the Endpoint as a variable

Once the API is saved in the above format the related synapse-config available in <APIM-Home>/repository/deployment/server/synapse-configs/default/api will look as follows.

Please note the following properties which assist the assignment of the endpoint URL.

  1. uri.var.urlForBank expression=”$url:DynamicUrl”
  2. ENDPOINT_ADDRESS value= “{uri.var.urlForBank}”
<?xml version="1.0" encoding="UTF-8"?>
<api name="admin--DynamicTest" context="/test/1" version="1" version-type="context">
<resource methods="POST PATCH GET DELETE PUT" url-mapping="/*" faultSequence="fault">
<inSequence>
<sequence name="changeEndpointForEachBank_Seq">
<log level="full">
<property name="message" value="In seq changeEndpointForEachBank_Seq" />
</log>
<property name="uri.var.urlForBank" expression="$url:DynamicUrl" />
<log level="full">
<property expression="get-property('uri.var.urlForBank')" name="messageUrlValueMock" scope="default" type="STRING" />
</log>
</sequence>
<property name="api.ut.backendRequestTime" expression="get-property('SYSTEM_TIME')" />
<filter source="$ctx:AM_KEY_TYPE" regex="PRODUCTION">
<then>
<send>
<endpoint name="admin--DynamicTest_APIproductionEndpoint_0">
<http uri-template="{uri.var.urlForBank}" />
<property name="ENDPOINT_ADDRESS" value="{uri.var.urlForBank}" />
</endpoint>
</send>
</then>
<else>
<send>
<endpoint name="admin--DynamicTest_APIsandboxEndpoint_0">
<http uri-template="{uri.var.urlForBank}" />
<property name="ENDPOINT_ADDRESS" value="{uri.var.urlForBank}" />
</endpoint>
</send>
</else>
</filter>
</inSequence>
<outSequence>
<class name="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtResponseHandler" />
<send />
</outSequence>
</resource>
<handlers>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.common.APIMgtLatencyStatsHandler" />
<handler class="org.wso2.carbon.apimgt.gateway.handlers.security.CORSRequestHandler">
<property name="apiImplementationType" value="ENDPOINT" />
</handler>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler" />
<handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler" />
<handler class="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtUsageHandler" />
<handler class="org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtGoogleAnalyticsTrackingHandler">
<property name="configKey" value="gov:/apimgt/statistics/ga-config.xml" />
</handler>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler" />
</handlers>
</api>

If the endpoint URL related to the above created API is http://localhost:9000/endpoint1, the API can be invoked while setting the DynamicUrl parameter in the request as follows.

curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 'http://localhost:8280/test/1/?DynamicUrl=http://localhost:9000/endpoint1'

Note: It’s possible to send the above endpoint URL in the request as a payload parameter as well. In that case, the expression for the above “uri.var.urlForBank” will require to be set as $ctx:DynamicUrl.

Thanks.

[1]. https://blog.lakmali.com/2016/08/dynamic-endpoints-in-wso2-api- manager.html

--

--

Lashan Sivaganeshan

What you search is out there. It's a matter of pressing the right keys.