In this tutorial, you will learn how to create a new API and including an API Route to an endpoint that will extract a list of all books from the BookStore database.
The target route you are going to build and test should look something like this:
When executing the API request, the response should return the book information of all books in the BookStore database.
Watch the video below to learn how to create the API Route using the Synatic platform.
The following topics explain each step of the process as shown in the video.
Let's create an API
In Synatic, go to the API Builder menu and click ⨁ to add a new API. This new API will contain operations to manage books in the BookStore database.
Give it the name Bookstore and a meaningful description.
Select RESTful as the API type.
The Info tab of the API is opened and displays the following information:
Service - The service API endpoint is automatically created and represents your base URL.
Swagger - Synatic also creates a metadata document that describes your API in the Swagger format.
The Title and Description values default from the first time creation. You may make any edits to the Title and Description.
The first operation this tutorial will demonstrate is a GET operation and used to extract a list of all the books in the database.
To do this, you must first create a backing Synatic flow to access the database and extract the data.
The flow must then be connected to the API.
The first path you will create is the GET route, which uses the flow to call the database and execute an operation that extracts a list of all the books.
But first, you must create a new solution in Synatic.
Create the new Solution
In Synatic, go to the Solutions menu and click ⨁ to add a new Solution. This Solution will contain the API flows and all the backend logic.
For this tutorial, give it the name Bookstore Backend and a meaningful description.
Next, create the Flows to manage the data.
Create the backing Flow for the Get all Books route
This flow requires the following steps to be configured; JSON and SQL Server.
Open the Solution menu:
To create a new Flow, click ⨁ adjacent to the Flows option.
Give it the name Get All Books and a meaningful description.
Click Save to open the flow designer canvas.
You will be accessing and reading from a SQL database.
So, let's insert the SQL Server step from the Source drawer in the Steps palette.
Select and drag it to the flow.
The SQL Server step configuration opens.
Create the SQL Server database connection
Before continuing, you must first create a connection to the SQL Server database.
📝 Keep in mind that you could also connect to many other data sources, for example, MongoDB, PostgreSQL, Oracle, Dropbox, or plain flat files.
Click ⨁ adjacent to the SQL Server Connection to create a new Connection.
Give the connection a name. For this tutorial, use 'BookStore database'
Enter your SQL Server details and credentials.
Write the Query
Now enter the SQL Query script to read all books from the database.
Enter the following in the SQL Command code block:
SELECT * FROM tutorials.BookStore;
Click Save Step.
Test the Connection and the Query
It's always going to be a good idea, and advisable to test the query before continuing.
Click the blue Run Test button on the Flow to run a simulation then click Test to execute the query.
You should check the response to verify that the connection is working and that the returned data is correct.
Connect the Flow to the API Route
To complete the API, you can now connect the Get all Books backing Flow.
Open the API Builder again.
Click ⨁ adjacent to the Paths tab to create a new Route.
Select the GET operation and type in the Path's name /Books.
📝 The Definition tab requires no configuration at this stage.
Select the Source tab and then the Flow tab.
Scroll to select the Flow Get All Books from the available list.
Test the API Route with Postman
To test the API route, you first connect to the API endpoint using the Postman platform:
Open the API Builder again.
Select the Info tab and copy the Service URL.
Launch Postman, select a GET request, and paste the URL.
'/Books'to the end of the URL.
Click Send to test.
Now, check the response to verify that the correct list of books is returned.