Search code examples
wso2wso2-esbwso2-api-manager

How to Chain Multiple API Calls in Integration Studio with WSO2 Synapse?


ISSUE Description:

I'm working on a project in Integration Studio that involves making a series of HTTP API calls, where each subsequent call depends on the response of the previous one. I'm using WSO2 Synapse for this integration.

I've searched for documentation and examples on how to achieve this, but I couldn't find a clear and concise resource that explains how to chain these API calls effectively.

I have a sample code snippet that demonstrates the kind of behavior I'm trying to achieve:

<?xml version="1.0" encoding="UTF-8"?>
<template name="Test" xmlns="http://ws.apache.org/ns/synapse">
    <sequence>
        <!-- Initial setup and payload preparation -->
        <!-- First API call -->
        <send>
            <endpoint>
                <http method="get">
                    <!-- Configuration for the first API call -->
                </http>
            </endpoint>
        </send>
        <!-- Process the response from the first API call -->
        <!-- Prepare payload for the second API call -->
        <send>
            <endpoint>
                <http method="get">
                    <!-- Configuration for the second API call -->
                </http>
            </endpoint>
        </send>
        <!-- Process the response from the second API call -->
        <!-- Continue with more API calls if needed -->
    </sequence>
</template>

I would appreciate it if someone could provide guidance on how to structure the Synapse sequence to achieve this behavior. Are there any best practices, examples, or documentation that I might have missed?

Here are The used Versions

  • wso2am 4.1.0
  • WSO2-Integration-Studio 8.1.0

Thank you for your assistance!


Solution

  • If you need to do multiple HTTP calls in the same flow, you need to use the Call Mediator instead of Send Mediator which will allow you to pause the flow until the response is received. If you use the send mediator the response will arrive at the Out sequence. Once you receive the response you can use the Payloadfactory Mediator to construct a response body to send to the next endpoint. Something like below.

    <?xml version="1.0" encoding="UTF-8"?>
    <template name="Test" xmlns="http://ws.apache.org/ns/synapse">
        <sequence>
            <payloadFactory media-type="json">
              <format>{ "something": "123456" }</format>
              <args></args>
            </payloadFactory>
            <call>
                <endpoint>
                    <http method="get">
                        <!-- Configuration for the first API call -->
                    </http>
                </endpoint>
            </call>
            <!-- Extract something from the response -->
            <property description="Get Actual Fee" expression="json-eval($.actualFee)" name="prop1" scope="default" type="STRING"/>
            <!-- Prepare payload for the second API call -->
            <payloadFactory media-type="json">
              <format>{ "somethingElse":$1 }</format>
              <args>
                  <arg evaluator="xml" expression="$ctx:prop1"/>
              </args>
            </payloadFactory>
            <call>
                <endpoint>
                    <http method="get">
                        <!-- Configuration for the second API call -->
                    </http>
                </endpoint>
            </call>
            <!-- Process the response from the second API call -->
            <!-- Continue with more API calls if needed -->
        </sequence>
    </template>
    

    Also take a look at this tutorial.