In this tutorial, you will learn how to create an API route to an endpoint that will delete 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 DELETE request loaded with the JSON Object, add the id of the book's record to the end of the URL, then submit the request. The response should delete the identified book.
For the DELETE 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 DELETE operation to remove a book identified by its ID only.
To do this, you must first create a backing Synatic flow that will access the database to delete the data, which you then connect to the API.
Then you will create an API path and connect the Flow to the API.
Lastly, you can test the Route using Postman.
Create the backing Flow for the Delete Book by ID route
This flow requires the following steps to be configured; Input Parameters, JSON, and SQL Server.
Open the Solution menu:
Click ⨁ adjacent to the Flows option to create a new Flow.
Give it the name 'Delete Book by ID' and a meaningful description.
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 delete a book by its ID, you must first create an Input Parameter for the ID in the flow.
Select the Input Parameter step in the flow.
Enter 'id' for the parameter name.
Select the data type as Integer and also the required attribute.
Click Save.
Add a JSON step
You will be submitting a JSON data file to delete a record from the SQL database, so let's insert a JSON step from the Reader drawer in the Steps palette to read a JSON formatted script.
Select and drag it to the flow.
The JSON step opens. Click to save it as no configuration is required.
Add the SQL Server step
You will be accessing the SQL database to delete a record, so let's insert the SQL Server step from the Destination drawer in the Steps palette.
Select and drag it to the flow.
The SQL Server step opens.
SQL Server database connection
When the Flow is saved, the SQL Server step configuration will open.
The connection to the database already exists.
You just need to select a connection.
Click on the SQL Server connection field and select the BookStore Connection.
Write the Query
Now you can enter the SQL Query to delete a specific book selected by its ID from the database.
Select the Delete Query type.
Enter the following in the SQL Command code block:
DELETE
FROM
tutorials.BookStore
WHERE
id = {{parameters.id}};
📝 Synatic uses the Handlebars template {{ ... }} to access parameters.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.
📝 You'll see that an Input Parameter entry field is now available.Enter the ID of the book that you want to delete.
Test the Query
Click Test to execute the query.
You should check the response to verify that the connection is working and that the record was deleted successfully.
Connect the Flow to the API Route
To complete the API, the Delete Book by ID backing Flow must be connected.
Open the API Builder again.
Click ⨁ adjacent to the Paths tab to create a new Route.
Select the DELETE operation and type in the Path's name /Books/{id}.
Select the Definition tab and add the API Key Scheme you created earlier.
Select the Source tab and then the Flow tab.
Scroll to select the Flow Delete Book from the available list.
Test the API Route with Postman
You can test your API endpoint using the Postman platform.
Open the API Builder again.
Select the info tab and copy the Service URL.
Launch Postman, select a DELETE request and paste the URL
https://rest.synatic.com/indri/bookstore-api
Add
'/Books/5'
to the URL. (5 is the ID of the book.)Click Send to test the deletion.