DEV Community

Free Python Code
Free Python Code

Posted on

Create Quotes API using FastAPI and Python

Hi 🙂

Welcome to a new post. Today I will share with you how to create quotes using the FastAPI and Python APIs. It is very easy to make an API like this. You need to create a JSON file to store quotes. Use this link: https://www.goodreads.com/quotes/ to find more quotes.


{
    "quotes": [
        {
            "quote": "If children were brought into the world by an act of pure reason alone, would the human race continue to exist? Would not a man rather have so much sympathy with the coming generation as to spare it the burden of existence, or at any rate not take it upon himself to impose that burden upon it in cold blood?",
            "author": "Arthur Schopenhauer"
        },

        {
            "quote" : "The idea of bringing someone into the world fills me with horror. I would curse myself if I were a father. A son of mine! Oh no, no, no! May my entire flesh perish and may I transmit to no one the aggravations and the disgrace of existence.",
            "author": "Gustave Flaubert"
        },

        {
            "quote" : "A coin is examined, and only after careful deliberation, given to a beggar, whereas a child is flung out into the cosmic brutality without hesitation.",
            "author": "Peter Wessel Zapffe"
        },

        {
            "quote" : "Parents have a child, and in doing so they bring into the world a monster that kills everything it comes in contact with.",
            "author": "Thomas Bernhard"
        },

        {
            "quote" : "Every time a man is begotten and born the clock of human life is wound up anew, to repeat once more its same old tune that has already been played innumerable times, movement by movement and measure by measure, with insignificant variations.",
            "author": "Arthur Schopenhauer"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Now we are ready to create the API. 🤗


import json
from fastapi import FastAPI


app = FastAPI()

quotes = json.load(open('quotes.json', 'r'))

@app.get('/')
def home():
    return {}

@app.get('/quotes')
def get_quotes():
    return quotes['quotes']


Enter fullscreen mode Exit fullscreen mode

run the API

uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

result

Image description

This is a very simple example of the create quotes API. You can add more features, like getting quotes by tag name, author name, and more.

Now we're done 🤗

Don't forget to like and follow 🙂

Support me on PayPal 🤗
https://www.paypal.com/paypalme/amr396

Top comments (0)