AzuraJS Logo
AzuraJSFramework
v2.2 Beta

Quick Start

Build your first API with AzuraJS in minutes

Quick Start 🚀

Build your first RESTful API with AzuraJS in just a few minutes!

Step 1: Create Configuration File ⚙️

TypeScript

Create azura.config.ts in your project root:

azura.config.ts
import type { ConfigTypes } from "azurajs/config";

const config: ConfigTypes = {
  environment: "development",
  server: {
    port: 3000,
    cluster: false,
    ipHost: true,
    https: false,
  },
  logging: {
    enabled: true,
    showDetails: true,
  },
  plugins: {
    cors: {
      enabled: true,
      origins: ["*"],
      methods: ["GET", "POST", "PUT", "DELETE", "PATCH"],
      allowedHeaders: ["Content-Type", "Authorization"],
    },
    rateLimit: {
      enabled: false,
      limit: 100,
      timeframe: 60000, // 1 minute
    },
  },
};

export default config;

JavaScript

Create azura.config.js in your project root:

azura.config.js
const config = {
  environment: "development",
  server: {
    port: 3000,
    cluster: false,
    ipHost: true,
    https: false,
  },
  logging: {
    enabled: true,
    showDetails: true,
  },
  plugins: {
    cors: {
      enabled: true,
      origins: ["*"],
      methods: ["GET", "POST", "PUT", "DELETE", "PATCH"],
      allowedHeaders: ["Content-Type", "Authorization"],
    },
    rateLimit: {
      enabled: false,
      limit: 100,
      timeframe: 60000, // 1 minute
    },
  },
};

module.exports = config;

Step 2: Create Your Server 🎯

TypeScript with Decorators

Create controller with decorators in src/controllers/UserController.ts:

src/controllers/UserController.ts
import { Controller, Get, Post, Put, Delete, Body, Param, Res } from "azurajs/decorators";
import type { ResponseServer } from "azurajs";

// In-memory database (for demo purposes)
const users = [
  { id: 1, name: "Alice", email: "[email protected]" },
  { id: 2, name: "Bob", email: "[email protected]" },
];

@Controller("/api/users")
export class UserController {
  
  // GET /api/users - Get all users
  @Get()
  getAllUsers(@Res() res: ResponseServer) {
    res.json({ 
      success: true, 
      data: users 
    });
  }

  // GET /api/users/:id - Get user by ID
  @Get("/:id")
  getUserById(@Param("id") id: string, @Res() res: ResponseServer) {
    const user = users.find(u => u.id === Number(id));
    
    if (!user) {
      return res.status(404).json({ 
        success: false, 
        error: "User not found" 
      });
    }
    
    res.json({ success: true, data: user });
  }

  // POST /api/users - Create a new user
  @Post()
  createUser(@Body() body: any, @Res() res: ResponseServer) {
    const newUser = {
      id: users.length + 1,
      name: body.name,
      email: body.email,
    };
    
    users.push(newUser);
    
    res.status(201).json({ 
      success: true, 
      data: newUser 
    });
  }

  // PUT /api/users/:id - Update user
  @Put("/:id")
  updateUser(
    @Param("id") id: string, 
    @Body() body: any, 
    @Res() res: ResponseServer
  ) {
    const user = users.find(u => u.id === Number(id));
    
    if (!user) {
      return res.status(404).json({ 
        success: false, 
        error: "User not found" 
      });
    }
    
    if (body.name) user.name = body.name;
    if (body.email) user.email = body.email;
    
    res.json({ success: true, data: user });
  }

  // DELETE /api/users/:id - Delete user
  @Delete("/:id")
  deleteUser(@Param("id") id: string, @Res() res: ResponseServer) {
    const index = users.findIndex(u => u.id === Number(id));
    
    if (index === -1) {
      return res.status(404).json({ 
        success: false, 
        error: "User not found" 
      });
    }
    
    users.splice(index, 1);
    
    res.json({ 
      success: true, 
      message: "User deleted successfully" 
    });
  }
}

JavaScript with Functional API

Create server with functional routing in src/index.js:

src/index.js
const { AzuraClient } = require("azurajs");
const { createLoggingMiddleware } = require("azurajs/middleware");

// In-memory database (for demo purposes)
const users = [
  { id: 1, name: "Alice", email: "[email protected]" },
  { id: 2, name: "Bob", email: "[email protected]" },
];

// Create the application instance
const app = new AzuraClient();

// Add logging middleware
const logger = createLoggingMiddleware(app.getConfig());
app.use(logger);

// GET /api/users - Get all users
app.get("/api/users", (req, res) => {
  res.json({ 
    success: true, 
    data: users 
  });
});

