Random Hex Color Generator API with Fastify

  • Share this:

Code introduction


This function creates a Fastify server that provides an API for generating random hexadecimal colors.


Technology Stack : Fastify

Code Type : API function

Code Difficulty : Intermediate


                
                    
def random_color_hex():
    import random
    import fastify

    app = fastify.Fastify()

    @app.get("/random-color")
    async def random_color(request, reply):
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        return {"color": f"#{r:02x}{g:02x}{b:02x}"}

    app.run()                
              
Tags: