In this tutorial, you will learn how to create an API route to an endpoint that will update a specific book identified by its ID in 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

When executing this API request, you use a PUT request loaded with the JSON Object containing the book's changed details, add the id of the book's record to the end of the URL, then submit the request. The response should update the book details.

For the PUT Route, you will also be adding a Security Scheme to the API.


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 PUT operation to update a book identified by its ID only.

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

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

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


Create the backing Flow for the Update Book by ID route

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

Open the Solution menu:

  1. Click ⨁ adjacent to the Flows option to create a new Flow.

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

  3. Click Save to open the flow designer canvas.


Create an 'id' parameter in the flow

You will be accessing a SQL database that requires the Key ID in the database to link to the parameter ID.

To update 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 Parameter step in the flow.

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

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

  3. Click Save.


Add a JSON step

  1. You will be submitting a JSON data file to update the SQL database, so let's insert the JSON step from the Reader drawer in the Steps palette to read the book data from a JSON formatted script.

  2. Select and drag it to the flow.

  3. The JSON step opens. Click to save it as no configuration is required.


Add a Calculator step

  1. The Calculator step is required to add some JavaScript to link the 'id' parameter to the record.

  2. Let's insert the Calculator step from the Mapper drawer in the Steps palette to add the JavaScript code to link to the record in the database.

  3. Select and drag it to the flow.

  4. The Calculator step opens.

  5. Enter the following code in the JavaScript code block:
    let inputRecord=input.record;
    inputRecord.Id = input.parameters.Id;
    return inputRecord;


Add the SQL Server step

  1. You will also be accessing the SQL database to update a record, so let's insert the SQL Server step from the Destination drawer in the Steps palette.

  2. Select and drag it to the flow.

  3. The SQL Server step opens.

Synatic - Flow SQL Server update

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.


Select the Query type

Enter the SQL Query type to update an existing book in the database.

  1. Select the Update SQL Query type.

  2. In Update Options, enter the Object Name as 'tutorial.BookStore', the table name for the book data.

  3. In Column Mappings, enter the paths and columns with their related SQL Data Types and Field length as shown below.
    - It's necessary to map the incoming data to the associated database columns.
    - The names used in the JSON data file and the database must be the same.
    - For this tutorial, it's an easy process as they are both the same.

  4. Configure the Key Mapping structure to link the ID in the JSON data file to the ID in the record that must be updated.

Now click Save Step to confirm and preserve the column mappings.

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.

  2. Now you should add a JSON test object into the Capture Source Text code block, such as:
    {
    "Name": "Misery",
    "Author": "Stephen King",
    "User Rating": 4.6,
    "Reviews": 9847,
    "Price": 35,
    "Year": 1975,
    "Genre": "Suspense"
    }

  3. Click Test to execute the query.

  4. Check the response to verify that the update was successful.


Connect the Flow to the API Route

To complete the API, the Update Book by ID backing Flow must be connected.

  1. Open the API Builder again.

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

  3. Select the PUT operation and type in the Path's name /Books.

  4. Select the Definition tab and add the API Key Scheme.

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

  6. Scroll to select the Flow Update Book by ID from the available list.

Synatic - API Paths tab

Test the API Route with Postman

You can test your 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 PUT request, and paste the URL
    https://rest.synatic.com/indri/bookstore-api

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

  5. Select the Body tab and enter your JSON object:
    {
    "Name": "Misery",
    "Author": "Stephen King",
    "User Rating": 4.6,
    "Reviews": 9847,
    "Price": 35,
    "Year": 1975,
    "Genre": "Suspense"
    }

  6. Select the RAW tab and select JSON.

  7. Click Send to test.

Synatic - API Test

< API Route to POST a new Book API Path to DELETE a book >

Did this answer your question?