AI Agent Payment Orchestration Platform - Backend API
This is a modular monolith designed for easy extraction into microservices.
Each service is self-contained with:
- Own business logic
- Own data access
- Own types/interfaces
- Clear public API
- No direct imports between services (use dependency injection)
/services
/agents → Agent management
/rules → Rules engine
/transactions → Transaction orchestration
/blockchain → Base L2 + x402 integration
/ledger → Financial ledger (double-entry)
/webhooks → Webhook delivery
/approvals → Approval workflows
/auth → Authentication & authorization
- Node.js 20+
- Docker & Docker Compose
- PostgreSQL 16+ (via Docker)
- Redis 7+ (via Docker)
# Install dependencies
npm install
# Start infrastructure (Postgres + Redis)
npm run docker:up
# Copy environment variables
cp .env.example .env
# Generate Prisma client
npm run db:generate
# Run database migrations
npm run db:migrate
# Seed database (optional)
npm run db:seed
# Start development server
npm run devServer will start at: http://localhost:3000
backend/
├── src/
│ ├── index.ts # Application entry point
│ ├── server.ts # Express server setup
│ │
│ ├── api/ # HTTP API Layer
│ │ ├── routes.ts # Route registration
│ │ ├── middleware/ # Express middleware
│ │ └── endpoints/ # REST endpoints
│ │ ├── agents.ts
│ │ ├── transactions.ts
│ │ └── ...
│ │
│ ├── services/ # Business Logic (Decoupled Services)
│ │ ├── agents/ # Agent Service
│ │ │ ├── agent.service.ts
│ │ │ ├── agent.types.ts
│ │ │ ├── agent.repository.ts
│ │ │ └── index.ts
│ │ │
│ │ ├── rules/ # Rules Engine Service
│ │ │ ├── rules.service.ts
│ │ │ ├── rules-engine.ts
│ │ │ ├── rule-evaluators/
│ │ │ └── index.ts
│ │ │
│ │ ├── transactions/ # Transaction Service
│ │ │ ├── transaction.service.ts
│ │ │ ├── transaction.orchestrator.ts
│ │ │ └── index.ts
│ │ │
│ │ ├── blockchain/ # Blockchain Service
│ │ │ ├── blockchain.service.ts
│ │ │ ├── x402-client.ts
│ │ │ ├── contracts/
│ │ │ └── index.ts
│ │ │
│ │ ├── ledger/ # Ledger Service
│ │ │ ├── ledger.service.ts
│ │ │ ├── double-entry.ts
│ │ │ └── index.ts
│ │ │
│ │ ├── webhooks/ # Webhook Service
│ │ │ ├── webhook.service.ts
│ │ │ ├── webhook-delivery.ts
│ │ │ └── index.ts
│ │ │
│ │ ├── approvals/ # Approval Service
│ │ │ ├── approval.service.ts
│ │ │ └── index.ts
│ │ │
│ │ └── auth/ # Auth Service
│ │ ├── auth.service.ts
│ │ ├── jwt.ts
│ │ └── index.ts
│ │
│ ├── queues/ # BullMQ Job Queues
│ │ ├── transaction.queue.ts
│ │ ├── webhook.queue.ts
│ │ ├── reconciliation.queue.ts
│ │ └── index.ts
│ │
│ ├── workers/ # Queue Workers
│ │ ├── transaction.worker.ts
│ │ ├── webhook.worker.ts
│ │ ├── reconciliation.worker.ts
│ │ └── index.ts
│ │
│ ├── database/ # Database Layer
│ │ ├── schema.prisma # Prisma schema
│ │ ├── client.ts # Prisma client singleton
│ │ ├── migrations/ # Database migrations
│ │ └── seed.ts # Seed data
│ │
│ ├── shared/ # Shared Utilities
│ │ ├── types/ # Global types
│ │ ├── errors/ # Custom error classes
│ │ ├── utils/ # Helper functions
│ │ ├── config/ # Configuration
│ │ ├── logger/ # Logging setup
│ │ └── validators/ # Zod schemas
│ │
│ └── tests/ # Test files
│ ├── unit/
│ ├── integration/
│ └── helpers/
│
├── prisma/ # Prisma files
│ └── schema.prisma
│
├── docker-compose.yml # Local development infrastructure
├── package.json
├── tsconfig.json
└── README.md
Each service has:
- Own folder
- Own types/interfaces
- Own repository layer (if needed)
- Clear public API exported via
index.ts
Services don't import each other directly:
// ❌ Bad: Direct import
import { AgentService } from '../agents/agent.service';
// ✅ Good: Inject dependency
class TransactionService {
constructor(
private agentService: AgentService,
private rulesService: RulesService
) {}
}Each service owns one domain:
- AgentService → Agent CRUD only
- RulesService → Rule evaluation only
- TransactionOrchestrator → Coordinates services
Services use repository pattern:
// service/agents/agent.repository.ts
export class AgentRepository {
async findById(id: string) {
return prisma.agent.findUnique({ where: { id } });
}
}
// service/agents/agent.service.ts
export class AgentService {
constructor(private repo: AgentRepository) {}
async getAgent(id: string) {
return this.repo.findById(id);
}
}# Development
npm run dev # Start with hot reload
npm run build # Build for production
npm run start # Run production build
# Database
npm run db:generate # Generate Prisma client
npm run db:migrate # Run migrations
npm run db:push # Push schema (dev only)
npm run db:seed # Seed database
# Testing
npm test # Run tests
npm run test:watch # Run tests in watch mode
npm run test:coverage # Generate coverage report
# Code Quality
npm run lint # Lint code
npm run lint:fix # Fix linting issues
npm run format # Format code with Prettier
# Docker
npm run docker:up # Start Postgres + Redis
npm run docker:down # Stop containersBase URL: http://localhost:3000/v1
POST /auth/login- User loginPOST /auth/register- User registration
POST /organizations- Create organizationGET /organizations/:id- Get organization
POST /agents- Create agentGET /agents- List agentsGET /agents/:id- Get agent detailsPATCH /agents/:id- Update agentDELETE /agents/:id- Deactivate agent
POST /rules- Create spending ruleGET /rules- List rulesGET /rules/:id- Get rule detailsPATCH /rules/:id- Update ruleDELETE /rules/:id- Delete rule
POST /agents/:id/spend- Initiate spendGET /transactions- List transactionsGET /transactions/:id- Get transaction details
POST /webhooks- Register webhookGET /webhooks- List webhooksDELETE /webhooks/:id- Delete webhook
# Run all tests
npm test
# Run specific test file
npm test -- agent.service.test.ts
# Watch mode
npm run test:watch
# Coverage
npm run test:coverage- Health check:
GET /health - Metrics:
GET /metrics - Queue dashboard:
GET /admin/queues(Bull Board)
- All endpoints require authentication (JWT or API Key)
- Rate limiting: 100 requests per 15 minutes
- Helmet.js for security headers
- Input validation with Zod
- SQL injection protection (Prisma)
See .env.example for all required environment variables.
Critical variables:
DATABASE_URL- PostgreSQL connectionREDIS_URL- Redis connectionJWT_SECRET- JWT signing secret (32+ chars)BASE_RPC_URL- Base L2 RPC endpointWALLET_PRIVATE_KEY- Hot wallet private key
# Install Railway CLI
npm install -g @railway/cli
# Login
railway login
# Link project
railway link
# Deploy
railway up# Build image
docker build -t auton-backend .
# Run container
docker run -p 3000:3000 --env-file .env auton-backend- Create folder:
src/services/my-service/ - Add service class:
my-service.service.ts - Add types:
my-service.types.ts - Add repository (if needed):
my-service.repository.ts - Export public API:
index.ts - Register in dependency container
- Create endpoint:
src/api/endpoints/my-endpoint.ts - Add validation schema (Zod)
- Use service via DI
- Add tests:
src/tests/integration/my-endpoint.test.ts
- Update
prisma/schema.prisma - Run
npm run db:migrate - Update seed data if needed
- Update types
- API Docs: [Coming Soon]
- Architecture: [Coming Soon]
- Contributing: [Coming Soon]
- Create feature branch
- Write tests
- Update documentation
- Submit PR
MIT License - see LICENSE file