Random Quote API Endpoint

  • Share this:

Code introduction


This function creates an API endpoint using the Starlette framework that randomly returns a quote.


Technology Stack : Starlette, JSONResponse, Request, random

Code Type : API Function

Code Difficulty : Intermediate


                
                    
from starlette.responses import JSONResponse
from starlette.requests import Request
import random

def generate_random_quote(request: Request):
    quotes = [
        "The best way to predict the future is to invent it.",
        "Innovation distinguishes between a leader and a follower.",
        "The future belongs to those who believe in the beauty of their dreams.",
        "The only way to do great work is to love what you do."
    ]
    
    selected_quote = random.choice(quotes)
    return JSONResponse(content={"quote": selected_quote})

# JSON Explanation