FastAPI
FastAPI is a modern, fast (high-performance) Web framework for building APIs, using Python and based on standard Python type hints. It has the following features:
- Fast: Extremely high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic)
- Highly efficient coding: Increase feature development speed by approximately 200% to 300%
- Fewer bugs: Reduce approximately 40% of human-induced (developer) errors
- Intelligent: Great editor support with autocompletion everywhere, reducing debugging time
- Simple: Designed to be easy to use and learn, with less time reading documentation
- Automatic documentation: Automatically generates interactive API documentation
This guide describes how to deploy a FastAPI application on CloudBase HTTP cloud functions.
Prerequisites
Before you begin, make sure you have:
- Have Python 3.10 or later versions installed
- Have a Tencent Cloud account with CloudBase service activated
- Have basic knowledge of Python and FastAPI development
Step 1: Create a FastAPI Project
💡 Tip: If you already have a FastAPI project, you can skip this step.
Create a Project Directory
mkdir fastapi-cloudbase
cd fastapi-cloudbase
Create Application Files
Create the app.py file, which is the application entry file:
from fastapi import FastAPI, HTTPException, Query
from pydantic import BaseModel
from typing import List, Optional
import uvicorn
app = FastAPI(
title="FastAPI CloudBase Demo",
description="A FastAPI application running on CloudBase HTTP Functions",
version="1.0.0"
)
# Data Models
class User(BaseModel):
id: int
name: str
email: str
age: Optional[int] = None
class UserCreate(BaseModel):
name: str
email: str
age: Optional[int] = None
# Mock Database
users_db = [
User(id=1, name="Zhang San", email="zhangsan@example.com", age=25),
User(id=2, name="Li Si", email="lisi@example.com", age=30),
User(id=3, name="Wang Wu", email="wangwu@example.com", age=28)
]
@app.get("/")
async def root():
"""Root path handler function"""
return {
"message": "Hello from FastAPI on CloudBase!",
"framework": "FastAPI",
"docs": "/docs",
"redoc": "/redoc"
}
@app.get("/health")
async def health_check():
"""Health Check API"""
return {
"status": "healthy",
"framework": "FastAPI",
"version": "1.0.0"
}
@app.get("/api/users", response_model=List[User])
async def get_users(
page: int = Query(1, ge=1, description="page number"),
limit: int = Query(10, ge=1, le=100, description="items per page")
):
"""Get user list (with pagination support)"""
start_index = (page - 1) * limit
end_index = start_index + limit
paginated_users = users_db[start_index:end_index]
return paginated_users
@app.get("/api/users/{user_id}", response_model=User)
async def get_user(user_id: int):
"""Obtain user by ID"""
user = next((user for user in users_db if user.id == user_id), None)
if not user:
raise HTTPException(status_code=404, detail="User not found")
return user
@app.post("/api/users", response_model=User, status_code=201)
async def create_user(user: UserCreate):
"""Create New User"""
# Check whether the email already exists
if any(u.email == user.email for u in users_db):
raise HTTPException(status_code=400, detail="Email already registered")
# Generate new ID
new_id = max(u.id for u in users_db) + 1 if users_db else 1
# Create new user
new_user = User(id=new_id, **user.dict())
users_db.append(new_user)
return new_user
@app.put("/api/users/{user_id}", response_model=User)
async def update_user(user_id: int, user_update: UserCreate):
"""Update User Information"""
user_index = next((i for i, u in enumerate(users_db) if u.id == user_id), None)
if user_index is None:
raise HTTPException(status_code=404, detail="User not found")
# Check whether the email is already in use by another user
if any(u.email == user_update.email and u.id != user_id for u in users_db):
raise HTTPException(status_code=400, detail="Email already registered")
# Update user
updated_user = User(id=user_id, **user_update.dict())
users_db[user_index] = updated_user
return updated_user
@app.delete("/api/users/{user_id}")
async def delete_user(user_id: int):
Delete User
user_index = next((i for i, u in enumerate(users_db) if u.id == user_id), None)
if user_index is None:
raise HTTPException(status_code=404, detail="User not found")
deleted_user = users_db.pop(user_index)
return {"message": f"User {deleted_user.name} deleted successfully"}
# Custom Exception Handling
@app.exception_handler(404)
async def not_found_handler(request, exc):
return {"error": "Not Found", "message": "The requested resource was not found"}
@app.exception_handler(500)
async def internal_error_handler(request, exc):
return {"error": "Internal Server Error", "message": "Something went wrong"}
if __name__ == "__main__":
# CloudBase HTTP cloud function requires listening on port 9000
uvicorn.run(app, host="0.0.0.0", port=9000)
Create dependency file
Create the requirements.txt file:
fastapi==0.104.1
uvicorn[standard]==0.24.0
pydantic==2.5.0
💡 Note:
fastapi: FastAPI frameworkuvicorn: ASGI server for running FastAPI applicationspydantic: Data validation and serialization library
Step 2: Test Locally
⚠️ Important Notice: CloudBase HTTP cloud function requires the application to listen on port 9000.
Set Up a Virtual Environment
# Create a Virtual Environment
python -m venv venv
# Activate the Virtual Environment
# Linux/macOS
source venv/bin/activate
# Windows
# venv\Scripts\activate
Install Dependencies
pip install -r requirements.txt
Start the Application
# Method 1: Direct Run
python app.py
# Method 2: Using the uvicorn command
uvicorn app:app --host 0.0.0.0 --port 9000 --reload
Test the API
After the application starts up, you can test it in the following ways:
Access Documentation:
- Swagger UI:
http://localhost:9000/docs - ReDoc:
http://localhost:9000/redoc
Test API interfaces:
# Test Root Path
curl http://localhost:9000/
# Test Health Check
curl http://localhost:9000/health
# Test User List
curl http://localhost:9000/api/users
# Test Pagination
curl "http://localhost:9000/api/users?page=1&limit=2"
# Test Obtain Single User
curl http://localhost:9000/api/users/1
# Test User Creation
curl -X POST http://localhost:9000/api/users \
-H "Content-Type: application/json" \
-d '{"name":"New User","email":"newuser@example.com","age":25}'
# Test User Update
curl -X PUT http://localhost:9000/api/users/1 \
-H "Content-Type: application/json" \
-d '{"name":"Updated User","email":"updated@example.com","age":26}'
Step 3: Create a Startup Script
Create the scf_bootstrap file (without extension):
#!/bin/bash
export PYTHONPATH="./venv/lib/python3.10/site-packages:$PYTHONPATH"
/var/lang/python310/bin/python3.10 -m uvicorn app:app --host 0.0.0.0 --port 9000
Grant execute permission to the startup script:
chmod +x scf_bootstrap
💡 Note:
scf_bootstrapis the startup script for CloudBase cloud functions- Start the FastAPI application using uvicorn
- Ensure the application listens on port 9000
Step 4: Prepare Deployment Files
Ensure your project directory structure is as follows:
fastapi-cloudbase/
├── venv/ # Virtual Environment (Optional)
├── app.py # Main application file
├── requirements.txt # Dependency List
└── scf_bootstrap # Startup script
Step 5: Deploy to CloudBase HTTP cloud function
Deploy via the Console
- Log in to the CloudBase Console
- Select your environment and go to the [Cloud Function] page
- Click [New Cloud Function]
- Select [HTTP cloud function]
- Fill in the function name (for example:
fastapi-app) - Select runtime: Python 3.10
- Select submission method: Local folder upload
- Select the project root directory for function code upload
- Automatic Dependency Installation: Enable this option
- Click the "Create" button and wait for the deployment to complete.
Deploy via CLI (Coming Soon)
Package and deploy
If you need to package manually:
# Create Deployment Package (excluding virtual environment)
zip -r fastapi-app.zip . -x "venv/*" ".git/*" "__pycache__/*" "*.pyc"
Step 6: Access Your Application
After successful deployment, you can refer to Accessing Cloud Functions via HTTP to configure custom domain access for
You can access the following interfaces:
- Root Path:
/- Welcome Message - API Documentation:
/docs- Swagger UI documentation - Alternative Docs:
/redoc- ReDoc documentation - Health Check:
/health- Application Status - User API:
/api/users- RESTful User API
Frequently Asked Questions
Q: Why must I use port 9000?
A: The CloudBase HTTP cloud function requires the application to listen on port 9000, which is the standard configuration of the platform.
Q: Can FastAPI's auto-generated documentation function properly within cloud functions?
A: Yes, FastAPI's Swagger UI and ReDoc documentation can be normally accessed and used within cloud functions.
Q: How do I handle static files?
A: FastAPI can handle static files using the StaticFiles middleware or by utilizing CDN services.
Q: How do I view application logs?
A: On the CloudBase console's cloud function page, click the function name to go to the details page and view the running logs.
Q: Which Python versions are supported?
A: CloudBase supports Python versions such as 3.6, 3.7, 3.9, 3.10, etc. It is recommended to use Python 3.10.
Best Practices
1. Environment Variable Management
import os
from fastapi import FastAPI
# Use Environment Variables
DEBUG = os.getenv("DEBUG", "False").lower() == "true"
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key")
app = FastAPI(debug=DEBUG)
2. Database Integration
# Use SQLAlchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./test.db")
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
3. Middleware Configuration
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
# CORS Configuration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Gzip Compression
app.add_middleware(GZipMiddleware, minimum_size=1000)
4. Log Configuration
import logging
# Configure Logs
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
5. Dependency Injection
from fastapi import Depends
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.get("/api/users")
async def get_users(db: Session = Depends(get_db)):
return db.query(User).all()
Advanced Features
1. Authentication
pip install python-jose[cryptography] passlib[bcrypt] python-multipart
2. Background Tasks
from fastapi import BackgroundTasks
@app.post("/send-email/")
async def send_email(background_tasks: BackgroundTasks):
background_tasks.add_task(send_email_task, "user@example.com")
return {"message": "Email sent in background"}
3. WebSocket Support
from fastapi import WebSocket
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message: {data}")
Next Steps
- Learn more about HTTP cloud function configuration options
- Learn how to connect to the CloudBase database