Skip to content

KishanViramgama/ktor-api-client

Repository files navigation

Kotlin Ktor

🌐 ktor-api-client — Kotlin Ktor REST API with Firebase, MySQL & WebSockets

ktor-api-client is a powerful open-source Kotlin project that demonstrates how to build modern, scalable RESTful APIs and real-time chat applications using the Ktor framework.

This project provides a complete backend solution supporting both Firebase Firestore and MySQL for data storage. It features a robust real-time chat system powered by WebSockets, alongside essential authentication APIs (login, register, and user profile management) with support for local image uploads.

🚀 Why Use ktor-api-client?

  • ✅ Build production-ready REST APIs in Kotlin
  • 🔥 Integrates with Firebase Firestore and MySQL
  • 🧪 Fully working Ktor authentication API example
  • 📁 User image uploads stored locally in the /uploads directory
  • 💻 Perfect for backend developers learning Ktor + Kotlin + Firestore + MySQL

🔑 Features (REST API Endpoints)

🔐 /login (POST)

Authenticate user credentials.
Request Body:

{
  "email": "user@example.com",
  "password": "password123"
}

📝 /register (POST)

Register a new user.
Request Body (multipart/form-data):

  • name: String
  • email: String
  • password: String
  • userImage: File

📋 /users (GET)

Get a list of all registered users.

💬 /mysql/chat/{senderId}/{receiverId} (WebSocket)

Real-time chat using MySQL.
Connect via WebSocket to send and receive messages.

⚙️ How to Setup and Run (2026 Guide)

Follow these steps to get your Ktor + MySQL + Firebase backend up and running in minutes.

1️⃣ Clone the Repository

git clone https://github.com/KishanViramgama/ktor-api-client.git
cd ktor-api-client

2️⃣ MySQL Database Setup

  1. Install MySQL Server (v8.0 or later).
  2. Open MySQL Workbench or your preferred SQL terminal.
  3. Run the following SQL command to create the database:
    CREATE DATABASE login_register;
  4. Configure Credentials:
    • Open src/main/resources/application.conf.
    • Update the user and password fields under the storage block to match your local MySQL settings.
    • Default settings in this project:
      • user: root
      • password: MySql@2026

3️⃣ Firebase Configuration

  1. Go to the Firebase Console.
  2. Create a new project or select an existing one.
  3. Navigate to Project Settings > Service accounts.
  4. Click Generate new private key. A .json file will be downloaded.
  5. Add the file: Copy this JSON file into the src/main/resources/ directory.
  6. Link the file: Open src/main/kotlin/Application.kt, find the initFirebase() function, and update the filename to match your downloaded JSON:
    val serviceAccount = Application::class.java.getResourceAsStream("/your-firebase-file.json")

4️⃣ Server IP & Port Configuration

To allow mobile apps or other devices to connect, you must use your machine's Local IP Address.

  1. Find your IP:
    • Windows: Run ipconfig in CMD.
    • Mac/Linux: Run ifconfig or hostname -I.
  2. Update IP in Code:
    • Open src/main/kotlin/base/Base.kt.
    • Update IP_ADDRESS to your current local IP (e.g., 192.168.1.5).
    const val IP_ADDRESS = "your.local.ip.here"
  3. Verify Port: The default port is 8080. Ensure this port is open on your firewall.

5️⃣ Run the Server

Open the project in Android Studio or IntelliJ IDEA and run the ApplicationKt file, or use the terminal:

./gradlew run

Once started, you will see:

  • ✅ Connected to MySQL Database
  • ✅ Firebase Initialized
  • [main] INFO ktor.application - Responding at http://0.0.0.0:8080

💬 WebSocket Testing (Real-time Chat)

Since Postman (standard) doesn't always handle Ktor WebSockets perfectly, you can use the Postman WebSocket Request or a tool like wscat:

  1. Postman: Click New > WebSocket Request.
  2. URL: ws://192.168.5.240:8080/chat/user1/user2 (for Firebase) or ws://192.168.5.240:8080/mysql/chat/user1/user2 (for MySQL).
  3. Communication: Once connected, send a JSON message:
    { "text": "Hello!", "receiverId": "user2" }

🛠 Choosing Ktor Options (Advanced Setup)

If you are starting a new Ktor project and want to match this setup, use the Ktor Project Generator with these selections:

  • Project Type: Kotlin
  • Configuration Engine: Netty
  • Build System: Gradle Kotlin DSL
  • Plugins to Include:
    • Routing: For handling API endpoints.
    • Content Negotiation + kotlinx.serialization: For JSON support.
    • WebSockets: For real-time chat functionality.
    • Static Content: To serve uploaded images from the /uploads folder.
    • Exposed: For MySQL ORM integration.

💡 Use Cases

  • 🔐 Learn Ktor authentication
  • 🔄 REST API integration with Firebase + MySQL
  • 💬 Implement Real-time Chat using WebSockets
  • 🧰 Use as boilerplate for Kotlin backend projects
  • 🧪 Practice handling file uploads and image storage in Ktor

📌 Keywords for Search Optimization

ktor, kotlin, kotlin ktor, ktor rest api, ktor firebase mysql, authentication api, kotlin backend, ktor client server, kotlin rest api, ktor image upload, kotlin api example, ktor websocket chat, kotlin realtime chat, ktor mysql chat

📬 Postman API Collection

Test all APIs easily using the included Postman collection.
📥 Download Postman Collection

📱 Mobile Application Authentication Demo

Looking for a mobile application that demonstrates user login and registration using the same Ktor API provided in this project? Check out the companion Android demo app:

🔗 Android Demo App (Login & Register):
https://github.com/KishanViramgama/LoginRegister

This Android project is fully integrated with this API and showcases:

  • ✅ User registration flow
  • 🔐 User login handling
  • 🔗 Ktor client API integration
  • 🎨 Jetpack Compose UI (if applicable)

Clone and run the app on your emulator or device to experience the complete authentication flow in action.

🤝 Contributing

Pull requests and issues are welcome! Feel free to fork the project and enhance it.

📣 Let's Connect

Follow me on GitHub to stay updated with Kotlin, Ktor, and backend projects! 🌟

☕ Support

If you like this library and want to support my work, consider buying me a coffee.

About

A high-performance Kotlin Ktor REST API integrated with Firebase Firestore, MySQL (Exposed ORM), and WebSockets. Features a complete backend suite including Secure Authentication (Login/Register), Local Image Uploads, and a Real-time 1-to-1 Chat system.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages