Skip to content

🧞 AI Mock API Genie is an intelligent API mocking tool that revolutionizes how developers create realistic test environments. Unlike traditional mock tools that generate random data, it captures real API responses, uses AI (Anthropic Claude/OpenAI) to understand patterns and generates contextually appropriate mock data for complex domains

License

Notifications You must be signed in to change notification settings

simplyrk/ai-mock-api-genie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧞 AI Mock API Genie

A powerful, scalable AI-powered API mocking tool that captures real API requests and generates realistic mock responses using AI. Each endpoint gets a unique ID for easy management and routing.

Python FastAPI License

✨ Features

  • 🎯 Capture Real APIs: Record actual API responses for learning
  • πŸ”’ Unique Endpoint IDs: Auto-generated 7-digit CAPS alphanumeric IDs for each endpoint
  • πŸ€– AI Sample Generation: Uses Anthropic Claude or OpenAI GPT to generate realistic samples per endpoint
  • πŸš€ Scalable Routes: Serve multiple endpoints simultaneously with /api/<ID>/<path> format
  • πŸ“‹ Endpoint Management: List and manage captured endpoints with their unique IDs
  • βš™οΈ Configurable: X (context requests), Y (sample count), and prompts are all configurable
  • 🌐 Production Ready: Built with FastAPI, comprehensive error handling, and CORS support

πŸš€ Quick Start

Installation

# Clone the repository
git clone https://github.com/simplyrk/ai-mock-api-genie.git
cd ai-mock-api-genie

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Basic Scalable Workflow

# 1. Initialize the database
mock-api-genie init

# 2. Capture endpoints (each gets a unique ID)
mock-api-genie capture https://jsonplaceholder.typicode.com/users/1 --count 3
mock-api-genie capture https://jsonplaceholder.typicode.com/posts --count 5

# 3. List captured endpoints with their IDs
mock-api-genie list

# 4. Generate AI samples for specific endpoints
mock-api-genie generate --id ABC1234
mock-api-genie generate --id XYZ5678

# 5. Start the scalable mock server
mock-api-genie serve --port 8000

# 6. Access mock endpoints using their unique IDs
curl http://localhost:8000/api/ABC1234/users/1
curl http://localhost:8000/api/XYZ5678/posts

πŸ“‹ Prerequisites

  • Python 3.9+
  • SQLite3 (usually comes pre-installed with Python)
  • Anthropic API key or OpenAI API key

πŸ”‘ Configuration

Create a .env file in the project root:

# ============================================
# AI PROVIDER CONFIGURATION
# ============================================
# Get your Anthropic API key from: https://console.anthropic.com/
ANTHROPIC_API_KEY=your_anthropic_api_key_here

# Get your OpenAI API key from: https://platform.openai.com/api-keys
OPENAI_API_KEY=your_openai_api_key_here

# AI Provider (anthropic or openai)
AI_PROVIDER=anthropic

# AI Models
ANTHROPIC_MODEL=claude-3-5-sonnet-20241022
OPENAI_MODEL=gpt-4-turbo-preview

# ============================================
# DATABASE CONFIGURATION
# ============================================
DATABASE_URL=sqlite:///mock_api_genie.db

# ============================================
# MOCK SERVER CONFIGURATION
# ============================================
MOCK_SERVER_HOST=0.0.0.0
MOCK_SERVER_PORT=8000

# Enable CORS for mock server
ENABLE_CORS=true

# Mock server response delay (milliseconds)
MIN_RESPONSE_DELAY_MS=50
MAX_RESPONSE_DELAY_MS=500

# Logging
LOG_LEVEL=INFO

# ============================================
# AI SAMPLE GENERATION SETTINGS
# ============================================
# X - Number of recent captured requests to analyze as context
AI_CONTEXT_REQUESTS=10

# Y - Number of sample responses to generate each time
AI_SAMPLE_COUNT=5

# Maximum number of captures to store per endpoint
MAX_CAPTURES_PER_ENDPOINT=100

