dartapi 0.0.7 copy "dartapi: ^0.0.7" to clipboard
dartapi: ^0.0.7 copied to clipboard

DartAPI A lightweight, FastAPI-inspired framework for building scalable, high-performance REST APIs in Dart with built-in routing, middleware, and CLI support.

🚀 DartAPI - A Lightweight FastAPI-like Framework for Dart #

DartAPI is a lightweight and developer-friendly framework for building fast, modern, and scalable APIs using Dart.

📌 Features #

Fast and lightweight - Minimal dependencies, optimized for speed.
Easy to use - Simple setup and minimal boilerplate.
Configurable port - Start the server with a custom port (--port=<number>).
Dynamic routing - Automatically registers controllers and their routes.
Middleware support - Includes logging and future authentication middleware.
CLI Tool - Generate projects, controllers, and models using the dartapi CLI.


🔧 Installation #

To use DartAPI globally, install it via Dart's package manager:

dart pub global activate dartapi
copied to clipboard

After activation, you can use the dartapi CLI to create projects and manage your API.


📦 Creating a New API Project

dartapi create my_project
cd my_project
dart pub get
copied to clipboard

🚀 Running the Server

You can start the API server using:

1️⃣ Default Port (8080)

dartapi run
copied to clipboard

1️⃣ Custom Port (8080)

dartapi run --port=3000
copied to clipboard

Alternatively, run it directly via Dart:

dart run bin/main.dart --port=3000
copied to clipboard

✅ Expected Output:

🚀 Server running on http://localhost:3000


🔥 API Routes

The boilerplate comes with the following methods

Method Route Description
GET /users Fetch list of users
POST /users Create a new user

Example Request (Using cURL)

curl -X GET http://localhost:8080/users
copied to clipboard

✅ Response:

{"users": ["Christy", "Akash"]}
copied to clipboard

🛠 Generating a Controller

dartapi generate controller Product
copied to clipboard

✅ Creates:

lib/src/controllers/product_controller.dart
copied to clipboard

The generated controller includes:


import 'package:shelf/shelf.dart';
import 'base_controller.dart';

class ProductController extends BaseController {
  @override
  List<RouteDefinition> get routes => [
        RouteDefinition('GET', '/products', getAllProducts),
        RouteDefinition('POST', '/products', createProduct),
      ];

  Response getAllProducts(Request request) {
    return Response.ok('{"products": ["Laptop", "Phone"]}', headers: {'Content-Type': 'application/json'});
  }

  Response createProduct(Request request) {
    return Response.ok('{"message": "Product created"}', headers: {'Content-Type': 'application/json'});
  }
}
copied to clipboard

✅ Now accessible at:

•	GET /products
•	POST /products
copied to clipboard

🛠 Middleware

DartAPI includes middleware support. The default logging middleware logs all requests:

Example Middleware (lib/src/middleware/logging.dart)


import 'package:shelf/shelf.dart';

Middleware loggingMiddleware() {
  return (Handler innerHandler) {
    return (Request request) async {
      print("📌 Request: \${request.method} \${request.requestedUri}");
      final response = await innerHandler(request);
      return response;
    };
  };
}
copied to clipboard

✅ Adding Middleware in server.dart:


final handler = Pipeline()
    .addMiddleware(loggingMiddleware()) 
    .addHandler(_router.handler.call);

copied to clipboard

🗄 Database Setup (Planned Feature)

Currently, DartAPI provides a placeholder for database connections:


class Database {
  static void connect() {
    print('🔗 Connecting to database...');
  }
}

In future versions, we will support:
- ✅ PostgreSQL, SQLite, MongoDB
- ✅ Database models with dartapi generate model User
- ✅ Migrations (dartapi migrate db)
copied to clipboard

🎯 Planned Features

  • 📌 Swagger UI (/docs route for API documentation)
  • 📌 Authentication System (JWT Middleware)
  • 📌 WebSocket Support (/ws for real-time communication)
  • 📌 Database ORM Integration (PostgreSQL, SQLite, MongoDB)
  • 📌 Task Scheduling (Cron Jobs, Background Tasks)
  • 📌 Deployment Support (docker and dartapi deploy)

📝 License

This project is open-source under the MIT License.


🚀 Get Started Now!

dartapi create my_project
cd my_project
dart pub get
dartapi run --port=8080
copied to clipboard

✅ Start building APIs with Dart! 🚀🚀🚀


✅ Adding Auth! 🚀🚀🚀

Add the dartapi_auth package.

dart pub add dartapi_auth
copied to clipboard

Currently there is support for JWT using Auth Middleware.

   final jwtService = JwtService(
    accessTokenSecret: 'super-secret-key',
    refreshTokenSecret: 'super-refresh-secret',
    issuer: 'dartapi',
    audience: 'dartapi-users',
  );
copied to clipboard

Add the authMiddleware Middle ware to the route definition.

RouteDefinition('GET', '/users', getAllUsers, middlewares: [authMiddleware(jwtService)]),
copied to clipboard
5
likes
160
points
404
downloads

Publisher

verified publisherakashgk.com

Weekly Downloads

2024.09.10 - 2025.03.25

DartAPI A lightweight, FastAPI-inspired framework for building scalable, high-performance REST APIs in Dart with built-in routing, middleware, and CLI support.

Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

MIT (license)

Dependencies

path, shelf, shelf_router, shelf_static

More

Packages that depend on dartapi