A proof-of-concept for write once, deploy everywhere: service functions automatically become both API endpoints AND CLI commands with zero boilerplate.
@register_endpoint decorator creates both API route and CLI command# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Edit .env and set your API_KEY
Start the FastAPI server:
uvicorn ncatoolkit.main:app --reload
Make API calls with authentication:
# Health check
curl http://localhost:8000/
# List all endpoints
curl http://localhost:8000/endpoints
# Transcribe media (no webhook)
curl -X POST http://localhost:8000/v2/media/transcribe \
-H "X-API-Key: test-api-key-12345" \
-H "Content-Type: application/json" \
-d '{"media_file": "audio.mp3", "language": "en"}'
# Transcribe media with webhook notification
curl -X POST http://localhost:8000/v2/media/transcribe \
-H "X-API-Key: test-api-key-12345" \
-H "Content-Type: application/json" \
-d '{
"media_file": "audio.mp3",
"language": "en",
"webhook_url": "https://webhook.site/your-unique-url"
}'
Run commands directly (no API key required):
# List available commands
python -m ncatoolkit.cli list-commands
# Transcribe using JSON payload (primary method)
python -m ncatoolkit.cli transcribe --json '{
"media_file": "audio.mp3",
"language": "en",
"webhook_url": "https://webhook.site/your-unique-url"
}'
# Transcribe using individual flags (optional method)
python -m ncatoolkit.cli transcribe \
--media-file audio.mp3 \
--language en \
--webhook-url https://webhook.site/your-unique-url
Simply create a service function with the decorator:
# ncatoolkit/services/your_service.py
from ncatoolkit.core.registry import register_endpoint
@register_endpoint(route="/v2/your/endpoint", method="POST")
async def your_function(param1: str, param2: str = None):
# Your business logic
return {"result": "success"}
This automatically creates:
POST /v2/your/endpoint (with auth)python -m ncatoolkit.cli endpoint --json '{"param1":"value"}'webhook_url provided)/ncatoolkit
/core
config.py # Environment settings (API_KEY)
auth.py # API key validation
webhook.py # Webhook notifications
registry.py # Auto-registration decorator
/services
transcription.py # Example service
/schemas
transcription.py # Pydantic models
main.py # FastAPI app
cli.py # Typer CLI app
Webhooks send standardized JSON:
Success:
{
"status": "success",
"result": {
"transcription": "...",
"language": "en",
"duration": 42.5
}
}
Error:
{
"status": "error",
"error": "Error message here"
}
# Test webhook notifications at:
https://webhook.site
# Example test flow:
1. Get a unique webhook URL from webhook.site
2. Call API/CLI with webhook_url parameter
3. Check webhook.site for the notification
X-API-Key header matching API_KEY in .envThe registry pattern enables adding new endpoints with minimal code:
/ncatoolkit/services/@register_endpoint decorator with route and methodmain.py (auto-registers API route)cli.py that uses the registryCurrent Implementation:
ENDPOINT_REGISTRYWhy CLI isn't fully auto-registered: Typer's dynamic command registration has limitations with current Python/Typer versions. The current approach provides explicit CLI commands while still leveraging the shared registry for business logic and validation.
Benefits achieved:
@register_endpoint)docker pull stephengpope/ncatk2.0:latest