# ============================================
# AI PROMPT TEMPLATES (SIMPLIFIED)
# ============================================
# System prompt for AI sample generation
AI_SAMPLE_SYSTEM_PROMPT=You are an AI assistant that generates realistic sample API responses based on captured request patterns. Generate diverse, realistic responses that follow the patterns from the provided examples.

# User prompt template for sample generation (Variables: {requests}, {num_samples})
AI_SAMPLE_PROMPT=Based on these captured API requests and responses: {requests} Generate {num_samples} new realistic sample responses that follow similar patterns. Make the data varied and realistic while maintaining the same structure and data types. Return a JSON object with a samples key containing an array of sample response objects.

πŸ—οΈ Architecture

The scalable version supports unlimited endpoints with unique routing:

  1. Capture - Auto-assigns unique 7-digit CAPS IDs to endpoints
  2. Generate - Creates AI samples for specific endpoint IDs
  3. Serve - Routes requests to /api/<ID>/<path> format
ai-mock-api-genie/
β”œβ”€β”€ mock_api_genie/
β”‚   β”œβ”€β”€ cli.py              # Simplified CLI interface
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ analyzer.py     # Endpoint-specific AI generation
β”‚   β”‚   β”œβ”€β”€ generator.py    # Sample serving
β”‚   β”‚   β”œβ”€β”€ capture.py      # Auto-generates unique IDs
β”‚   β”‚   └── config.py       # Configuration management
β”‚   β”œβ”€β”€ server/
β”‚   β”‚   └── app.py          # Scalable FastAPI server
β”‚   └── models/
β”‚       └── database.py     # Database models with unique_id fields
β”œβ”€β”€ requirements.txt        # Clean dependencies
β”œβ”€β”€ setup.py               # Production-ready setup
β”œβ”€β”€ .env.example           # Configuration template
└── README.md              # This file

🌐 CLI Commands

Command Description Example
init Initialize database mock-api-genie init
capture Capture endpoint (auto-assigns ID) mock-api-genie capture https://api.com/users --count 5
list List captured endpoints with IDs mock-api-genie list
generate Generate samples for specific endpoint mock-api-genie generate --id ABC1234
serve Start scalable mock server mock-api-genie serve --port 8000

πŸ“Š Endpoint Management

List Captured Endpoints

mock-api-genie list

Output:

πŸ“‹ Captured Endpoints:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   ID    β”‚ Method β”‚                   URL                   β”‚ Capturesβ”‚ Generated β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ ABC1234 β”‚ GET    β”‚ https://jsonplaceholder.typicode.com... β”‚    3    β”‚     5     β”‚
β”‚ XYZ5678 β”‚ GET    β”‚ https://api.github.com/users/octocat    β”‚    2    β”‚     0     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Generate Samples for Specific Endpoint

mock-api-genie generate --id ABC1234

Output:

πŸ€– Generating AI samples for endpoint ABC1234...
βœ… Generated 5 new samples for endpoint ABC1234
πŸ“ Endpoint: GET https://jsonplaceholder.typicode.com/users/1

🌐 API Endpoints & Routes

Server Management

Endpoint Method Description
/ GET Server info and active endpoints
/health GET Health check endpoint
/admin/generate/{endpoint_id} POST Generate samples via API

Mock Endpoints (Dynamic)

Route Pattern Description Example
/api/{endpoint_id}/{path} Serves mock data for endpoint ID /api/ABC1234/users/1

πŸ”„ Complete Example Workflow

# 1. Set up environment
export ANTHROPIC_API_KEY="your_key_here"

# 2. Initialize database
mock-api-genie init

# 3. Capture multiple different endpoints
mock-api-genie capture https://jsonplaceholder.typicode.com/users/1 --count 3
mock-api-genie capture https://jsonplaceholder.typicode.com/posts --count 5
mock-api-genie capture https://api.github.com/users/octocat --count 2

# 4. List all captured endpoints with their unique IDs
mock-api-genie list

