You shouldn’t have to be a Swagger expert to design and build an API. Creating an API from scratch can be a difficult task, so what if you could do it without writing a line of code?
The Web Integrator in TIBCO Cloud Integration is an API-led platform that makes it straightforward to get started building REST services. Think about the message you want to receive and the response you want to send back — that’s all you need to define your API.
What we’re building #
A FlightBookings app that sends an email confirmation to the user requesting air travel.
Create the app #
Like every app on TIBCO Cloud Integration, start with the orange Create button. The Web Integrator is powered by TIBCO’s OSS Project Flogo and the mascot (Flynn) is in the interface. Click on Create a Web Integrator App to get started.
Create the flow #
Click Create a flow and select Rest Trigger. This is where you configure your API. The resource path is what your API listens on — best practice is to use the plural form of a noun (books, apps, or in our case, flightbookings). The HTTP methods supported by Web Integrator are:
- GET: Get the resource specified (e.g. get a single book or get all books)
- POST: Create a new resource of the object (e.g. create a new flightbooking)
- PUT: Update a resource or create a new one if it doesn’t exist yet (e.g. update an app or create a new one)
- DELETE: Remove the resource (e.g. delete the cookie)
We’re creating new flightbookings, so the method is POST. Rather than writing JSON schema by hand, the Web Integrator lets you paste a sample message for input and output. Copy this into the input box and click Create:
{
"Class": "string",
"DepartureDate": "2017-05-27",
"Destination": "string",
"FirstName": "string",
"EmailAddress": "string"
}Obviously you can have more fields, but this is good enough for now
Implement logic #
Now for the business logic. Click on the newly created flow and you’ll see a canvas with two tiles: ReceiveHTTPMessage (triggers the flow) and ReplyToHTTPMessage (sends a response to the client). We’ll do three things:
- Add a log tile to log that a new message arrived
- Add an email tile to send a confirmation
- Update the
ReplyToHTTPMessageto send back the data we need
Adding a log tile #
Click on ReplyToHTTPMessage and drag it two spaces over to make room for new tiles.
Click the first empty tile and select Log Message. It needs an input — on the Input click message to craft the log message. There are plenty of functions available, but we’ll use a simple string concatenation. You can type in the textbox or click on parameters and functions on the right side. Or just copy this:
string.concat("A new booking request has arrived for ", $TriggerData.body.FirstName)This function concatenates a string with the FirstName passed in as a parameter
Sending an email #
For the email step, I covered the Send Mail activity setup in a previous post so I’ll skip the connection details. The email activity needs 4 inputs:
- sender: who the message comes from (e.g. some@email.com)
- recipients: the email address from the request. Click on recipients and search for EmailAddress inside $TriggerData (or use `$TriggerData.body.EmailAddress`)
- subject: the subject line (e.g. thank you for requesting a flight)
- message: the email body, using concat again to personalize it:
string.concat("Dear ", string.concat($TriggerData.body.FirstName, ". Thank you for requesting a flight. Please note we'll take care of it soon!"))Updating the ReplyToHTTPMessage #
Instead of echoing back the input, update the Input Settings with a new response sample. We want to reply with the FirstName, a unique identifier, and the current date:
{
"FirstName": "string",
"ID": "string",
"Date": "string"
}Paste that in and the Input tab fields update automatically. Map them:
- FirstName:
$TriggerData.body.FirstName - ID:
number.random(999999) - Date:
datetime.currentDate()
Push your app #
Everything’s mapped and configured. Click the blue Push app button to deploy.
Test it #
After pushing, you’ll be back on the Apps page. Click View and Test 1 Endpoint then View API to open the test page. On the POST method, the only required item is the body:
{
"Class": "string",
"DepartureDate": "string",
"Destination": "string",
"EmailAddress": "string",
"FirstName": "string"
}Be sure to replace the value of EmailAddress with an actual email address to make sure you see the result :)
The response body should look something like:
{
"Date": "2017-08-15+00:00",
"FirstName": "string",
"ID": 623436
}That’s it #
A few steps and you’ve got a working API for flight bookings. As always let me know your thoughts on this tutorial either by commenting below or posting something on the TIBCO Community!