Developing REST APIs with Ktor

Developing REST APIs with Ktor

Photo by Compare Fibre on Unsplash

As developers, we deal with REST APIs on daily basis, as mobile/frontend developers, to consume these APIs and as Backend developers to develop these APIs. As an Android dev, I was always curious about backend technologies and how this API related stuff works under the hood. And I am sure this holds true for most frontend as well as Android/iOS devs. But now the important question for me now was how to choose the right backend technology for your app. Being an Android dev, I was quite familiar with Kotlin, and when I learnt about Ktor I straight away started with the same, and the developing experience has been amazing so far.

Ktor is a framework to easily build connected applications — web applications, HTTP services, mobile and browser applications.

Let’s quickly get started and develop basic notes API with CRUD operations in Ktor!

Endpoints

The API endpoints for our notes API will be -:

1. Create a new note

POST 0.0.0.0:3536/notes

2. Fetch all notes

GET http://0.0.0.0:3536/notes

3. Fetch a particular note

GET http://0.0.0.0:3536/notes/{id}

4. Delete a particular note

DELETE http://0.0.0.0:3536/notes/{id}

5. Update a particular note

PUT http://0.0.0.0:3536/notes/{id}

Database

After doing some research on databases, I chose Ktorm.

Ktorm is a lightweight and efficient ORM Framework for Kotlin directly based on pure JDBC. It provides strong-typed and flexible SQL DSL and convenient sequence APIs to reduce our duplicated effort on database operations.

The fact that this ORM framework provides DSL support for SQL queries encouraged me to pick it up. Kotlin gives you the tools to help craft code into something which feels more natural to use, through DSL, and DSL support for SQL queries, definitely helps us write more clean and understandable code. The development time also reduces a lot as writing raw SQL queries may require some time and efforts, especially for non-backend developers. Ktorm generates raw SQL queries for you under the hood.

Ktorm Query to Insert into Database :

Generated SQL:

Routing

Routing is one of the most important aspects to be token into consideration when developing REST APIs. Routing defines the endpoints of our APIs. How our API knows which action to perform on which URL or various endpoints is through Routing. Ktor provides a convenient way of Routing using the Routing Plugin provided by Ktor framework. Here is the representation of the fetch notes API endpoint. We will place this in our main function of Application.kt file.

Request and Response

While dealing with REST APIs, we deal with parameters, headers, auth , request body, response body etc. I assume you understand the basics of the same. In case you’re not familiar with all these terminologies, I urge you to read about the same and then switch back to current article.In this article we will only discuss about how to deal with Request and Response bodies in Ktor.

1. Request Body

a) Create a Request Model Class which represents a Request Body of our Create Notes API:

b) Get the Request Body parameters in the Routing Function:

2. Response Body

a) Create a Response Model Class which represents a Response Body of our Fetch Notes API:

b) Handle returning the response body on API call by the client.

Connecting the dots

We are now ready to connect the dots and complete our project. Yayy!
I will list the relevant code as per the scope of the article.In case you want to display the whole project, please check the source code mentioned at the end of the article.

Code for NotesRoute.kt:

For complete code, check out the repo ktor-api
Thanks for your patience, will be back soon with a new article related to Ktor and continue the project. Stay tuned.

Did you find this article valuable?

Support Hitesh's blog by becoming a sponsor. Any amount is appreciated!