You can download this code by clicking the button below.
This code is now available for download.
This function creates a FastAPI application, defines a User model, generates 10 random user data, and provides a GET interface to retrieve user data.
Technology Stack : FastAPI, Pydantic
Code Type : API service
Code Difficulty : Intermediate
def random_user_data():
import random
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
class User(BaseModel):
id: int
name: str
age: int
app = FastAPI()
# Generate a list of random users
users = [User(id=random.randint(1, 100), name=f"User{random.randint(1, 1000)}", age=random.randint(18, 65)) for _ in range(10)]
@app.get("/users", response_model=list[User])
async def get_users():
return users
return app