Part 4: API Development and Integrations

Introduction

APIs are the backbone of modern software. In this part, I'll show you how I build production-ready APIs and integrate with external services using the Model Context Protocol (MCP).

We'll build a complete API for the POS system mentioned in Part 1, with MCP integration for AI-powered features.

API Design Principles

RESTful API Structure

spinner

The POS API Endpoints

// src/api/routes.ts
import { FastifyInstance } from 'fastify';

export async function registerRoutes(app: FastifyInstance) {
  // Health check (no auth)
  app.get('/health', healthController.check);

  // Authentication
  app.post('/auth/login', authController.login);
  app.post('/auth/refresh', authController.refresh);

  // Products (requires auth)
  app.register(async (authenticated) => {
    authenticated.addHook('onRequest', authMiddleware);

    // CRUD operations
    authenticated.get('/products', productController.list);
    authenticated.get('/products/:id', productController.get);
    authenticated.post('/products', productController.create);
    authenticated.put('/products/:id', productController.update);
    authenticated.delete('/products/:id', productController.delete);

    // Bulk operations
    authenticated.post('/products/bulk', productController.bulkCreate);
    authenticated.put('/products/bulk', productController.bulkUpdate);
  });

  // Orders
  app.register(async (authenticated) => {
    authenticated.addHook('onRequest', authMiddleware);

    authenticated.get('/orders', orderController.list);
    authenticated.get('/orders/:id', orderController.get);
    authenticated.post('/orders', orderController.create);
    authenticated.put('/orders/:id/status', orderController.updateStatus);
    authenticated.post('/orders/:id/cancel', orderController.cancel);
    authenticated.get('/orders/:id/receipt', orderController.getReceipt);
  });

  // Analytics (requires admin role)
  app.register(async (admin) => {
    admin.addHook('onRequest', authMiddleware);
    admin.addHook('onRequest', requireRole('admin'));

    admin.get('/analytics/sales', analyticsController.sales);
    admin.get('/analytics/products', analyticsController.topProducts);
    admin.get('/analytics/revenue', analyticsController.revenue);
  });

  // MCP Integration - AI Features
  app.register(async (mcp) => {
    mcp.addHook('onRequest', authMiddleware);

    mcp.post('/ai/analyze-sales', mcpController.analyzeSales);
    mcp.post('/ai/forecast', mcpController.forecastDemand);
    mcp.post('/ai/recommendations', mcpController.productRecommendations);
  });
}

Implementing the Product API

The Controller Layer

The Service Layer

Model Context Protocol (MCP) Integration

MCP allows AI assistants to interact with your application. Here's how I integrated it.

MCP Server Implementation

MCP Configuration

Using MCP from VS Code Copilot

Once configured, I can ask Copilot:

Me: "What are the top 5 selling products this month?"

Copilot (uses MCP tool):

Me: "Forecast demand for Cappuccino for next month"

Copilot:

API Documentation

OpenAPI/Swagger

Access docs at: http://localhost:3000/docs

API Versioning

URL-based Versioning

Rate Limiting

Key Takeaways

  1. Layer your API: Controller → Service → Data Layer

  2. Use caching: Redis significantly reduces DB load

  3. Implement MCP: Let AI interact with your app

  4. Document everything: OpenAPI/Swagger is essential

  5. Version your API: Breaking changes need new versions

  6. Rate limit: Protect your API from abuse

What's Next

In Part 5, we'll deploy this API:

  • Docker containerization

  • CI/CD pipeline setup

  • Kubernetes deployment

  • Security hardening

  • Monitoring and alerting


APIs are contracts. Make them reliable, well-documented, and versioned. See you in Part 5.

Last updated