Fastify Server with Random User Data Endpoint

  • Share this:

Code introduction


This function creates a Fastify server and provides a GET endpoint to generate random user data. The user data includes a unique ID, name, and email.


Technology Stack : Fastify, UUID, Random

Code Type : Web API

Code Difficulty : Intermediate


                
                    
def random_user_data():
    import fastify
    import random
    import uuid

    app = fastify.Fastify()

    @app.get("/generate")
    async def generate_user_data(request, reply):
        user_data = {
            "id": str(uuid.uuid4()),
            "name": f"User_{random.randint(1000, 9999)}",
            "email": f"user{random.randint(1000, 9999)}@example.com"
        }
        return user_data

    return app