All Collections
Tutorials
API Builder/Gateway
3. API Tutorial - Route to Get a Book by ID
3. API Tutorial - Route to Get a Book by ID

Create an API Route to extract a book from our database

Praise Magidi avatar
Written by Praise Magidi
Updated over a week ago

In this tutorial, you will learn how to create an API route to an endpoint that will extract a specific book, identified by its ID, from the BookStore database.

The target route you are going to build and test should look something like this:

https://rest.synatic.com/indri/bookstore-api/Books/{id}

When executing the API request, add the id of the book's record to the end of the URL. The response should return the specific book's information.


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

The BookStore API has already been created in the API tutorial - Part 2, so you only need to create the required Flow and Paths and then add them to the API.

This tutorial will guide you through creating a GET operation to extract a book identified by its ID only.

  1. To do this, you must first create a backing Synatic flow that will include the logic to access the database and extract the data, which you then connect to the API.

  2. Then you'll create an API path and connect the Flow to the API path.

  3. Lastly, you can test the Route using Postman.


Create the backing Flow for the Get Book by ID route

This flow requires the following steps to be configured; Input Parameters, JSON, and SQL Server.

Open the Solution menu:

  1. To create a new Flow, click ⨁ adjacent to the Flows option.

  2. Give it the name Get Book by ID and a meaningful description.

  3. Click Save to open the flow designer canvas.

To get a book by its ID, you must first create an Input Parameter for the ID in the flow.

Synatic - Flow Input Parameters

Select the Input Parameters step in the flow.

Synatic - Flow input parameter create
  1. Enter 'id' for the parameter name.

  2. Select the data type as Integer and the attribute as required.

  3. Click Save.

You will also be accessing and reading from the SQL database:

  1. So, let's insert the SQL Server step from the Source drawer in the Steps palette.

  2. Select and drag it to the flow.

  3. The SQL Server step configuration opens.

Synatic - API SQL Server Step Command

SQL Server database connection

The connection to the database has already been created in the API tutorial - Part 2, so you just need to select the connection.

  1. Click on the SQL Server connection field.

  2. Select the BookStore Connection.


Write the Query

Now you can enter the SQL Query script to read a specific book selected by its ID from the database.

Enter the following in the SQL Command code block:

  1. SELECT * FROM tutorials.BookStore WHERE id = {{parameters.id}};
    πŸ“ Synatic uses the Handlebars template {{ ... }} to access parameters.

  2. Click Save Step.

Synatic - Flow Designer canvas

Test the Connection and the Query

It's always going to be a good idea, and advisable to test the query before continuing.

  1. Click the blue Run Test button on the Flow to run a simulation.
    πŸ“ You'll see that an Input Parameter entry field is now available.

  2. Enter the ID of the book that you want to retrieve.

Synatic - API Test Input Parameter

Test the Query

  1. Click Test to execute the query.

  2. You should check the response to verify that the connection is working and that the record was retrieved successfully.


Connect the Flow to the API Route

Now, to complete the API, you connect the Get Book by ID backing Flow.

  1. Open the API Builder again.

  2. Click ⨁ adjacent to the Paths tab to create a new Route.

  3. Select the GET operation and type in the Path's name /Books/{id}.
    πŸ“ The Definition tab requires no configuration at this stage.

  4. Select the Source tab and then the Flow tab.

  5. Scroll to select the Flow Get Book by ID from the available list.

Synatic - API Builder - Add Path

Test the API Route with Postman

To test the API route, you first connect to the API endpoint using the Postman platform:

  1. Open the API Builder again.

  2. Select the info tab and copy the Service URL.

  3. Launch Postman, select a GET request, and paste the URL.
    ​https://rest.synatic.com/indri/bookstore-api

  4. Add '/Books/5' to the URL. (5 is the ID of the book.)

  5. Click Send to test.

  6. Now, check the response to verify that the correct book is returned.

Synatic - API Test

Did this answer your question?