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

DartAPI is a CLI-driven project generator and toolset for building typed REST APIs in Dart.

DartAPI CLI #

DartAPI is a modular and developer-friendly CLI tool for building robust, typed REST APIs using the Dart language.
Rather than acting as a heavy, opinionated framework, DartAPI provides powerful code generation tools that let you build scalable backend applications with clean architecture, JWT authentication, request validation, and PostgreSQL/MySQL support.


๐Ÿ“ฆ What It Does #

  • โœ… Project scaffolding (dartapi create)
  • โœ… Controller generation (dartapi generate controller)
  • โœ… Hot-reload style dev server with keyboard controls (dartapi run)
  • โœ… Integrated with:

๐Ÿš€ Installation #

Activate globally:

dart pub global activate dartapi

๐Ÿ“ CLI Commands #

dartapi create <project_name> #

Creates a full DartAPI project with:

  • bin/main.dart
  • Controllers (UserController, AuthController, ProductController)
  • Middleware (logging, auth)
  • JWT setup with dartapi_auth
  • DB support with dartapi_db
  • DTOs and validation helpers
  • Auto schema definitions for future Swagger support

There are no additional items we need to required to run the generated project.

Note: that this is not mandatory. You can remove the product controller and DB related code from the main.dart file if you don't need it.

But if you want to use the product controller, you need to install the postgres DB.

๐Ÿ”ง Step 1: Install PostgreSQL

โœ… On macOS (using Homebrew):

brew install postgresql@14
brew services start postgresql@14

โœ… On Ubuntu / Debian:

sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl enable postgresql
sudo systemctl start postgresql

โœ… On Windows:

โ€ข	Download the PostgreSQL installer from: https://www.postgresql.org/download/windows/
โ€ข	Use the graphical installer to complete the setup.

๐Ÿ“ฆ Step 2: Create a Database

Run the following commands in your terminal or psql:

psql postgres

Then in the psql prompt:

CREATE DATABASE dartapi_test;
CREATE USER dartuser WITH ENCRYPTED PASSWORD 'postgres';
GRANT ALL PRIVILEGES ON DATABASE dartapi_test TO dartuser;

Update your DbConfig in main.dart as per your configuration:


dartapi generate controller <Name> #

Adds a controller to an existing DartAPI project:

dartapi generate controller Product

Generates lib/src/controllers/product_controller.dart with GET and POST methods and proper typing.


dartapi run --port <port> #

Runs your DartAPI server using bin/main.dart.
You can control it interactively:

  • Type :q to quit
  • Type r to reload
dartapi run --port=8080

โ–ถ๏ธ Running a Generated Project #

After scaffolding, follow these steps to get the server running:

1. Create and enter your project

dartapi create my_app
cd my_app

2. Install dependencies

dart pub get

3. (Optional) Set up the database

If you want to use ProductController, make sure PostgreSQL is running and update the DbConfig in bin/main.dart with your credentials:

final config = const DbConfig(
  type: DbType.postgres,
  host: 'localhost',
  port: 5432,
  database: 'dartapi_test',
  username: 'postgres',
  password: 'yourpassword',          // โ† change this
  poolConfig: PoolConfig(maxConnections: 10),
);

Then create the products table:

CREATE TABLE products (
  id       SERIAL PRIMARY KEY,
  name     TEXT NOT NULL,
  price    NUMERIC(10, 2) NOT NULL,
  quantity INTEGER NOT NULL
);

If you don't need DB support, remove ProductController from main.dart and delete product_controller.dart.

4. Start the server

dartapi run --port=8080

You should see:

๐Ÿš€ Server running on http://localhost:8080

๐Ÿงช Testing with Postman #

The generated project includes three controllers with the following endpoints. Import the requests below into Postman to test them.

Auth endpoints #

POST /auth/login

Returns a JWT access token and refresh token.

  • URL: http://localhost:8080/auth/login
  • Method: POST
  • Body (raw JSON):
{
  "username": "admin@mail.com",
  "password": "1234"
}
  • Response:
{
  "accessToken": "<jwt_access_token>",
  "refreshToken": "<jwt_refresh_token>"
}

Copy the accessToken value โ€” you'll need it as a Bearer token for protected routes.


POST /auth/refresh

Exchanges a valid refresh token for a new access token.

  • URL: http://localhost:8080/auth/refresh
  • Method: POST
  • Body (x-www-form-urlencoded):
Key Value
refresh_token <your_refresh_token>
  • Response:
{
  "access_token": "<new_jwt_access_token>"
}

User endpoints #

GET /users

Returns a list of users. Requires authentication.

  • URL: http://localhost:8080/users
  • Method: GET
  • Headers:
Key Value
Authorization Bearer <access_token>
  • Response:
["Christy", "Akash"]

POST /users

Creates a new user.

  • URL: http://localhost:8080/users
  • Method: POST
  • Body (raw JSON):
{
  "name": "Jane",
  "age": 28,
  "email": "jane@example.com"
}
  • Response:
User Jane created

Product endpoints #

Requires a running PostgreSQL database with the products table created (see setup above).

All product endpoints require an Authorization: Bearer <access_token> header.

GET /products

Returns all products from the database.

  • URL: http://localhost:8080/products
  • Method: GET
  • Headers:
Key Value
Authorization Bearer <access_token>
  • Response:
[
  { "id": 1, "name": "Keyboard", "price": 29.99, "quantity": 15 }
]

POST /products

Inserts a new product into the database.

  • URL: http://localhost:8080/products
  • Method: POST
  • Headers:
Key Value
Authorization Bearer <access_token>
  • Body (raw JSON):
{
  "name": "Keyboard",
  "price": 29.99,
  "quantity": 15
}
  • Response:
{ "id": 1, "name": "Keyboard", "price": 29.99, "quantity": 15 }

Suggested Postman flow #

  1. Call POST /auth/login โ†’ copy accessToken
  2. Set a Postman environment variable token = <accessToken>
  3. Use Authorization: Bearer {{token}} in all protected requests
  4. Call POST /products to insert a product, then GET /products to verify

๐Ÿงฑ Generated Project Structure #

my_app/
โ”œโ”€โ”€ bin/
โ”‚   โ””โ”€โ”€ main.dart
โ”œโ”€โ”€ lib/
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ core/           # Server/router setup
โ”‚       โ”œโ”€โ”€ controllers/    # UserController, AuthController, etc.
โ”‚       โ”œโ”€โ”€ dto/            # DTOs with schema
โ”‚       โ”œโ”€โ”€ db/             # DB connection logic
โ”‚       โ”œโ”€โ”€ middleware/     # Auth/logging middleware
โ”‚       โ””โ”€โ”€ utils/          # Validation, helpers
โ”œโ”€โ”€ pubspec.yaml
โ””โ”€โ”€ analysis_options.yaml

โœ… Why Use DartAPI? #

  • Minimal but powerful
  • Follows clean architecture principles
  • Type-safe routing using ApiRoute<ApiInput, ApiOutput>
  • Built-in JWT auth and DB integration
  • Ready to extend with OpenAPI/Swagger

Checkout the Medium article for more details.


๐Ÿ“„ License #

BSD 3-Clause License ยฉ 2025 Akash G Krishnan
LICENSE


9
likes
0
points
1.62k
downloads

Publisher

verified publisherakashgk.com

Weekly Downloads

DartAPI is a CLI-driven project generator and toolset for building typed REST APIs in Dart.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

path, shelf, shelf_router, shelf_static

More

Packages that depend on dartapi