AI-powered API generator for NestJS - Generate REST APIs from natural language with MongoDB integration
npm install ai-api-generator-nestjsAI-powered API generator for NestJS - Generate REST APIs from natural language with MongoDB integration.
``bash`
npm install ai-api-generator-nestjs
`typescript
// app.module.ts
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { AiApiGeneratorModule } from 'ai-api-generator-nestjs';
@Module({
imports: [
MongooseModule.forRoot('mongodb://localhost:27017/mydb'),
AiApiGeneratorModule.forRoot({
ai: {
provider: 'openai',
apiKey: process.env.OPENAI_API_KEY,
model: 'gpt-4',
},
basePath: '/ai-api-generator', // UI served here
apiPrefix: 'ai-api-generator', // API endpoints prefix
}),
],
})
export class AppModule {}
`
`typescript
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { AiApiGeneratorUI } from 'ai-api-generator-nestjs';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// Setup the AI API Generator UI (similar to Swagger)
AiApiGeneratorUI.setup('/ai-api-generator', app);
await app.listen(3000);
}
bootstrap();
`
Open your browser and navigate to http://localhost:3000/ai-api-generator
`typescript`
interface AiApiGeneratorModuleOptions {
ai: {
provider: 'openai' | 'anthropic';
apiKey: string;
model?: string; // Default: 'gpt-4'
baseUrl?: string; // Custom API endpoint
temperature?: number; // Default: 0.7
maxTokens?: number; // Default: 2000
};
basePath?: string; // Default: '/ai-api-generator'
apiPrefix?: string; // Default: 'ai-api-generator'
connectionName?: string; // Mongoose connection name (for multi-db setups)
metadataCollection?: string; // Default: 'ai_api_metadata'
cache?: {
enabled?: boolean; // Default: true
type?: 'memory' | 'redis';
host?: string; // Redis host
port?: number; // Redis port
ttl?: number; // Cache TTL in seconds
};
ui?: {
enabled?: boolean; // Default: true
title?: string; // Custom UI title
};
}
`typescript`
AiApiGeneratorModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
ai: {
provider: 'openai',
apiKey: configService.get('OPENAI_API_KEY'),
model: configService.get('OPENAI_MODEL'),
},
}),
inject: [ConfigService],
})
`typescript`
AiApiGeneratorUI.setup('/ai-api-generator', app, {
apiPrefix: '/api/v1/ai-generator', // Custom API prefix
title: 'My API Generator', // Custom title
customCss: 'body { font-family: Arial; }',
customJs: 'console.log("Custom JS loaded");',
});
Once configured, the following endpoints are available:
| Endpoint | Method | Description |
|----------|--------|-------------|
| /{prefix}/generate | POST | Generate API from natural language |/{prefix}
| | GET | List all APIs |/{prefix}
| | POST | Create a new API |/{prefix}/:id
| | GET | Get API by ID |/{prefix}/:id
| | PUT | Update API |/{prefix}/:id
| | DELETE | Delete API |/{prefix}/:id/execute
| | POST | Execute API |/{prefix}/:id/clone
| | POST | Clone API |/{prefix}/database-schema
| | GET | Get database schema |/{prefix}/database-schema/collections` | GET | List collections |
|
- Natural Language API Generation: Describe your API in plain English
- MongoDB Integration: Automatically generates MongoDB queries
- Schema Introspection: Reads your database schema for accurate query generation
- OpenAPI Export: Generate OpenAPI 3.0 specifications
- API Execution: Test APIs directly from the UI
- Redis Caching: Optional schema caching with Redis
- Swagger-like UI: Pre-built React interface for managing APIs
- Node.js >= 18.0.0
- NestJS >= 10.0.0
- MongoDB >= 7.0
- Redis (optional, for caching)
MIT