Getting Started with VayuAPI
Welcome to VayuAPI!
This guide will help you get started with the VayuAPI framework in just a few minutes.
Installation
Basic Installation
pip install vayuapi
With All Features
pip install vayuapi[all]
Optional Feature Sets
# Django ORM support
pip install vayuapi[django]
# Async ORM (Tortoise)
pip install vayuapi[orm]
# AI/ML features
pip install vayuapi[ai,rag]
# Security features
pip install vayuapi[security]
# Task scheduling
pip install vayuapi[scheduler]
# Vector databases
pip install vayuapi[vector]
1. Hello World
Create a file main.py:
from vayuapi import VayuAPI
app = VayuAPI()
@app.get("/")
async def home():
return {"message": "Hello, VayuAPI!"}
if __name__ == "__main__":
app.run()
Run the application:
python main.py
Visit http://localhost:8000 in your browser.
2. With Pydantic Models
from vayuapi import VayuAPI
from pydantic import BaseModel
app = VayuAPI()
class User(BaseModel):
name: str
email: str
age: int
@app.post("/users")
async def create_user(user: User):
return {"message": "User created", "user": user}
@app.get("/users/{user_id}")
async def get_user(user_id: int):
return {"user_id": user_id, "name": "John Doe"}
if __name__ == "__main__":
app.run()
3. API Documentation
VayuAPI automatically generates interactive API documentation:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc - OpenAPI Schema:
http://localhost:8000/openapi.json
4. Database Integration
from vayuapi import VayuAPI
from tortoise import fields
from tortoise.models import Model
app = VayuAPI(admin_enabled=True)
class User(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=100)
email = fields.CharField(max_length=100, unique=True)
created_at = fields.DatetimeField(auto_now_add=True)
@app.get("/users")
async def list_users():
users = await User.all()
return {"users": users}
@app.post("/users")
async def create_user(name: str, email: str):
user = await User.create(name=name, email=email)
return {"user": user}
if __name__ == "__main__":
app.run()
5. WebSocket Support
from vayuapi import VayuAPI
app = VayuAPI()
@app.websocket("/ws")
async def websocket_endpoint(websocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Echo: {data}")
if __name__ == "__main__":
app.run()
6. Middleware & Authentication
from vayuapi import VayuAPI
from vayuapi.middleware import AuthMiddleware
app = VayuAPI(
middleware=[
AuthMiddleware
],
cors_enabled=True,
allowed_origins=["*"]
)
@app.get("/protected")
async def protected_route(request):
user = request.user
return {"message": f"Hello, {user.username}!"}
if __name__ == "__main__":
app.run()
7. Native Concurrency - Thread Pool
Handle blocking I/O without blocking the event loop:
from vayuapi import VayuAPI, run_in_thread
app = VayuAPI()
@app.get("/blocking")
async def blocking_operation():
# Runs in thread pool (doesn't block other requests)
result = await run_in_thread(slow_blocking_function)
return {"result": result}
# Result: Handles 100+ concurrent requests instead of 0.2 RPS
8. Native Concurrency - Process Pool
True parallelism for CPU-intensive tasks (bypasses Python GIL):
from vayuapi import VayuAPI, run_in_process
app = VayuAPI()
@app.post("/compute")
async def compute_intensive(data: list):
# Runs in separate process (full CPU core)
result = await run_in_process(expensive_computation, data)
return {"result": result}
9. Resource Control with Semaphores
Prevent resource exhaustion by limiting concurrent access:
from vayuapi import VayuAPI, Semaphore
app = VayuAPI()
# Limit to 10 concurrent database connections
db_semaphore = Semaphore(10)
@app.get("/query")
async def query_database():
async with db_semaphore:
# Max 10 concurrent DB connections
return await database.query("SELECT * FROM users")
10. JWT Authentication
Implement secure token-based authentication:
from vayuapi import VayuAPI, Depends
from vayuapi.security import JWTHandler, JWTBearer
app = VayuAPI()
jwt_handler = JWTHandler(
secret_key="your-secret-key",
algorithm="HS256",
access_token_expire_minutes=30
)
jwt_auth = JWTBearer(jwt_handler)
@app.post("/login")
async def login(username: str, password: str):
if verify_credentials(username, password):
token = jwt_handler.create_access_token(
data={"sub": username, "user_id": 123}
)
return {"access_token": token, "token_type": "bearer"}
@app.get("/protected")
async def protected_route(payload = Depends(jwt_auth)):
return {
"message": "Access granted",
"user": payload.get("sub"),
"user_id": payload.get("user_id")
}
11. Async Caching
Speed up frequently accessed data with LRU caching:
from vayuapi import VayuAPI, AsyncLRUCache
app = VayuAPI()
cache = AsyncLRUCache(max_size=1000, ttl=300)
@cache.cached
async def expensive_query(user_id: int):
return await slow_database_operation(user_id)
@app.get("/user/{user_id}")
async def get_user(user_id: int):
# Cached for 5 minutes, 500x faster
return await expensive_query(user_id)
Next Steps
- Check out the API Reference for detailed documentation
- Explore all available features
- Learn about deployment options
- Read about security best practices
Need Help?
If you have questions or run into issues:
- Check the Installation Guide
- Review the API Reference
- Join our community and ask for help