Learn to build a Fake Data Generator API in Python using GitHub Copilot for generating mock customer records in various formats.
In this guide, you’ll learn how to build a Fake Data Generator API in Python using GitHub Copilot. By the end, you’ll have a service that produces mock customer records in JSON, CSV, or plain text—perfect for development and testing without exposing real personal data.
A new company policy prohibits using real customer data in test environments. To comply and maintain developer productivity, we’ll build an API service that generates synthetic customer records on demand.Example JSON output:
In app/main.py, set up the FastAPI app and fake data endpoint:
Copy
Ask AI
from fastapi import FastAPIfrom app.utils import generate_fake_customersfrom app.schemas import RequestModel, CustomerModelapp = FastAPI(title="Fake Data Generator API", version="1.0.0")@app.post("/api/v1/getfakedata", response_model=list[CustomerModel])def get_fake_data(request: RequestModel): """ Generate synthetic customer records. - **count**: Number of records to generate. - **format**: Output format (json, csv, text). """ return generate_fake_customers(request.count)
In app/schemas.py, define Pydantic models:
Copy
Ask AI
from pydantic import BaseModel, Fieldclass RequestModel(BaseModel): count: int = Field(..., gt=0, description="Number of records to generate")class CustomerModel(BaseModel): first_name: str last_name: str email_address: str age: int city: str occupation: str
In app/utils.py, use Faker to produce data:
Copy
Ask AI
from faker import Fakerfrom app.schemas import CustomerModelfake = Faker()def generate_fake_customers(count: int) -> list[CustomerModel]: return [ CustomerModel( first_name=fake.first_name(), last_name=fake.last_name(), email_address=fake.email(), age=fake.random_int(min=18, max=80), city=fake.city(), occupation=fake.job(), ) for _ in range(count) ]