# Example output:
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚   ID    β”‚ Method β”‚                   URL                   β”‚ Capturesβ”‚ Generated β”‚
# β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
# β”‚ A7B9X2M β”‚ GET    β”‚ https://jsonplaceholder.typicode.com... β”‚    3    β”‚     0     β”‚
# β”‚ K3N8Y4P β”‚ GET    β”‚ https://jsonplaceholder.typicode.com... β”‚    5    β”‚     0     β”‚
# β”‚ R6T1Z9Q β”‚ GET    β”‚ https://api.github.com/users/octocat    β”‚    2    β”‚     0     β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

# 5. Generate AI samples for each endpoint
mock-api-genie generate --id A7B9X2M
mock-api-genie generate --id K3N8Y4P  
mock-api-genie generate --id R6T1Z9Q

# 6. Start the scalable server
mock-api-genie serve --port 8000

# 7. Access mock endpoints using their unique IDs
curl http://localhost:8000/api/A7B9X2M/users/1     # Returns realistic user data
curl http://localhost:8000/api/K3N8Y4P/posts       # Returns realistic posts data
curl http://localhost:8000/api/R6T1Z9Q/users/octocat # Returns realistic GitHub user data

# 8. Check server status and available endpoints
curl http://localhost:8000/

🎯 Route Structure Examples

For captured endpoint https://jsonplaceholder.typicode.com/users/1 with ID A7B9X2M:

Original Request Mock Route Description
GET https://jsonplaceholder.typicode.com/users/1 GET /api/A7B9X2M/users/1 Exact path match
GET https://jsonplaceholder.typicode.com/users/123 GET /api/A7B9X2M/users/123 Pattern match with different ID

βš™οΈ Configuration Options

Environment Variables

Variable Description Default
AI_CONTEXT_REQUESTS Number of recent requests to analyze (X) 10
AI_SAMPLE_COUNT Number of samples to generate (Y) 5
AI_SAMPLE_SYSTEM_PROMPT System prompt for AI Configurable
AI_SAMPLE_PROMPT User prompt template for AI Configurable
ANTHROPIC_API_KEY Anthropic API key Required
OPENAI_API_KEY OpenAI API key Alternative to Anthropic
AI_PROVIDER AI provider choice anthropic
DATABASE_URL Database connection sqlite:///mock_api_genie.db
MOCK_SERVER_PORT Server port 8000

πŸš€ Running the Server

Option 1: CLI

mock-api-genie serve --port 8000

Option 2: Direct Python

python -c "
import uvicorn
from mock_api_genie.server.app import create_app
app = create_app()
uvicorn.run(app, host='0.0.0.0', port=8000)
"

Option 3: Using uvicorn directly

uvicorn mock_api_genie.server.app:create_app --factory --host 0.0.0.0 --port 8000

🌟 Key Benefits

  1. Unlimited Endpoints: Mock as many APIs as needed simultaneously
  2. Easy Management: Each endpoint has a memorable 7-digit ID
  3. AI-Powered: Generates realistic, contextually appropriate data
  4. Isolated Samples: AI generates samples specific to each endpoint
  5. Clear Routing: /api/<ID>/<path> format prevents conflicts
  6. Production Ready: FastAPI backend with comprehensive error handling
  7. Flexible Deployment: Serve multiple mock APIs from one server

πŸ”§ Development

Installing for Development

git clone https://github.com/simplyrk/ai-mock-api-genie.git
cd ai-mock-api-genie
python -m venv venv
source venv/bin/activate
pip install -e .

Running Tests

pytest tests/

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ’‘ Acknowledgments

Built with:

πŸ“§ Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.

πŸ‘¨β€πŸ’» Author

simplyrk - GitHub

About

🧞 AI Mock API Genie is an intelligent API mocking tool that revolutionizes how developers create realistic test environments. Unlike traditional mock tools that generate random data, it captures real API responses, uses AI (Anthropic Claude/OpenAI) to understand patterns and generates contextually appropriate mock data for complex domains

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages