Content
Explore The Hero’s JourneyStorytelling Techniques Applied to Social Media - PART I
March 9, 2023Supercharge your dev skills and build your API in record time using Python and FastAPI
March 9, 2023
By: Michael Cardoza
Quit dawdling with slow and outdated approaches and say goodbye to the tedious process of building APIs from scratch!
Introducing the champions of rapid development: Python and FastAPI!
Join forces with this formidable duo, and embark on a journey of unmatched speed and efficiency. In the next few minutes, you’ll unlock the magic of creating APIs, broken down into five incredibly straightforward steps.
With a few taps on your keyboard, lay the foundation of your exciting project.
1 $ mkdir app && cd app
2 $ python3 -m venv venv
3 $ source venv/bin/activate
4
Before we begin crafting, let’s arm ourselves with the essential tools.
1 $ pip install fastapi
2 $ pip install "uvicorn[standard]"
3
Time to put pen to paper (or, more aptly, fingers to keys). Watch as lines of code morph into an interactive digital interface.
1 from typing import Union
2
3 from fastapi import FastAPI
4
5 app = FastAPI()
6
7
8 @app.get("/")
9 def read_root():
10 return {"say-hello": "Hello Bits Kingdom"}
11
12
13 @app.get("/items/{item_id}")
14 def read_item(item_id: int, q: Union[str, None] = None):
15 return {"item_id": item_id, "q": q}
16
Fire up the engines and behold the birth of your application.
1 $ uvicorn main:app --reload
2
3 INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
4 INFO: Started reloader process [28720]
5 INFO: Started server process [28722]
6 INFO: Waiting for application startup.
7 INFO: Application startup complete.
8
The moment of truth! Launch your browser and see your creation come to life.
Open your browser at http://127.0.0.1:8000 or http://127.0.0.1:8000/items/5?q=somequery
1 {
2 "say-hello": "Hello Bits Kingdom"
3 }
Can’t get enough of our Bits Tips? Our dev experts are constantly chipping in with tips that help you improve your coding in a snap! Check out these Terminal, Shell, or Console secrets from a super coder!
✍🏾 This article was written by our copywriter, Sofia, in collaboration with our DEV expert, Michael.