Connect to SaaS or B2B Systems¶
In the previous tutorial, you learned how to route and transform messages, deploy, and test integrations in WSO2 Micro Integrator (MI). In this tutorial, you’ll learn how to create a loan review email notification flow that sends an email based on the client's loan status.
What you'll build¶
Let's consider a scenario where a client sends a loan request to the Bank
API deployed in WSO2 Micro Integrator. Upon receiving the request, the API sends an email notification to the client indicating that the loan request has been received and is under review. This is done using a SaaS-based email service.
In this example, you will use Gmail's SMTP service as the email provider.
What you'll learn¶
- How to integrate and send emails using the Email connector.
Prerequisites¶
-
You need Visual Studio Code (VS Code) with the Micro Integrator for VS Code extension installed.
Info
See the Install Micro Integrator for VS Code documentation to learn how to install Micro Integrator for VS Code.
-
You must have completed the Route and Transform messages tutorial under Build your first Integration before proceeding. Start the Route and Transform messages tutorial if you haven’t completed it yet.
Now, it's time to design the email notification flow. Follow the steps below to create the email notification integration.
Step 1 - Create a new API resource¶
To develop the above scenario, let's get started with creating a new API resource in the Bank
API.
-
Click on the Service Designer (
) icon of the
Bank
API in the Micro Integrator Project Explorer to open the Service Designer. -
In the Service Designer, click the + Resource button to add a new API resource.
-
In the Add API Resource pane, set
/loan-review
as the Resource Path and select thePOST
method. -
Finally, click Create to add the new API resource.
Step 2 - Design the integration¶
-
Open the Resource View of the newly created API resource by clicking the
POST /loan-review
resource under Available resources in the Service Designer. -
After opening the Resource View, click on the Start node on the canvas to set an input payload for the integration flow.
Note
Setting an input payload for the integration flow is not mandatory. However, it is recommended, as it will be used to enable expression suggestions, which you will explore in later steps of this tutorial.
-
Click Add Request, provide the following JSON payload, then click Add. Finally, click Save to complete the input payload setup.
{ "customerId": "C567", "name": "Jane Smith", "email": "[email protected]", "amount": 25000 }
-
Click on the + icon on the canvas to open the Mediator Palette.
-
Search for
email
in the Mediator Palette, then click the download () icon to add the Email connector to the project. In the confirmation pane, select Yes to add the required dependencies.
What is a connector?
- Connectors in WSO2 Micro Integrator (MI) enable seamless integration with external systems, cloud platforms, and messaging services without the need for custom implementations. They provide a standardized way to send, receive, and process data from third-party applications like Salesforce, Kafka, and AWS services. To explore connectors in detail, see the Connector documentation.
- In VS Code, you can view all available connectors by clicking Add Module under the Mediators tab in the Mediator Palette.
-
Once the connector is downloaded, select the
Send
operation from the Mediator Palette. -
Click + Add new connection to create a new connection.
-
Select
SMTPS
and fill in the following details to create a connection to Gmail's SMTP service. Finally, click Add in the Add New Connection form to create the connection.Tip
If two-factor authentication is enabled, you need to obtain an app password by following the instructions here.
Property Value Connection Name GmailConnection
Host smtp.gmail.com
Port 465
Username Your email address Password Your email password or app password Note
It is recommended to use secure vault to store credentials. For more information, refer to Encrypting Secrets.
-
After creating the connection, enter your email address in the From field and set
Loan Application Received
as the Subject in the Add Send form.In the next step, you will extract the recipient's email address from the incoming payload and assign it to the To field using an expression.
-
Click on the expression (
) icon to enable expression mode for the To field.
-
Once expressions are enabled, click on the expression editor (
) icon to open the editor.
-
Select Payload → email to extract the recipient's email address from the payload.
In the next step, you will use a template with inline expressions to extract data from the incoming payload and construct the email body.
Tip
You can use the following pre-filled template in the Content field to skip the inline expression insertion steps and continue from Step 16.
Dear ${payload.name}, We have received your loan application for $ ${payload.amount}. Our team will review your application and contact you shortly. Thank you, O2 Bank
-
In the Content field, enter the following template.
Dear , We have received your loan application for $ . Our team will review your application and contact you shortly. Thank you, O2 Bank
-
In the template, place your cursor in the appropriate location (next to
Dear
), click on the inline expression editor () icon, then select Payload → name, and click Add to insert the inline expression into the template.
-
Repeat the same steps to insert the loan amount after the
$
symbol in the template. -
Ensure that Overwrite Message Body is checked, then click Submit to add the email operation to the integration flow.
-
Click on the + icon located just after the Send operation to open the Mediator Palette to add a Respond Mediator to respond to the client request.
-
Select Respond mediator under Mediators, and click Add to insert it to the integration flow.
You have successfully updated the integration flow to send an email with the loan request status. For reference, you can review the configured API and Email connection.
Bank API
Info
You can view the source view by clicking on the Show Source (</>
) icon located in the top right corner of the VS Code.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/bankapi" name="BankAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" uri-template="/">
<inSequence>
<payloadFactory media-type="json" template-type="default">
<format>{
"greetings":"Welcome to O2 Bank !!"
}
</format>
</payloadFactory>
<respond/>
</inSequence>
<faultSequence>
</faultSequence>
</resource>
<resource methods="POST" uri-template="/deposit">
<inSequence>
<http.post configKey="CurrencyConverter">
<relativePath>/currency/rate</relativePath>
<headers>[]</headers>
<requestBodyType>JSON</requestBodyType>
<requestBodyJson>{
"fromCurrency": " ${payload.currency} ",
"toCurrency": "USD"
}
</requestBodyJson>
<forceScAccepted>false</forceScAccepted>
<disableChunking>false</disableChunking>
<forceHttp10>false</forceHttp10>
<noKeepAlive>false</noKeepAlive>
<forcePostPutNobody>false</forcePostPutNobody>
<responseVariable>http_post_1</responseVariable>
<overwriteBody>false</overwriteBody>
</http.post>
<filter xpath="${vars.http_post_1.attributes.statusCode == 200}">
<then>
<payloadFactory media-type="json" template-type="default">
<format>{ "status": "successful", "amountDeposited": ${payload.amount * vars.http_post_1.payload.rate} }</format>
</payloadFactory>
</then>
<else>
<payloadFactory media-type="json" template-type="default">
<format>{
"status": "unsuccessful",
"error": "${vars.http_post_1.payload.message}"
}
</format>
</payloadFactory>
</else>
</filter>
<respond/>
</inSequence>
<faultSequence>
</faultSequence>
</resource>
<resource methods="POST" uri-template="/loan-review">
<inSequence>
<email.send configKey="GmailConnection">
<from>ENTER_YOUR_EMAIL_HERE</from>
<personalName></personalName>
<to>{${payload.email}}</to>
<cc></cc>
<bcc></bcc>
<replyTo></replyTo>
<subject>Loan Application Received</subject>
<content>Dear ${payload.name},
We have received your loan application for $ ${payload.amount}. Our team will review your application and contact you shortly.
Thank you,
O2 Bank</content>
<contentType>text/html</contentType>
<encoding>UTF-8</encoding>
<attachments></attachments>
<inlineImages>[]</inlineImages>
<contentTransferEncoding>Base64</contentTransferEncoding>
<responseVariable>email_send_1</responseVariable>
<overwriteBody>true</overwriteBody>
</email.send>
<respond />
</inSequence>
<faultSequence>
</faultSequence>
</resource>
</api>
Email Connection
Info
You can view the source view by clicking on the Show Source (</>
) icon located in the top right corner of the VS Code.
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="GmailConnection" xmlns="http://ws.apache.org/ns/synapse">
<email.init>
<connectionType>SMTPS</connectionType>
<host>smtp.gmail.com</host>
<port>465</port>
<username>REPLACE</username>
<password>REPLACE</password>
<name>GmailConnection</name>
</email.init>
</localEntry>
Step 3 - Run the integration¶
Now that you have updated the integration, it's time to deploy the integration to the Micro Integrator server runtime.
Click the Build and Run icon located in the top right corner of VS Code.
Step 4 - Test the integration service¶
-
Once the Runtime Services interface is open, select the
BankAPI
, and click the Try It button. -
Select the
/loan-review
resource and click Try it Out to test the API. -
Provide a JSON payload and click the Execute button to invoke the API. You may use the following sample payload to test the API. Make sure to update the
name
andemail
fields with the actual recipient's information.{ "customerId": "C567", "name": "Jane Smith", "email": "[email protected]", "amount": 45000 }
-
Check the response received from the server, and verify that the loan request status email has been successfully delivered by checking the recipient’s inbox.
Congratulations! You have now learned how to integrate with an email SaaS provider using connectors to send emails as part of your integration flow.
What's Next?¶
You have now completed this tutorial series, where you learned how to build a complete integration flow step by step. Throughout this journey, you have explored how to create Integration APIs, route and transform messages, connect to external SaaS and B2B systems.
Now, you can explore advanced integration scenarios, applying what you’ve learned to real-world use cases and expanding your expertise in WSO2 Micro Integrator.
Try more tutorials and examples.