// GET /api/users/:id - Get user by ID
app.get("/api/users/:id", (req, res) => {
  const { id } = req.params;
  const user = users.find(u => u.id === Number(id));
  
  if (!user) {
    return res.status(404).json({ 
      success: false, 
      error: "User not found" 
    });
  }
  
  res.json({ success: true, data: user });
});

// POST /api/users - Create a new user
app.post("/api/users", (req, res) => {
  const body = req.body;
  const newUser = {
    id: users.length + 1,
    name: body.name,
    email: body.email,
  };
  
  users.push(newUser);
  
  res.status(201).json({ 
    success: true, 
    data: newUser 
  });
});

// PUT /api/users/:id - Update user
app.put("/api/users/:id", (req, res) => {
  const { id } = req.params;
  const body = req.body;
  const user = users.find(u => u.id === Number(id));
  
  if (!user) {
    return res.status(404).json({ 
      success: false, 
      error: "User not found" 
    });
  }
  
  if (body.name) user.name = body.name;
  if (body.email) user.email = body.email;
  
  res.json({ success: true, data: user });
});

// DELETE /api/users/:id - Delete user
app.delete("/api/users/:id", (req, res) => {
  const { id } = req.params;
  const index = users.findIndex(u => u.id === Number(id));
  
  if (index === -1) {
    return res.status(404).json({ 
      success: false, 
      error: "User not found" 
    });
  }
  
  users.splice(index, 1);
  
  res.json({ 
    success: true, 
    message: "User deleted successfully" 
    });
});

// Start the server
await app.listen();

Step 3: Create Entry Point (TypeScript only) 🖥️

For JavaScript, you already created the complete server in Step 2. For TypeScript with decorators, you need a separate entry point.

TypeScript

Create src/index.ts:

src/index.ts
import { AzuraClient, applyDecorators } from "azurajs";
import { createLoggingMiddleware } from "azurajs/middleware";
import { UserController } from "./controllers/UserController";

// Create the application instance
const app = new AzuraClient();

// Add logging middleware
const logger = createLoggingMiddleware(app.getConfig());
app.use(logger);

// Register controllers
applyDecorators(app, [UserController]);

// Start the server
await app.listen();

JavaScript

Your server is already complete in src/index.js from Step 2!

Step 4: Run Your Server 🏃

TypeScript

Run your application:

# With Bun (recommended)
bun run src/index.ts

# With Node.js and tsx
npx tsx src/index.ts

# Or add to package.json scripts
{
  "scripts": {
    "dev": "tsx watch src/index.ts",
    "start": "tsx src/index.ts"
  }
}

JavaScript

Run your application:

# With Bun (recommended)
bun run src/index.js

# With Node.js
node src/index.js

# Or add to package.json scripts
{
  "scripts": {
    "dev": "node --watch src/index.js",
    "start": "node src/index.js"
  }
}

You should see:

[master] listening on http://localhost:3000

Step 5: Test Your API 🧪

Test your endpoints using curl, Postman, or any HTTP client:

Get all users

curl http://localhost:3000/api/users

Response:

{
  "success": true,
  "data": [
    { "id": 1, "name": "Alice", "email": "[email protected]" },
    { "id": 2, "name": "Bob", "email": "[email protected]" }
  ]
}

Get user by ID

curl http://localhost:3000/api/users/1

Create a new user

curl -X POST http://localhost:3000/api/users \
  -H "Content-Type: application/json" \
  -d '{"name":"Charlie","email":"[email protected]"}'

Update a user

curl -X PUT http://localhost:3000/api/users/1 \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice Smith"}'

Delete a user

curl -X DELETE http://localhost:3000/api/users/2

🎉 Congratulations!

You've just built a complete RESTful API with AzuraJS! Here's what you learned:

TypeScript

  • ✅ How to configure AzuraJS
  • ✅ How to create controllers with decorators
  • ✅ How to use parameter decorators (@Body, @Param, @Res)
  • ✅ How to register controllers with applyDecorators
  • ✅ How to start the server

JavaScript

  • ✅ How to configure AzuraJS
  • ✅ How to define routes with functional API
  • ✅ How to handle request parameters and body
  • ✅ How to use middleware
  • ✅ How to start the server

What's Next? 📚

Now that you have a basic API running, explore more features:

Pro Tips 💡

Hot Reload: Use tsx watch or bun --watch for automatic server restart during development

Multiple Controllers: You can register multiple controllers at once:

applyDecorators(app, [UserController, PostController, AuthController]);

Environment Variables: Use process.env in your config file for production settings

On this page